gpt4 book ai didi

c++ - OpenCV 寻找轮廓

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

以下用于在图像中查找轮廓的代码不会给出任何编译错误。但是,在运行时出现错误OpenCV imshow 文件中的“Open cv:Assertion failed (size.width > 0 && size.height > 0)”。

我只用 imshow 函数尝试了代码,删除了它后面的所有内容,代码运行正常,因此文件位置似乎不是问题!

任何帮助将不胜感激。提前致谢!

#include <opencv\cv.h>
#include <opencv2\highgui\highgui.hpp>
#include <opencv\cvaux.h>
#include <opencv\cxcore.h>
#include <opencv2\imgproc\imgproc.hpp>
#include <iostream>
#include <conio.h>

using namespace cv;
using namespace std;

int main() {
Mat img1;
Mat output;

Mat img = imread("blue.jpg");

cvtColor(img, img1, CV_BGR2GRAY);
threshold(img1, output, 176, 255, CV_THRESH_BINARY);
imshow("hi", output);
vector<vector<Point>> Contours;
vector<Vec4i> hier;
Mat final;

findContours(img1, Contours, hier, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);

for (int i = 0; i < Contours.size(); i++)
{
drawContours(final, Contours, i, Scalar(0, 255, 0), 1, 8, hier);
}

imshow("result", final);

waitKey();
}

最佳答案

你正在绘制一个未初始化的矩阵(final):

Mat final;
....
drawContours(final, Contours, i, Scalar(0, 255, 0), 1, 8, hier);

你应该首先初始化final,比如:

Mat final = img.clone();

关于c++ - OpenCV 寻找轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31317568/

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