OpenCV C++,为什么不使用归零函数会出现黑屏?

OpenCV C++, how come I get a black screen without using the zero function?

#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" 一样,当我 [=18] 时为什么会出现黑屏=] 吗?而不仅仅是精明的转换图像使照片的其余部分除了轮廓线变黑?我假设从我读到的零函数将图片中矩阵的值更改为 0 使其变为黑色,这将导致 for 循环绘制仅显示轮廓线的黑色图片。

根据 documentation of findContours():

image – Source, an 8-bit single-channel image. Non-zero pixels are treated as 1’s. Zero pixels remain 0’s, so the image is treated as binary ... The function modifies the image while extracting the contours.

特别是将镜像的类型修改为8UC1。最后,函数 drawContours() 以黑色打印轮廓,因为它使用 Scalars(0,255,0)。如果您使用 Scalar(255,0,0),问题将不会被注意到。

只需将调用修改为drawContours()

drawContours( img, contours, i, Scalar(255), 2, 8, hierarchy, 0, Point());

PS: Octopusthere的功能可以用来打印图片的类型