【问题标题】:OpenCV C++, how come I get a black screen without using the zero function?OpenCV C++,不使用归零功能怎么会黑屏?
【发布时间】:2016-06-12 18:29:13
【问题描述】:
#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 循环在仅显示轮廓线的黑色图片上绘制。

【问题讨论】:

    标签: c++ opencv opencv-contour


    【解决方案1】:

    根据documentation of findContours()

    image – 源,8 位单通道图像。非零像素被视为 1。零像素保持为 0,因此图像被视为二进制... 函数在提取轮廓的同时修改图像

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

    只需修改调用drawContours()

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

    PS:八达通there的功能可以打印图片的类型。

    【讨论】:

    • 现在完全可以理解为什么他们添加了零函数,以便他们能够拥有 8UC3 的图像,从而允许他们使用颜色 BGR 来绘制轮廓。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-21
    • 1970-01-01
    • 2010-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多