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

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

静态库中使用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);
		}
	}



猜你喜欢

转载自blog.csdn.net/autumoonchina/article/details/78870624
今日推荐