opencv3.0 + VS2010中FindContours()函数崩溃问题

5 篇文章 0 订阅

老问题,至于解决方案网上有很多,在这里我直接贴出我自己解决问题的办法,并且通过测试,需要的同学可以直接拿来使用,参考。

静态库中使用MFC或者动态库中使用MFC都可以正常使用,建议使用动态MFC,防止崩溃。

 

	CString strFilePath = sdf.OpenFile();

	if (strFilePath.GetLength())
	{
		cv::Mat mSrc = imread(sdf.CString2Char(strFilePath), CV_LOAD_IMAGE_GRAYSCALE);

#ifdef _AFXDLL
		std::vector<std::vector<cv::Point>> contours;
		std::vector<cv::Vec4i> hierarchy;
#else
		std::vector<Mat> contours(10000);
		std::vector<Vec4i> hierarchy(10000);
#endif // _AFXDLL

		cv::findContours(mSrc, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

		
		for (int i = 0; i < contours.size(); ++i)
		{
			CString strContent;

#ifdef _AFXDLL
			for (int j = 0; j < contours[i].size(); ++j)
			{
				strContent += "x=" + sdf.Int2Cstring(contours[i][j].x) +  " y=" + sdf.Int2Cstring(contours[i][j].x) + " || ";
			}
#else
			//如果使用mat作为参数
			for (int n = 0; n < contours[i].rows; ++n)
			{
				uchar* data = contours[i].ptr<uchar>(n);
				for (int m = 0; m < contours[i].cols; ++m)
				{
					int da = data[m];
					strContent += sdf.Int2Cstring(da) +  " | ";
				}
			}
#endif // _AFXDLL

			cv::Rect rect = boundingRect(contours[i]);
			cv::Mat mCanvas = cv::Mat::zeros(cv::Size(mSrc.cols, mSrc.rows), CV_8UC3);
			cv::putText(mCanvas, sdf.CString2Char(strContent), cv::Point(rect.x, rect.y), FONT_HERSHEY_PLAIN, 1.0, cv::Scalar(0,255,0));

			drawContours(mCanvas, contours, i, Scalar(0,0,255), 1);
			imshow("contour", mCanvas);
			waitKey();
			
			MessageBox(strContent, "轮廓坐标!", MB_ICONINFORMATION);
		}
	}

更多资源,欢迎交流。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值