当前位置:网站首页>[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);
}
边栏推荐
- How to modify data file path in DM database
- C case of communication between server and client based on mqttnet
- 面试会问的 Promise.all()
- Go Chan's underlying principles
- 6.30年终小结,学生时代结束
- MySQL table insert Chinese change? Solution to the problem of No
- June book news | 9 new books are listed, with a strong lineup and eyes closed!
- Steam教育的实际问题解决能力
- Rhcsa --- work on the third day
- 培养中小学生对教育机器人的热爱之心
猜你喜欢
Common errors of dmrman offline backup
ThinkPHP kernel work order system source code commercial open source version multi user + multi customer service + SMS + email notification
LeetCode-对链表进行插入排序
農業生態領域智能機器人的應用
Federal learning: dividing non IID samples according to Dirichlet distribution
Cultivate primary and secondary school students' love for educational robots
Thinkphp Kernel wo system source Commercial Open source multi - user + multi - Customer Service + SMS + email notification
Starting from the classification of database, I understand the map database
Mysql表insert中文变?号的问题解决办法
Several methods of capturing packets under CS framework
随机推荐
Common errors of dmrman offline backup
Change deepin to Alibaba image source
Future trend of automated testing ----- self healing technology
One step implementation of yolox helmet detection (combined with oak intelligent depth camera)
Geotrust OV Multi - Domain Domain SSL Certificate rmb2100 per year contains several Domain names?
Mouse events in JS
Markdown edit syntax
Virtual machine installation deepin system
Let正版短信测压开源源码
Mathematical problems (number theory) trial division to judge prime numbers, decompose prime factors, and screen prime numbers
初学爬虫-笔趣阁爬虫
C# 图片显示占用问题
Flag bits in assembly language: CF, PF, AF, ZF, SF, TF, if, DF, of
10 minute quick start UI automation ----- puppeter
Analyzing the hands-on building tutorial in children's programming
[understand one article] FD_ Use of set
Oracle和MySQL的基本区别(入门级)
C - derived classes and constructors
Rhcsa --- work on the fourth day
Research on the security of ognl and El expressions and memory horse