我正在使用iPhone5s进行黑色物体追踪:但经常会遇到
Thread 6:EXC_BAD_ACCESS(code 1, dress=0x8)
然后我的应用程序突然退出。谁能告诉我为什么会这样呢?
此错误发生在:
template<typename _Tp> inline
_Tp Rect_<_Tp>::area() const
{
return width * height; //Thread 6:EXC_BAD_ACCESS(code 1, dress=0x8)
}
//this method is in types.hp in latest opencv framework
我的彩色物体识别代码如下:
#pragma mark - Protocol CvVideoCameraDelegate
#ifdef __cplusplus
- (void)processImage:(cv::Mat &)image{
Mat imageCopy,imageCopy2;
cvtColor(image, imageCopy, COLOR_BGRA2BGR);
cvtColor(imageCopy, imageCopy2, COLOR_BGR2HSV);
//smooth the image
GaussianBlur(imageCopy2, imageCopy, cv::Size(5,5),0, 0);
cv::inRange(imageCopy, cv::Scalar(0,0,0,0), cv::Scalar(180,255,30,0),
imageCopy2);
/*****************************find the contour of the detected area abd draw it***********************************/
//2-D point to store countour
std::vector< std::vector<cv::Point>> contour1;
//do opening on the binary thresholded image
int erosionSize = 3;
Mat erodeElement =
getStructuringElement(cv::MORPH_ELLIPSE,cv::Size(2*erosionSize+1,2* erosionSize+1), cv::Point(erosionSize,erosionSize));
erode(imageCopy2, imageCopy2, erodeElement);
dilate(imageCopy2, imageCopy2, erodeElement);
//Acual line to find the contour
cv::findContours(imageCopy2, contour1, RETR_EXTERNAL, CHAIN_APPROX_NONE);
//set the color used to draw the conotour
Scalar color1 = Scalar(50,50,50);
//loop the contour to draw the contour
for(int i=0; i< contour1.size(); i++){
drawContours(image, contour1, i, color1);
}
/******END*****/
/****************************find the contour of the detected area abd draw it***********************************/
/****************************Appproximate the contour to polygon && get bounded Rectangle and Circle*************/
std::vector<std::vector<cv::Point>> contours_poly(contour1.size());
std::vector<cv::Rect> boundedRect(contour1.size());
std::vector<cv::Point2f> circleCenter(contour1.size());
std::vector<float> circleRadius(contour1.size());
for (int i=0; i< contour1.size(); i++){
approxPolyDP(Mat(contour1[i]), contours_poly[i], 3, true);
boundedRect[i] = boundingRect(Mat(contours_poly[i]));
minEnclosingCircle((Mat)contours_poly[i], circleCenter[i], circleRadius[i]);
}
/******END*******/
/*****************************draw the rectangle for detected area ***********************************************/
Scalar recColor = Scalar(121,200,60);
Scalar fontColor = Scalar(0,0,225);
//find the largest contour
int largestContourIndex=0;
for (int i=0; i<contour1.size(); i++){
if(boundedRect[i].area()> boundedRect[largestContourIndex].area())
largestContourIndex=i;
}
int j=largestContourIndex;
if(boundedRect[j].area()>40){
rectangle(image, boundedRect[j].tl(), boundedRect[j].br(), recColor);
//show text at tl corner
cv::Point fontPoint = boundedRect[j].tl();
putText(image, "Black", fontPoint, FONT_HERSHEY_COMPLEX, 3, fontColor);
}
// cvtColor(imageCopy, image, COLOR_HLS2BGR);
}
#endif
答案 0 :(得分:0)
最后找出原因: 正如@SolaWing所说,存在一些空指针。对于未来的观众,只是想让它更清晰:
问题出现在以下代码中:
if(boundedRect[j].area()>40){
rectangle(image, boundedRect[j].tl(), boundedRect[j].br(), recColor);
//show text at tl corner
cv::Point fontPoint = boundedRect[j].tl();
putText(image, "Black", fontPoint, FONT_HERSHEY_COMPLEX, 3, fontColor);
}
对于这段代码,它已经假设总是存在检测到的区域,但实际上,当手机摄像头前面没有目标区域时, contour.size()为零,话虽如此,对于std::vector<cv::Rect> boundedRect(contour1.size());
boundedRect是空指针,当我使用if(boundedRect[j].area()>40){}
时会出现问题,这是使用空指针的第一个指针。