#include <opencv2/core/core.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main( int argc, char** argv ){
//sets up image you want
Mat img = imread("shape.jpg",CV_LOAD_IMAGE_GRAYSCALE);
//checks to see if image was read
if(img.empty()){
cout<<"Image not found"<<endl;
return -1;
}
//identifies the edges on the picture
Canny(img, img, 200, 200,3 );
//creates a vector of all the points that are contoured
vector<vector<Point>> contours;
//needed for function
vector<Vec4i> hierarchy;
//finds all the contours in the image and places it into contour vector array
findContours( img, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
Mat drawing = Mat::zeros( img.size(), CV_8UC3 );
//loop allows you to re-draw out all the contours based on the points in the vector
for( int i = 0; i< contours.size(); i++ )
{
drawContours( drawing, contours, i, Scalar(0,255,0), 2, 8, hierarchy, 0, Point());
}
//shows the images
imshow("Pic",drawing);
waitKey(0);
destroyWindow("Pic");
}
我怎么需要这条线?
Mat drawing = Mat::zeros( img.size(), CV_8UC3 );
就像如果我注释掉那行然后在它下面的其余代码中将变量“drawing”更改为“img”,我运行它时怎么会出现黑屏?而不仅仅是精巧的转换图像,它使除了轮廓线之外的照片的其余部分变黑?我假设从我读到的零函数将图片中矩阵的值更改为 0 使其变为黑色,这将导致 for 循环在仅显示轮廓线的黑色图片上绘制。