当前位置:网站首页>[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);
}
边栏推荐
猜你喜欢

Embedded-c language-9-makefile/ structure / Consortium

Social media search engine optimization and its importance

奠定少儿编程成为基础学科的原理

ThinkPHP kernel work order system source code commercial open source version multi user + multi customer service + SMS + email notification

Unity particle Foundation

Keil compilation code of CY7C68013A

数学知识(欧拉函数)

idea自動導包和自動删包設置

Detailed process of DC-1 range construction and penetration practice (DC range Series)

Leetcode merge sort linked list
随机推荐
Win10 disk management compressed volume cannot be started
Record the bug of unity 2020.3.31f1 once
Leetcode- insert and sort the linked list
Several methods of capturing packets under CS framework
js面试收藏试题1
解决:代理抛出异常错误
How to write a client-side technical solution
[high speed bus] Introduction to jesd204b
Interview question: do you know the difference between deep copy and shallow copy? What is a reference copy?
数学问题(数论)试除法做质数的判断、分解质因数,筛质数
Mathematical knowledge -- understanding and examples of fast power
JS interview collection test question 1
Ansible installation and use
Oracle stored procedure and job task setting
Solution of DM database unable to open graphical interface
cs架构下抓包的几种方法
Oracle和MySQL的基本区别(入门级)
Getting started with pytest ----- confitest Application of PY
Analyze the space occupied by the table according to segments, clusters and pages
【ClickHouse】How to create index for Map Type Column or one key of it?