opencv颜色识别代码

2024-11-16 18:09:54
推荐回答(1个)
回答1:

#include 
#include 
#include "highgui.h"

#ifdef _DEBUG
#pragma comment(lib, "opencv_core245d.lib")
#pragma comment(lib, "opencv_highgui245d.lib")
#pragma comment(lib, "opencv_imgproc245d.lib")
#else
#pragma comment(lib, "opencv_core245.lib")
#pragma comment(lib, "opencv_highgui245.lib")
#pragma comment(lib, "opencv_imgproc245.lib")
#endif

int main()
{
 cv::VideoCapture capture(0);
 if (!capture.isOpened())
  return 0;
 cv::Mat image, HSV, channels[3];
 float threshold = 0.3;
 while (capture.read(image))
 {
  cv::cvtColor(image, HSV, CV_BGR2HSV);
  cv::split(HSV, channels);
  cv::threshold(channels[0], channels[1], 15, 1,CV_THRESH_BINARY_INV);
  cv::threshold(channels[0], channels[0], 245, 1,CV_THRESH_BINARY);
  channels[0] += channels[1];
  cv::erode(channels[0], channels[0], cv::Mat::ones(3,3,CV_8UC1), cv::Point(1,1), 3);
  cv::Scalar v = cv::sum(channels[0]);
  int result = v[0] > channels[0].total() * threshold;
  std::cout << result << std::endl;
 }
}