基于c++下的opencv对红蓝板进行识别的代码
时间: 2023-04-03 22:03:49 浏览: 125
以下是基于 C 语言下的 OpenCV 对红蓝板进行识别的代码:
```
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat image = imread("board.jpg");
Mat hsv_image;
cvtColor(image, hsv_image, COLOR_BGR2HSV);
Mat blue_mask, red_mask;
inRange(hsv_image, Scalar(100, 100, 100), Scalar(120, 255, 255), blue_mask);
inRange(hsv_image, Scalar(0, 100, 100), Scalar(10, 255, 255), red_mask);
vector<vector<Point>> blue_contours, red_contours;
findContours(blue_mask, blue_contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
findContours(red_mask, red_contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
for (size_t i = 0; i < blue_contours.size(); i++)
{
drawContours(image, blue_contours, i, Scalar(255, 0, 0), 2);
}
for (size_t i = 0; i < red_contours.size(); i++)
{
drawContours(image, red_contours, i, Scalar(0, 0, 255), 2);
}
imshow("Result", image);
waitKey(0);
return 0;
}
```
希望对您有帮助。
阅读全文