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


    相似颜色的查找

    #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;
    }
  • 相关阅读:
    Eureka的使用
    自定义类加载器
    继承
    Active Objects模式
    Future设计模式
    mysql备份与还原
    多生产者多消费者(第一种方式),基于synchronized,wait,notifyAll
    自己实现阻塞队列(基于数组)
    linux定时任务每隔5分钟向文本追加一行
    自己实现CountDownLatch
  • 原文地址:https://www.cnblogs.com/Jason-AnHui/p/3474855.html
Copyright © 2020-2023  润新知