zoukankan      html  css  js  c++  java
  • Learning OpenCV Lecture 2 (Using the Strategy pattern in algorithm design)

    ColorDetector.h:
    #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    
    class ColorDetector
    {
    public:
          // empty constructor
         ColorDetector() : minDist(100) {
                // default parameter initialization here
                target[0] = target[1] = target[2] = 0;
         }
    
          // Sets the color distance threshold
          // Threshold must be positive
          // othrewise distance threshold is set to 0.
          void setColorDistanceThreshold(int distance) {
                if (distance < 0) {
                     distance = 0;
                }
                minDist = distance;
         }
          // Gets the color distance threshold
          int getColorDistanceThreshold() const {
                return minDist;
         }
    
          // Sets the color to be detected
          void setTargetColor(unsigned char red,
                unsigned char green,
                unsigned char blue) {
                     // BGR order
                     target[2] = red;
                     target[1] = green;
                     target[0] = blue;
         }
          // Sets the color to be detected
          void setTargetColor(cv::Vec3b color) {
                target = color;
         }
          // Gets the color to be detected
         cv::Vec3b getTargetColor() const {
                return target;
         }
    
         cv::Mat process(const cv::Mat &image);
          int getDistance(const cv::Vec3b& color) const;
    
    private:
          // minimum acceptable distance
          int minDist;
          // target color
         cv::Vec3b target;
          // image containing resulting binary map
         cv::Mat result;
    
    };
    

      ColorDetector.cpp:

    #include "ColorDetector.h"
    
    cv::Mat ColorDetector::process(const cv::Mat &image) {
         
          // re-allocate binary map if necessary
          // same size as input image, but 1-chaanel
         result.create(image.rows, image.cols, CV_8U);
    
          // get the iterators
         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 each pixel
          for ( ; it!= itend; ++it, ++itout) {
                // process each pixel ---------------------
                // compute distance from target color
                if (getDistance(*it) < minDist) {
                     *itout = 255;
                } else {
                     *itout = 0;
                }
                // end of pixel processing ----------------
         }
    
          return result;
    }
    
    int ColorDetector::getDistance(const cv::Vec3b& color) const {
          return abs(color[0] - target[0]) +
                  abs(color[1] - target[1]) +
                  abs(color[2] - target[2]);
    
          // Other methods to compute distance
    //   return static_cast<int>(
    //         cv::norm<int, 3>(cv::Vec3b(color[0] - target[0],
    //                                            color[1] - target[1],
    //                                            color[2] - target[2])));
    
    
    }

    main.cpp

    #include "ColorDetector.h"
    
    int main()
    {
          // 1. Create image processor boject
         ColorDetector cdetect;
    
          // 2. Read input image
         cv::Mat image = cv::imread( "test.jpg");
          if (!image.data) {
                return 0;
         }
    
          // 3. Set input parameters
         cdetect.setTargetColor(130, 190, 230); // here blue sky
    
         cv::namedWindow( "result");
         
          // 4. Process the image and display the result
         cv::imshow( "result", cdetect.process(image));
    
         cv::waitKey();
          return 0;
    }
  • 相关阅读:
    java 在线网络考试系统源码 springboot mybaits vue.js 前后分离跨域
    springboot 整合flowable 项目源码 mybiats vue.js 前后分离 跨域
    flowable Springboot vue.js 前后分离 跨域 有代码生成器 工作流
    Flowable 工作流 Springboot vue.js 前后分离 跨域 有代码生成器
    java 企业 网站源码 后台 springmvc SSM 前台 静态化 代码生成器
    java 进销存 商户管理 系统 管理 库存管理 销售报表springmvc SSM项目
    基于FPGA的电子计算器设计(中)
    基于FPGA的电子计算器设计(上)
    FPGA零基础学习:SPI 协议驱动设计
    Signal tap 逻辑分析仪使用教程
  • 原文地址:https://www.cnblogs.com/starlitnext/p/3861331.html
Copyright © 2011-2022 走看看