学习opencv day4

处理图像颜色

二级目录

1、colordetector.h未定义标识符CV_BGR2Lab

添加头文件#include "opencv2/imgproc/types_c.h"

一个困扰一两天的问题终于解决了我去!!!

一开始是碰到这样的报错:
错误LNK2019 无法解析的外部符号 main,该符号在函数 “int __cdecl invoke_main(void)” (?invoke_main@@YAHXZ) 中被引用

然后百度说可能是图片路径什么之类的问题,检查一下也没问题,图片是放在存放main函数文件夹中,后面又有一堆配置什么乱七八糟的,弄了半天还是无果。
再之后就对程序设置断点,一步一步执行,发现result矩阵里没有数据,即

cv::Mat result = cdetect.process(image);

这一句出了问题,再去.h文件中查找相关函数,发现只有函数声明

cv::Mat process(const cv::Mat &image);

没有具体定义,于是问题就是将此函数的定义在main函数中编辑出即可:

cv::Mat ColorDetector::process(const cv::Mat& image) {

    // re-allocate binary map if necessary
    // same size as input image, but 1-channel
    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 (getDistanceToTargetColor(*it) < maxDist) {

            *itout = 255;

        }
        else {

            *itout = 0;
        }

        // end of pixel processing ----------------
    }

    return result;
}

以上使用了在循环中使用迭代器的方法进行计算两个颜色向量间的距离。还有一种做法是调用OpenCV的系列函数:

cv::Mat operator()(const cv::Mat &image) {
	  
		  cv::Mat input;
		 
		  if (useLab) { // Lab conversion
			  cv::cvtColor(image, input, CV_BGR2Lab);
		  }
		  else {
			  input = image;
		  }

		  cv::Mat output;
		  // compute absolute difference with target color
		  cv::absdiff(input,cv::Scalar(target),output);
	      // split the channels into 3 images
	      std::vector<cv::Mat> images;
	      cv::split(output,images);
		  // add the 3 channels (saturation might occurs here)
	      output= images[0]+images[1]+images[2];
		  // apply threshold
          cv::threshold(output,  // input image
                      output,  // output image
                      maxDist, // threshold (must be < 256)
                      255,     // max value
                 cv::THRESH_BINARY_INV); // thresholding type
	
	      return output;
	  }

总结

《opencv计算机视觉编程攻略》源代码这个程序少了一部分代码,
只需加入即可,一步一步慢慢调试程序即可得出问题所在。

附上.h以及.cpp函数

https://download.csdn.net/download/qq_41815140/14928283

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值