gpt4 book ai didi

c++ - OpenCV C++ 断言调用 drawContours 失败

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

我试图找到最大的正方形并将其绘制在原始图像上。

当我打电话

drawContours(input,(screenCnt),-1,Scalar(255,0,0),3);

出现以下错误:

E/cv::error(): OpenCV Error: Assertion failed (i < 0) in cv::Mat cv::InputArray::getMat(int) const, file /build/master_pack-android/opencv/modules/core/src/matrix.cpp, line 1260

我在 Github 和 Google 上找了很多东西,但我找不到任何相关的解决方案。

你能帮助知道如何解决的人吗?

这是我的代码。 (c++)

bool compareContourAreas(vector<Point> contour1, vector<Point> contour2){
double i = fabs(contourArea(Mat(contour1)));
double j = fabs(contourArea(Mat(contour2)));
return ( i < j );
}


Mat &input = *(Mat *) matAddrInput;
Mat &result = *(Mat *) matAddrResult;

Mat gray;
Mat edge;
resize(input,input, cv::Size(), 0.75, 0.75);

cvtColor(input,gray,CV_RGB2GRAY);
GaussianBlur(gray,gray,Size(3,3),0);
Canny(gray,edge,100,200,3, false);

vector<vector<Point>> contours;
vector<Vec4i> hierarchy;


findContours(edge,contours,hierarchy,CV_RETR_LIST,CHAIN_APPROX_SIMPLE);
sort(contours.begin(),contours.end(),compareContourAreas);

vector<Point> approx;
vector<Point> screenCnt;

for(size_t i = 0; i < contours.size(); i++){
approxPolyDP((Mat(contours[i])),approx,arcLength(Mat(contours[i]),true) * 0.02, true);
if(approx.size() == 4){
screenCnt = approx;
break;
}
}
if(screenCnt.size() != 0){
drawContours(input,(screenCnt),-1,Scalar(255,0,0),3); << error
}

最佳答案

drawContours 需要一系列轮廓作为输入,而不仅仅是单个轮廓。您可以在调用中直接创建数组,例如:

drawContours(input, std::vector<std::vector<cv::Point>>{screenCnt},-1,Scalar(255,0,0),3); 

关于c++ - OpenCV C++ 断言调用 drawContours 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46926298/

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