opencv学习之路(6):图像相似颜色的查找

相似颜色的查找

#include 
#include "opencv2/opencv.hpp"
#include 
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_ itbegin=image.begin();
    cv::MatIterator_ itend=image.end();
    cv::MatIterator_ out= result.begin();
    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<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;
}

 

转载于:https://www.cnblogs.com/Jason-AnHui/p/3474855.html

你可能感兴趣的:(opencv学习之路(6):图像相似颜色的查找)