|
做了一个识别图片中白斑的小程序,图片是从工业相机实时抓取的,当连续运行一段时间后显示画面锁死,用onsole.WriteLine()测试是卡在CvInvoke.Canny中没有返回??发现不了问题,图像,请大家帮忙想想
private bool IsTubleExist(Image<Rgb, byte> image)
{
Console.WriteLine("IsTubleExist 1");
image.ROI = this.RectangelGetColor;
using (Image<Gray, byte> imgtmp = new Image<Gray, byte>(this.RectangelGetColor.Size))
{
CvInvoke.CvtColor(image, imgtmp, Emgu.CV.CvEnum.ColorConversion.Rgb2Gray);
image.ROI = System.Drawing.Rectangle.Empty;
imgtmp.SmoothGaussian(5);
CvInvoke.Threshold(imgtmp, imgtmp, 25, 255, Emgu.CV.CvEnum.ThresholdType.Binary);
Console.WriteLine("IsTubleExist 2");
//CvInvoke.AdaptiveThreshold(imgtmp, imgtmp, 255, Emgu.CV.CvEnum.AdaptiveThresholdType.GaussianC, Emgu.CV.CvEnum.ThresholdType.Binary, 3, 0);
//Stopwatch watch = Stopwatch.StartNew();
double cannyThreshold = 180.0;
//double circleAccumulatorThreshold = 120;
//CircleF[] circles = CvInvoke.HoughCircles(imgtmp, HoughType.Gradient, 2.0, 20.0, cannyThreshold, circleAccumulatorThreshold, 5);
double cannyThresholdLinking = 20.0;
using (UMat cannyEdges = new UMat())
{
Console.WriteLine("IsTubleExist 3 :"+imgtmp.Bytes.GetLength(0));
try
{
CvInvoke.Canny(imgtmp, cannyEdges, cannyThreshold, cannyThresholdLinking);
}
catch(Exception e)
{
}
//List<Triangle2DF> triangleList = new List<Triangle2DF>();
//List<RotatedRect> boxList = new List<RotatedRect>(); //a box is a rotated rectangle
Console.WriteLine("IsTubleExist 4 :"+cannyEdges.Bytes.GetLength(0));
using (Emgu.CV.Util.VectorOfVectorOfPoint contours = new Emgu.CV.Util.VectorOfVectorOfPoint())
{
try
{
//CvInvoke.FindContours(cannyEdges, contours, null, RetrType.List,RetrType.External, ChainApproxMethod.ChainApproxSimple);
CvInvoke.FindContours(cannyEdges, contours, null, RetrType.External, ChainApproxMethod.ChainApproxSimple);
}
catch (Exception e)
{
}
Console.WriteLine("IsTubleExist 5");
int count = contours.Size;
for (int i = 0; i < count; i++)
{
using (Emgu.CV.Util.VectorOfPoint contour = contours[i])
{
double area = CvInvoke.ContourArea(contour, false);
if ((area > 300) && (area < 1500))
{
return false;
}
}
}
}
}
}
return true;
}
|
|