• 写一手好的C++代码 ,利用Strategy模式写的


    效果:把蓝天部分给提取出来
    #include <opencv2/core/core.hpp>
    #include<opencv2/imgproc/imgproc.hpp>
    #include <opencv2/highgui/highgui.hpp>
    
    using namespace cv;
    using namespace std;
    
    class ColorDetector{
    
        private:
    
            int minDist;
            Vec3b target;
            Mat result;
    
        public:
    
        ColorDetector():minDist(100){
            target[0] = target[1] = target[2] = 0;
        }
        //设置色彩距离阈值
        void setColorDistanceThreshold(int distance){
            if (distance < 0)
                distance = 0;
            minDist = distance;
        }
        //get set方法设置Private里面的变量
        int getColorDistanceThreshold()const{
            return minDist;
        }
        //设置需检测的颜色
        void setTargetColor(unsigned char red, unsigned char green, unsigned char blue){
            target[2] = red;
            target[1] = green;
            target[0] = blue;
        }
        void setTargetColor(Vec3b color){
            target = color;
        }
        Vec3b getTargetColor()const{
            return target;
        }
    
        //计算与目标颜色的距离,采用的是街区距离
        int getDistance(const Vec3b& color) const{
            return abs(color[0] - target[0]) +
                abs(color[1] - target[1]) +
                abs(color[2] - target[2]);
        }
    
        Mat ColorDetector::process(const Mat &image){
            result.create(image.rows,image.cols, CV_8U);
            Mat_<Vec3b>::const_iterator it = image.begin<Vec3b>();
            Mat_<Vec3b>::const_iterator itend = image.end<Vec3b>();
            Mat_<uchar>::iterator itout = result.begin<uchar>();
            for (; it != itend; ++it, ++itout){
                if (getDistance(*it) < minDist){
                    *itout = 255;
                }
                else{
                    *itout = 0;
                }
            }
            return result;
        }
    };
    
    int main(){
        //创建对象
        ColorDetector cdector;
        Mat image = imread("boldt.jpg");
        if (!image.data){ return 0; }
        cdector.setTargetColor(130, 190, 230);
        namedWindow("result");
        imshow("result", cdector.process(image));
        waitKey(0);
        return 0;
    }
  • 相关阅读:
    uni_app 跳转到外部html5 的页面 (web-view) 的一些使用
    vue初学备份(动态切换class类)
    MySQL数据库基本操作
    蛋疼的JavaScript(二)this
    Java中的String,StringBuffer和StringBuilder
    LintCode_1 单例模式
    [转载]C语言EOF是什么?
    Python中的枚举
    对BOM的总结
    python类相关总结(持续更新)
  • 原文地址:https://www.cnblogs.com/lcj1105/p/4981498.html
Copyright © 2020-2023  润新知