相似颜色的查找
#include <iostream> #include "opencv2/opencv.hpp" #include <math.h> using namespace std; cv::Vec3b target; int minDist; int getDistance(const cv::Vec3b &color) { return abs(color[0]-target[0])+ abs(color[1]-target[1])+ abs(color[2]-target[2]); } void setTargetColor(cv::Vec3b color){ target=color; } void setTargetColor(uchar Red,uchar Green,uchar Blue) { target[2]=Red; target[1]=Green; target[0]=Blue; } void process(cv::Mat &image,cv::Mat &result) { result.create(image.rows,image.cols,image.type()); cv::MatIterator_<cv::Vec3b> itbegin=image.begin<cv::Vec3b>(); cv::MatIterator_<cv::Vec3b> itend=image.end<cv::Vec3b>(); cv::MatIterator_<cv::Vec3b> out= result.begin<cv::Vec3b>(); int ipixelcount=0; for(;itbegin!=itend;itbegin++,out++) { if(getDistance(*itbegin)<minDist) { (*out)[0]=0xff; (*out)[1]=0xff; (*out)[2]=0xff; } else { *out=0; } ipixelcount++; } cout<<"size vec3b"<<sizeof(*out)<<endl; cout<<ipixelcount<<endl; } int main() { cv::Mat image=cv::imread("E:/QtMaterial/images/boldt.jpg"); minDist=100; cv::Mat result; setTargetColor(130,190,230); process(image,result); cv::namedWindow("SetColor"); cv::imshow("SetColor",result); cv::imshow("Origin",image); cv::waitKey(); return 0; }