sinat_14944843
等级
结帖率 0%
void CAssimilation::DeleteObject(float **Data_AssHigh, int height, int width,int *MASK) { for (int X_pixel = 0; X_pixel <height; X_pixel++) { for (int Y_pixel = 0; Y_pixel < width; Y_pixel++) { if (Data_AssHigh[0][X_pixel*width + Y_pixel] > 0) { MASK[X_pixel*width + Y_pixel] = 255; //cout << MASK[X_pixel*width + Y_pixel]; } } } Mat SrcImage1(height, width, CV_8UC1); uchar *ptmp = NULL; for (int i = 0; i <height; ++i) { ptmp = SrcImage1.ptr<uchar>(i); for (int j = 0; j < width; ++j) { ptmp[j] = MASK[i*width + j]; } } //cv::Mat SrcImage1 = cv::Mat(height, width, CV_8UC1, MASK);容易出问题,导致转换出现问题,采用上面的方式进行转换; Mat thresholdImage; cv::threshold(SrcImage1, thresholdImage, 0, 255, CV_THRESH_OTSU + CV_THRESH_BINARY); vector< vector< Point> > contours; //用于保存所有轮廓信息 vector< vector< Point> > contours2; //用于保存面积不足100的轮廓 //vector<Point> tempV; //暂存的轮廓 cv::findContours(thresholdImage, contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE); //轮廓按照面积大小进行升序排序 sort(contours.begin(), contours.end(), ascendSort);//升序排序 vector<vector<Point> >::iterator itc = contours.begin(); int i = 0; while (itc != contours.end()) { //获得轮廓的矩形边界 Rect rect = boundingRect(*itc); int x = rect.x; int y = rect.y; int w = rect.width; int h = rect.height; //绘制轮廓的矩形边界 cv::rectangle(SrcImage1, rect, { 0, 0, 255 }, 1); if (itc->size() < 100) { contours2.push_back(*itc); //删除轮廓面积不足100的区域,即用黑色填充轮廓面积不足100的区域: cv::drawContours(SrcImage1, contours2, -1, Scalar(0, 0, 0), CV_FILLED); } ++itc; } for (int i_0 = 0; i_0 <height; ++i_0) { for (int j_0 = 0; j_0 < width; ++j_0) { //if (SrcImage1.at<uchar>(i_0, j_0)==255) //{ MASK[i_0*width + j_0] = SrcImage1.at<uchar>(i_0, j_0); //int a = MASK[i_0*width + j_0]; //} } } //释放内存; vector<vector< Point> >().swap(contours); vector<vector< Point> >().swap(contours2); } 各位,小弟自定义了一个剔除面积小的区域的函数,但是经过多次调用之后会出现处:有未经处理的异常: 0xC0000374: 堆已损坏。 (参数: 0x00007FFD3C0C97B0)。经过初步定为是 vector< vector< Point> > contours; //用于保存所有轮廓信息 vector< vector< Point> > contours2; //用于保存面积不足100的轮廓 的问题,该怎么处理为好?
2018-06-08 15:25:08
点赞 只看楼主 引用 举报 楼主
赵4老师
等级
勋章
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止。
2018-06-08 16:36:34
#1得分 0
robertbo
等级
首先,cv::drawContours(SrcImage1, contours2, -1, Scalar(0, 0, 0), CV_FILLED);你这句应该放在while (itc != contours.end())的外部,详细了解一下drawContours这个函数吧。至于报错,没有调试你的代码不确定具体位置,建议你自己debug一下,准确定位报错的代码位置后,再修改。
2018-06-11 18:04:29
#2得分 0
天下第二1992
等级
hello
2019-12-09 17:09:39
#3得分 0
libralibra
等级
勋章
大概看了下,你是按照轮廓的矩形区域,也就是boundingbox来判断,这个不推荐, 既然是去除小区域,还是按照区域自己的属性来比较好,有可能一个弯曲细长的边界返回特别大的boundingbox,导致你的结果不准确 要实现累死matlab的regionprops()函数一样的功能,opencv的findContours的参数需要调整如下:
C/C++ code?
1
2
3
4
dst.convertTo(dst,CV_8U);
cv::vector<cv::vector<cv::Point> > contours;
cv::vector<cv::Vec4i> hierarchy;
cv::findContours(dst,contours,hierarchy,CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
然后针对每一个contour,可以获取其内部封闭区域的面积:
C/C++ code?
1
double size = cv::contourArea(contours[i]);