0
OpenCV中的以下代碼是用於檢測黃色球並繪製其凸包。該代碼雖然沒有給出任何編譯錯誤,但在輸出窗口中給出了以下錯誤。 我使用了最大面積函數來避免較小的不需要的輪廓。 該錯誤是Open cv找到凸包
斷言在CV :: drawContours,文件C失敗 = contourIdx & & contourIdx <最後>:Buildsmasters ..(一些路徑),則線2299 ****
#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>
#include <stdlib.h>
using namespace cv;
using namespace std;
int main(){
Mat img, frame, img2, img3;
double maxarea = 0;
int lrgctridx; //largest contour index
VideoCapture cam(0);
while (true){
cam.read(frame);
cvtColor(frame, img, CV_BGR2HSV);
//thresholding
inRange(img, Scalar(0, 143, 86), Scalar(39, 255, 241), img2);
//finding contours
vector<vector<Point>> Contours;
vector<Vec4i> hier;
//morphological transformations
erode(img2, img2, getStructuringElement(MORPH_RECT, Size(3, 3)));
erode(img2, img2, getStructuringElement(MORPH_RECT, Size(3, 3)));
dilate(img2, img2, getStructuringElement(MORPH_RECT, Size(8, 8)));
dilate(img2, img2, getStructuringElement(MORPH_RECT, Size(8, 8)));
//finding the contours required
findContours(img2, Contours, hier, CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE, Point(0, 0));
//finding the contour of largest area and storing its index
for (int i = 0; i < Contours.size(); i++)
{
double a=contourArea(Contours[i]);
if (a> maxarea)
{
maxarea = a;
lrgctridx=i;
}
}
//convex hulls
vector<vector<Point> >hull(Contours.size());
for (int i = 0; i < Contours.size(); i++)
{
convexHull(Contours[i], hull[i], false);
}
//REQUIRED contour is detected,then draw a convex hull
if (maxarea!=0)
drawContours(frame, hull, lrgctridx, Scalar(255, 255, 255), 1, 8, vector<Vec4i>(), 0, Point());
imshow("output", frame);
char key = waitKey(33);
if (key == 27) break;
}
}
任何幫助將不勝感激。提前提前!
取代它在時間= T + 1假設有lrgctrdx沒有contours.The值爲0難道不仍然繪製一個contour.if即時更正唯一的事情,防止這是該地區!= 0部分?如果我刪除該行代碼似乎工作正常..我不明白 –
@SridharThiagarajan是的,你也應該重置'maxarea = 0'連同lrgctridx,所以你不會叫drawContours。或者只是'如果(!Contours.empty()){drawContours ...' – Miki
水晶般清晰的解釋..感謝隊友:) –