Mat imgthreshold; Mat imgsrc = cv::imread("C:/Users/Dell/Pictures/Camera Roll/Yelan.jpg",0); imshow("imgsrc", imgsrc); threshold(imgsrc, imgthreshold,50, 110, THRESH_BINARY); imshow("imgthreshold", imgthreshold); vector<vector<Point>> contours; findContours(imgthreshold, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE); cv::Mat imgdraw = imgsrc.clone(); drawContours(imgdraw, contours, -1, Scalar(0, 0, 255),1); imshow("imgdraw", imgdraw); waitKey(0);为什么输出的是白色边框
时间: 2024-03-15 20:45:58 浏览: 23
在这个问题中,你使用了灰度图像作为输入图像,然后使用阈值函数 threshold 对其进行二值化处理。在二值化过程中,所有低于阈值的像素都被设为 0,而高于阈值的像素都被设为 255。因此,输出的图像 imgthreshold 中只有两种颜色,即黑色和白色,没有其他颜色。在使用 drawContours 函数绘制边框时,你将颜色设置为红色 (0, 0, 255),但是由于图像中只有黑色和白色,所以绘制出来的边框是白色,而不是红色。
要输出红色边框,你需要在二值化后的图像上绘制轮廓,而不是在灰度图像上绘制轮廓。这可以通过将函数 findContours 的第一个参数设置为 imgthreshold 来实现。修改代码如下:
```c++
Mat imgthreshold;
Mat imgsrc = cv::imread("C:/Users/Dell/Pictures/Camera Roll/Yelan.jpg",0);
imshow("imgsrc", imgsrc);
threshold(imgsrc, imgthreshold,50, 110, THRESH_BINARY);
imshow("imgthreshold", imgthreshold);
vector<vector<Point>> contours;
findContours(imgthreshold, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
cv::Mat imgdraw = imgthreshold.clone(); // 修改这里
drawContours(imgdraw, contours, -1, Scalar(0, 0, 255), 1);
imshow("imgdraw", imgdraw);
waitKey(0);
```