zoukankan      html  css  js  c++  java
  • 提取图形中指定颜色的所有像素_opencv/c++


    #include "stdafx.h"
    #include<iostream>
    #include<opencv2/core/core.hpp>
    #include<opencv2/imgproc/imgproc.hpp>
    #include<opencv2/highgui/highgui.hpp>
    using namespace cv;

    class ColorDetector
    {
    private:
    //最小可接受距离
    int minDist;
    //目标色
    cv::Vec3b target;
    //结果图像
    cv::Mat result;

    //计算与目标颜色的距离
    int getDistance(cv::Vec3b color)
    {
    return abs(color[0] - target[0]) + abs(color[1] - target[1]) + abs(color[2] - target[2]);
    }
    public:
    //空构造函数
    ColorDetector() :minDist(100)
    {
    //初始化默认参数
    target[0] = target[1] = target[2] = 0;
    }
    void setColorDistanceThreshold(int distance);
    int getColorDistanceThreshold() const;
    void setTargetColor(unsigned char red, unsigned char green, unsigned char blue);
    void setTargetColor(cv::Vec3b color);
    cv::Vec3b getTargetColor() const;
    cv::Mat ColorDetector::process(const cv::Mat &image);
    };

    //设置色彩距离阈值,阈值必须是正的,否则设为0
    void ColorDetector::setColorDistanceThreshold(int distance)
    {
    if (distance < 0)
    distance = 0;
    minDist = distance;
    }

    //获取色彩距离阈值
    int ColorDetector::getColorDistanceThreshold() const
    {
    return minDist;
    }

    //设置需检测的颜色
    void ColorDetector::setTargetColor(unsigned char red, unsigned char green, unsigned char blue)
    {
    //BGR顺序
    target[2] = red;
    target[1] = green;
    target[0] = blue;
    }


    //设置需检测的颜色
    void ColorDetector::setTargetColor(cv::Vec3b color)
    {
    target = color;
    }
    //获取需检测的颜色
    cv::Vec3b ColorDetector::getTargetColor() const
    {
    return target;
    }

    cv::Mat ColorDetector::process(const cv::Mat &image)//核心的处理方法
    {
    //按需重新分配二值图像
    //与输入图像的尺寸相同,但是只有一个通道
    result.create(image.rows, image.cols, CV_8U);

    //得到迭代器
    cv::Mat_<cv::Vec3b>::const_iterator it = image.begin<cv::Vec3b>();
    cv::Mat_<cv::Vec3b>::const_iterator itend = image.end<cv::Vec3b>();
    cv::Mat_<uchar>::iterator itout = result.begin<uchar>();
    for (; it != itend; ++it, ++itout)//处理每个像素
    {
    //计算离目标颜色的距离
    if (getDistance(*it) < minDist)
    {
    *itout = 255;
    }
    else
    {
    *itout = 0;
    }
    }
    return result;
    }

    int _tmain(int argc, _TCHAR* argv[])
    {
    //1.创建图像处理的对象
    ColorDetector cdetect;
    //2.读取输入图像
    cv::Mat image = cv::imread("boldt.jpg");
    if (!image.data)
    {
    return 0;
    }
    //3.设置输入参数
    cdetect.setTargetColor(130, 190, 230);//蓝天的颜色
    cv::namedWindow("result");
    //4.处理并显示结果
    cv::imshow("result", cdetect.process(image));
    cv::waitKey();

    return 0;
    }

  • 相关阅读:
    DB2数据库基础
    mysqldump 备份数据库脚本
    Windows Server 2012 R2 卸载IE浏览器
    mysqlpump 和 mysql_config_editor测试
    Linux 修改时区
    PowerShe 消息提示框测试
    Python使用libsvm的“ImportError: No module named svmutil”问题
    常见的机器学习&数据挖掘知识点
    [猜你喜欢]冠军“yes,boy!”分享,含竞赛源代
    用户人品预测大赛获奖团队分享
  • 原文地址:https://www.cnblogs.com/hnzsb-vv1130/p/6587097.html
Copyright © 2011-2022 走看看