gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 22:50:57 24 4
gpt4 key购买 nike

#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 循环绘制仅显示轮廓线的黑色图片。

最佳答案

根据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:八达通的功能there可用于打印图像的类型。

关于c++ - OpenCV C++,为什么不使用归零函数会出现黑屏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37777707/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com