OPENCV / c++: approxpolydp断言失败错误
OPENCV / C++ : approxpolydp assertion failed error
我目前正在为学生做一个嵌入式视觉项目& &;记者,但我遇到了麻烦。我刚得到一个错误提示:
OpenCV Error: Assertion failed (npoints>= 0 &&(depth == CV_32S || depth == CV_32F))在approxPolyDP,文件/home/linux/opencv/modules/imgproc/src/approx.cpp,第679行抛出'cv::Exception'实例后调用终止what():/home/linux/opencv/modules/imgproc/src/approx.cpp:679: error:(-215) npoints>= 0 &&(depth == CV_32S || depth == CV_32F) in function approxPolyDP
你会在最后找到我的完整代码,这是我认为制造麻烦的代码:
Rect bounding_rect;
Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );
for( int i = 0; i< contours.size(); i++ )
{
// Find the largest area of contour, and the bounding rect for the largest contour
double a=contourArea( contours[i],false);
if(a>largest_area)
{
largest_area=a;cout<<i<<" area "<<a<<endl;
largest_contour_index=i;
bounding_rect=boundingRect(contours[i]);
}
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
//drawContours( drawing, contours_poly, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
rectangle( drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0 );
drawContours( src, contours_poly, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
}
approxPolyDP( Mat(contours[largest_contour_index]), contours_poly[0],20, true );
或者可能是因为我在后面放了很多if循环?由于我不是一个优秀的程序员,我有时会在网上获取一些示例,并将它们组合到我的代码中,并添加自己编写的代码,这对任何真正的程序员来说都没有真正的意义,而所有这些最终都会导致像这样的错误。无论如何,感谢你的任何提示/帮助!
如果有帮助,下面是我的完整代码:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace cv;
using namespace std;
Mat src;
Mat src_gray;
Mat dst(src.rows,src.cols,CV_8UC1,Scalar::all(0));
Mat transformed;
int thresh = 100;
int max_thresh = 255;
RNG rng(12345);
///function main ///
int main( int argc, char** argv )
{
int largest_area=0;
int largest_contour_index=0;
VideoCapture cap("test1.mp4");
if (!cap.isOpened()) //exit the main if not successful
{
cout << "Cannot open the web cam" << endl;
return -1;
}
while (true)
{
bool bSuccess = cap.read(src); // read a new frame from video
if (!bSuccess) //if not success, break loop
{
cout << "Cannot read a frame from video stream" << endl;
break;
}
cvtColor( src, src_gray, CV_BGR2GRAY );
blur( src_gray, src_gray, Size(3,3) );
char* source_window = "Source";
namedWindow( source_window, CV_WINDOW_AUTOSIZE );
imshow( source_window, src );
Mat threshold_output;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
// Detect edges using Threshold
threshold( src_gray, threshold_output, 190, 255, THRESH_BINARY );
// Find contours
findContours( threshold_output, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
// Approximate contours to polygons + get bounding rects
vector<vector<Point> > contours_poly( contours.size() );
vector<Rect> boundRect( contours.size() );
vector<Point2f>center( contours.size() );
vector<float>radius( contours.size() );
// Draw polygonal contour + bonding rects around the object
Rect bounding_rect;
Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );
for( int i = 0; i< contours.size(); i++ )
{
// Find the largest area of contour, and the bounding rect for the largest contour
double a=contourArea( contours[i],false);
if(a>largest_area)
{
largest_area=a;cout<<i<<" area "<<a<<endl;
largest_contour_index=i;
bounding_rect=boundingRect(contours[i]);
}
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
//drawContours( drawing, contours_poly, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
rectangle( drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0 );
drawContours( src, contours_poly, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
}
approxPolyDP( Mat(contours[largest_contour_index]), contours_poly[0],20, true );
if(contours_poly[0].size()==4)
{
std::vector<Point2f> quad_pts;
std::vector<Point2f> squre_pts;
for (int j = 0; j < 4 ; j++)
{
if ( contours_poly[0][j].x <= 900 && contours_poly[0][j].y <= 500 )
{
quad_pts.push_back(Point2f(contours_poly[0][j].x,contours_poly[0][j].y));
continue;
}
continue;
}
for (int k = 0; k < 4 ; k++)
{
if ( contours_poly[0][k].x <= 900 && contours_poly[0][k].y >= 500 )
{
quad_pts.push_back(Point2f(contours_poly[0][k].x,contours_poly[0][k].y));
continue;
}
continue;
}
for (int l = 0; l < 4 ; l++)
{
if ( contours_poly[0][l].x >= 900 && contours_poly[0][l].y <= 500 )
{
quad_pts.push_back(Point2f(contours_poly[0][l].x,contours_poly[0][l].y));
continue;
}
continue;
}
for (int m = 0; m < 4 ; m++)
{
if ( contours_poly[0][m].x >= 900 && contours_poly[0][m].y >= 500 )
{
quad_pts.push_back(Point2f(contours_poly[0][m].x,contours_poly[0][m].y));
continue;
}
continue;
}
squre_pts.push_back(Point2f(bounding_rect.x,bounding_rect.y));
squre_pts.push_back(Point2f(bounding_rect.x,bounding_rect.y+bounding_rect.height));
squre_pts.push_back(Point2f(bounding_rect.x+bounding_rect.width,bounding_rect.y));
squre_pts.push_back(Point2f(bounding_rect.x+bounding_rect.width,bounding_rect.y+bounding_rect.height));
Mat transmtx = getPerspectiveTransform(quad_pts,squre_pts);
cout << "quad =" << quad_pts << " squre =" << squre_pts << endl;
imwrite("perspecTrans.jpg",transmtx);
transformed = Mat::zeros(src.rows, src.cols, CV_8UC3);
warpPerspective(src, transformed, transmtx, src.size());
Point P1=contours_poly[0][0];
Point P2=contours_poly[0][1];
Point P3=contours_poly[0][2];
Point P4=contours_poly[0][3];
line(src,P1,P2, Scalar(0,0,255),1,CV_AA,0);
line(src,P2,P3, Scalar(0,0,255),1,CV_AA,0);
line(src,P3,P4, Scalar(0,0,255),1,CV_AA,0);
line(src,P4,P1, Scalar(0,0,255),1,CV_AA,0);
rectangle(src,bounding_rect,Scalar(255,255,0),1,8,0);
rectangle(transformed,bounding_rect,Scalar(0,255,255),1,8,0);
namedWindow("1",CV_WINDOW_AUTOSIZE);
imshow("1", transformed);
imwrite("result1.jpg",dst);
imwrite("result2.jpg",src);
imwrite("result3.jpg",transformed);
waitKey();
}
else
cout<<"Pb with the 4 corners using approxPolyDP?"<<endl;
Mat ROI=transformed(bounding_rect); //Set ROI on source image
imwrite("cropped.jpg",ROI); //save ROI image
namedWindow( "Contours", CV_WINDOW_AUTOSIZE );
imshow( "Contours", src );
waitKey(0);
destroyAllWindows();
}
return(0);
}
问题解决了:我只需要在for循环之前将largest_area重新初始化为0。我不认为这对任何人都有帮助但我写这些只是以防万一。
相关文章:
- 尝试使用 std::vector<std::thread时出现静态断言失败错误>
- uint_not_usable_without_attribute在业力规则中使用数字生成器时静态断言失败
- 从 exe 文件 (Visual Studio ) 启动时调试断言失败
- 在 CppUnit 中测试中止断言失败
- 使用扫描的调试断言失败
- 为什么我的Qt程序在断言失败后继续运行?
- 图片不显示,关闭时出错 --> 调试断言失败!表达式:is_block_type_valid(标头>_block_use)
- 访问提升:shared_ptr 主范围外崩溃,断言失败:px != 0.指针的正确用法是什么?
- C++ 调试断言失败 - 矢量下标超出视觉工作室的范围
- 错误:断言失败 (src.type() == CV_8UC1) 在阈值中
- Boost Beast 异步服务器失败,断言失败:(id_ != T::id) 在多个 aync 调用中
- 使用imwrite OpenCV时断言失败
- 调试断言失败的缓冲区!=nullptr
- 使用 ofstream 写入文本文件时断言失败
- 提升继续恢复断言失败
- 获取有关调试断言失败的错误:表达式:"(_Ptr_user &(_BIG_ALLOCATION_ALIGNMENT -1)) == 0" &&0
- OpenCV(3.4.1) 错误:断言失败(变暗 <= 2 && step[0] > 0) in cv::Mat::locateROI
- 在Win10上使用带有Qt5的OpenCV3.0,调试断言失败
- MFC GetDC() 断言失败 - "Not a window"
- OpenCV 错误:断言失败 - 矩阵减去