台灣最大程式設計社群網站
線上人數
1893
 
會員總數:241182
討論主題:186829
歡迎您免費加入會員
討論區列表 >> C# >> 利用emgucv找長方形
[ 變換順序 ]  
[我要回覆]
回應主題 加入我的關注話題 檢舉此篇討論 將提問者加入個人黑名單
利用emgucv找長方形
價值 : 20 QP  點閱數:345 回應數:0

樓主

ChienWei Kao
門外漢
0 8
88 9
發送站內信

各位好,小弟目前使用EMGUCV在做影像處理的東西
目前把相片丟進去後進行找canny邊跡偵測後
要找出矩形時發現問題,參照emgu提供的範例找了好久一值不知道出錯在哪
麻煩大家幫忙一下,感激不盡><

我影像解析度維1600*896但是他在canny尋的時候看起來斷斷續續,放大看後是一直線,但無法找出邊跡
我主要目的為:找出我1600*896解析度裡的長方形,並畫出此長方形找出它的Center
目前卡在:邊跡偵測完成後,找部到長方形,畫不出來,個人猜想是解析度問題?
在麻煩高手們了><謝謝

StringBuilder msgBuilder = new StringBuilder("Performance: ");

Image<Bgr, Byte> img = new Image<Bgr, byte>(imageBox1.Image.Bitmap);
// Image<Bgr, Byte> img =new Image<Bgr, byte>(imageBox1.Image.Bitmap).Resize(400, 300, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR, true);在這裡如使用400*300可以找出邊界,並畫出長方形,但是我影像的解析度維1600*896,所以必須使用原影像解析來做處理[/size]

Image<Gray, Byte> gray = img.Convert<Gray, Byte>().PyrDown().PyrUp();
Stopwatch watch = Stopwatch.StartNew();
double cannyThreshold = 50;
double circleAccumulatorThreshold = 120;
msgBuilder.Append(String.Format("Hough circles - {0} ms; ", watch.ElapsedMilliseconds));

watch.Reset(); watch.Start();
double cannyThresholdLinking = 1;
Image<Gray, Byte> cannyEdges = gray.Canny(cannyThreshold, cannyThresholdLinking);
LineSegment2D[] lines = cannyEdges.HoughLinesBinary(
1, //Distance resolution in pixel-related units
Math.PI / 45.0, //Angle resolution measured in radians.
10, //threshold
30, //min Line width
10 //gap between lines
)[0]; //Get the lines from the first channel
watch.Stop();
msgBuilder.Append(String.Format("Canny & Hough lines - {0} ms; ", watch.ElapsedMilliseconds));
watch.Reset(); watch.Start();
List<Triangle2DF> triangleList = new List<Triangle2DF>();
List<MCvBox2D> boxList = new List<MCvBox2D>(); //a box is a rotated rectangle
using (MemStorage storage = new MemStorage()) //allocate storage for contour approximation
for (
Contour<Point> contours = cannyEdges.FindContours(
Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE,
Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_LIST,
storage);
contours != null;
contours = contours.HNext)
{
Contour<Point> currentContour = contours.ApproxPoly(contours.Perimeter * 0.05, storage);

if (currentContour.Area > 150) //only consider contours with area greater than 250
{
if (currentContour.Total == 3) //The contour has 3 vertices, it is a triangle
{
Point[] pts = currentContour.ToArray();
triangleList.Add(new Triangle2DF(
pts[0],
pts[1],
pts[2]
));
}
else if (currentContour.Total == 4) //The contour has 4 vertices.
{
bool isRectangle = true;
Point[] pts = currentContour.ToArray();
LineSegment2D[] edges = PointCollection.PolyLine(pts, true);

for (int i = 0; i < edges.Length; i++)
{
double angle = Math.Abs(
edges[(i + 1) % edges.Length].GetExteriorAngleDegree(edges[i]));
if (angle < 80 || angle > 100)
{
isRectangle = false;
break;
}
}
if (isRectangle) boxList.Add(currentContour.GetMinAreaRect());
}
}
}
watch.Stop();
msgBuilder.Append(String.Format("Triangles & Rectangles - {0} ms; ", watch.ElapsedMilliseconds));
// imageBox1.Image = img;
Image<Bgr, Byte> triangleRectangleImage = img.CopyBlank();
foreach (Triangle2DF triangle in triangleList)
triangleRectangleImage.Draw(triangle, new Bgr(Color.DarkBlue), 2);
foreach (MCvBox2D box in boxList)
triangleRectangleImage.Draw(box, new Bgr(Color.DarkOrange), 2);
imageBox1.Image = triangleRectangleImage;
// imageBox1.Image = cannyEdges;



搜尋相關Tags的文章: [ EMGUCV 找矩形 ] ,
本篇文章發表於2017-07-08 17:49
別忘捐VP感謝幫助你的人 新手會員瞧一瞧
目前尚無任何回覆
[ 變換順序 ]   

回覆
如要回應,請先登入.