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",为什么当我运行它时会出现黑屏?而不仅仅是精明转换的图像,使照片的其余部分除了轮廓线变黑?我假设从我读到的内容中,零函数将图片中矩阵的值更改为 0 使其变为黑色,这将导致 for 循环在仅显示轮廓线的黑色图片上绘制。
根据findContours()
的文档:
图像 – 源,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:章鱼的功能可以用来打印图像的类型。
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 多态性和功能结合
- 带内存和隔离功能的SQLite
- 在CMakeLists.txt的安装功能中使用.cmake文件有什么用
- 类模板的成员功能的定义在单独的TU中完全专业化
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何在C++中获得"静态纯虚拟"功能?
- 两个文件使用彼此的功能-如何解决
- 我应该实现右值推送功能吗?我应该使用std::move吗
- QML按钮点击功能执行顺序
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 有没有可能有一个只有ADL才能找到的非好友功能
- 功能样式转换从 'int' 到 'ItemType' 的匹配转换
- 文件系统:复制功能的速度秘诀是什么
- 在用于格式4的arm模拟器中实现功能时的一个问题
- 如何在Directwrite中获得给定字体的可用OpenType功能
- 对可变参数使用声明.如何选择正确的功能
- 询问在设计我的手臂模拟器功能表示格式1
- 功能原型的目的
- 这里在 Linux 中具有"CreatePipe"和"CreateProcessW"功能吗?