2017-09-14 107 views

回答

0

canny顯示圖像中的所有邊緣,而不是外部輪廓。這是一種(但不是唯一的)做對象檢測(前景/背景提取)的方法。 以下片段通過使用canny邊緣檢測器提取對象roi(Min area Rect)。從otsu閾值開始的canny thresh參數的carrefull。

希望它有幫助。 Detection result

Image<Gray,byte> imageFullSize = new Image<Gray, byte>(imagePath); 
Rectangle roi = new Rectangle(Your Top Left X,Your Top Left Y,Your ROI Width,Your ROI Height); 
//Now get a roi (No copy, it is a new header pointing original image 
Image<Gray,byte> image = imageFullSize.GetSubRect(roi); 
//Step 1 : contour detection using canny 
//Gaussian noise removal, eliminate background false détections 
image._SmoothGaussian(15); 
//Canny thresh based on otsu threshold value: 
Mat binary = new Mat(); 
double otsuThresh = CvInvoke.Threshold(image, binary, 0, 255, ThresholdType.Binary | ThresholdType.Otsu); 
double highThresh = otsuThresh; 
double lowThresh = otsuThresh * 0.5; 
var cannyImage = image.Canny(lowThresh, highThresh); 
//Step 2 : contour extraction 
Mat hierarchy=new Mat(); 
VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint(); 
CvInvoke.FindContours(cannyImage,contours,hierarchy,RetrType.External,ChainApproxMethod.ChainApproxSimple); 
//Step 3 : contour fusion 
List<Point> points=new List<Point>(); 
for(int i = 0; i < contours.Size; i++) 
{ 
    points.AddRange(contours[i].ToArray()); 
} 

//Step 4 : Rotated rect 
RotatedRect minAreaRect = CvInvoke.MinAreaRect(points.Select(pt=>new PointF(pt.X,pt.Y)).ToArray()); 
Point[] vertices = minAreaRect.GetVertices().Select(pt => new Point((int)pt.X, (int)pt.Y)).ToArray(); 
//Step 5 : draw result 
Image <Bgr,byte > colorImageFullSize =new Image<Bgr, byte>(imagePath); 
Image <Bgr,byte > colorImage =colorImageFullSize.GetSubRect(roi); 
colorImage.Draw(vertices,new Bgr(Color.Red),2); 
+0

感謝ü主席先生,這讓我先走。但是當我更換IC時,我遇到了問題。 –

+0

也請告訴我如何使用此代碼的ROI。謝謝 –

+0

謝謝你,先生,它幫了我很多。 –