当前位置:网站首页>[opencv] image binarization
[opencv] image binarization
2022-07-02 04:58:00 【Water cut off passenger】
Binary image processing
Image binarization , It is to turn the color in the image into black according to the threshold / White two
The process of binarization
The pseudo code of binarization process is described as follows
for (;it != itend;it++, itout++) {
if (get_ColorToTargetDistance(colorx) <= _distance) {
*itout = 0;
}
else {
*itout = 255;
}
}
among it
Is an iterator of the source image ,itout
Is the iterator of the binary result container ;
get_ColorToTargetDistance()
Used to get the difference between color and threshold ;
The difference between the colors of two three channels can be calculated by the following formula :
distance =
abs(color1[0] - color2[0]) +
abs(color1[1] - color2[1]) +
abs(color1[2] - color2[2]);
If the color in the picture is near the threshold , Just change it to 0; Otherwise, it should be changed to 255;
The results are as follows :
Go straight to the code :
Binarization processing class ColorDetector
/* ColorDetector @brief: For binary processing @member function: > > > > */
#include <vector>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using std::vector;
using namespace cv;
class ColorDetector {
public:
/* Constructors */
ColorDetector() :
_distance(0)
{
};
/* Destructor */
~ColorDetector() {
};
/* binarization */
inline cv::Mat process(cv::Mat& image) {
_result.create(image.size(), CV_8U);
auto it = image.begin<cv::Vec3b>();
auto itend = image.end<cv::Vec3b>();
auto itout = _result.begin<uchar>();
std::vector<int> colorx(5);
for (;it != itend;it++, itout++) {
colorx[0] = it[0][0];
colorx[1] = it[0][1];
colorx[2] = it[0][2];
if (get_ColorToTargetDistance(colorx) <= _distance) {
*itout = 0;
}
else {
*itout = 255;
}
}
return _result;
};
/* Set the threshold of each channel */
inline void setTargetColor(const int blue, const int red, const int green) {
_target = std::vector<int>{
blue,red,green };
};
/* Set tolerances */
inline void setColorDistanceThreshold(int distance) {
_distance = (distance > 0) ? distance : 0;
};
/* Obtain tolerance */
inline int get_Distance() {
return _distance;
};
/* Get the urban distance from the color to the threshold */
inline int get_ColorToTargetDistance(std::vector<int>& color) {
return get_ColorDistance(_target, color);
}
/* Get the urban distance of the color */
inline int get_ColorDistance(std::vector<int>& color1, std::vector<int>& color2) {
return abs(color1[0] - color2[0]) + abs(color1[1] - color2[1]) + abs(color1[2] - color2[2]);
}
private:
vector<int> _target;/* threshold */
int _distance;/* tolerance */
cv::Mat _result;/* Binarization results */
};
Calling procedure
int main()
{
Mat imga = imread("D:\\cat.jpg");
ColorDetector cdetector;
/* Set the threshold */
cdetector.setTargetColor(50,50,50);
/* Set tolerances */
cdetector.setColorDistanceThreshold(150);
/* binarization */
cv::Mat result = cdetector.process(imga);
imshow("src", imga);;
imshow("zhc", result);;
waitKey(0);
}
边栏推荐
- Fasttext text text classification
- Feign realizes file uploading and downloading
- How do I interview for a successful software testing position? If you want to get a high salary, you must see the offer
- Starting from the classification of database, I understand the map database
- What data does the main account of Zhengda Meiou 4 pay attention to?
- Win10 disk management compressed volume cannot be started
- 10 minute quick start UI automation ----- puppeter
- idea自動導包和自動删包設置
- Use of Baidu map
- oracle 存储过程与job任务设置
猜你喜欢
Starting from the classification of database, I understand the map database
Mysql database learning
奠定少儿编程成为基础学科的原理
Thinkphp內核工單系統源碼商業開源版 多用戶+多客服+短信+郵件通知
Leetcode- insert and sort the linked list
Unity particle Foundation
TypeScript函数详解
Learn AI safety monitoring project from zero [attach detailed code]
LeetCode-对链表进行插入排序
Express logistics quick query method, set the unsigned doc No. to refresh and query automatically
随机推荐
Solution: the agent throws an exception error
Ansible installation and use
ansible安装与使用
正大美欧4的主账户关注什么数据?
Summary of MySQL key challenges (2)
Getting started with pytest -- description of fixture parameters
Here comes the chicken soup! Keep this quick guide for data analysts
Introduction to Luogu 3 [circular structure] problem list solution
Leetcode basic programming: array
Rhcsa --- work on the fourth day
Mouse events in JS
Record the bug of unity 2020.3.31f1 once
C - derived classes and constructors
C# 基于MQTTNet的服务端与客户端通信案例
A new attribute value must be added to the entity entity class in the code, but there is no corresponding column in the database table
Summary of common string processing functions in C language
DC-1靶场搭建及渗透实战详细过程(DC靶场系列)
Knowledge arrangement about steam Education
How to configure PostgreSQL 12.9 to allow remote connections
Keil compilation code of CY7C68013A