我遇到了Canny函数的问题 . 它在我的框架周围生成线条 . 因此,当我之后使用我的FindContour函数时,它会在我的框架周围绘制轮廓 . 我想看看我最大的轮廓,这应该是我的对象,但在这种情况下我的框架 . 当Canny函数清楚地显示一个形状时,该函数最有可能在我的框架周围绘制一个大轮廓 .
如果已经看到有办法通过转换图像来避免这个问题 . 当我这样做时,我得到一个错误 . 这是我的代码 .
private void ProcessFrame(object sender, EventArgs e)
{
if (_capture != null && _capture.Ptr != IntPtr.Zero)
{
_capture.Retrieve(imgOriginal, 0);
CvInvoke.CvtColor(imgOriginal, imgHSV, ColorConversion.Bgr2Hsv); //Convert the captured frame from BGR to HSV
CvInvoke.InRange(imgHSV, new ScalarArray(new MCvScalar(iLowH, iLowS, iLowV)), new ScalarArray(new MCvScalar(iHighH, iHighS, iHighV)), imgThres);
CvInvoke.Erode(imgThres, imgThres, CvInvoke.GetStructuringElement(ElementShape.Ellipse, new Size(5, 5), new Point(-1, -1)), new Point(-1, -1), 3, BorderType.Constant, new MCvScalar(255, 255, 255));
CvInvoke.Dilate(imgThres, imgThres, CvInvoke.GetStructuringElement(ElementShape.Ellipse, new Size(5, 5), new Point(-1, -1)), new Point(-1, -1), 3, BorderType.Constant, new MCvScalar(255, 255, 255));
CvInvoke.Canny(imgThres, imgCanny, 100, 200, 3);
CvInvoke.Invert(imgCanny, imgCanny, DecompMethod.Svd);
FindLargestContour(imgCanny, imgContour);
pictureBox1.Image = imgOriginal.Bitmap;
pictureBox2.Image = imgCanny.Bitmap;
}
}
private void FindLargestContour(IInputOutputArray imgCanny, IInputOutputArray imgContour)
{
int largest_contour_index = 0;
double largest_area = 0;
VectorOfPoint largestContour;
using (Mat hierachy = new Mat())
using (VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint())
{
IOutputArray hierarchy;
CvInvoke.FindContours(imgCanny, contours, hierachy, RetrType.Tree, ChainApproxMethod.ChainApproxSimple);
for (int i = 0; i < contours.Size; i++)
{
MCvScalar color = new MCvScalar(0, 0, 255);
double a = CvInvoke.ContourArea(contours[i], false); // Find the area of contour
if (a > largest_area)
{
largest_area = a;
largest_contour_index = i; //Store the index of largest contour
}
CvInvoke.DrawContours(imgContour, contours, largest_contour_index, new MCvScalar(255, 0, 0));
}
CvInvoke.DrawContours(imgContour, contours, largest_contour_index, new MCvScalar(0, 0, 255), 5, LineType.EightConnected, hierachy);
largestContour = new VectorOfPoint(contours[largest_contour_index].ToArray());
}
}
取消注释反转功能时的错误如下 .
CvException: OpenCV: type == CV_32F || type == CV_64F
我曾经把我的代码放在c中,并没有给出在框架周围绘制轮廓的问题 . 也许我应该为Canny函数使用不同的阈值?另一个解决方案是找到我的框架区域,然后从比较中取出该区域吗?
谢谢!
1 回答
可能不是最花哨的解决方法,但我编辑了查看轮廓最大区域的if循环 .
所以来自
我去了
这样,代码不应该保存尺寸几乎与整个框架一样大的轮廓 .