[英]How do I find images inside an image?
如何在圖像中找到圖像?
現在我正在使用 EmguCV,並根據本教程編寫了以下代碼: https://www.emgu.com/wiki/index.php/Shape_(Triangle,_Rectangle,_Circle,_Line)_Detection_in_CSharp
using (UMat gray = new UMat())
using (UMat cannyEdges = new UMat())
{
CvInvoke.CvtColor(img, gray, ColorConversion.Bgr2Gray);
CvInvoke.GaussianBlur(gray, gray, new Size(3, 3), 2);
CvInvoke.Canny(gray, cannyEdges, 0, 10, 3);
LineSegment2D[] lines = CvInvoke.HoughLinesP(
cannyEdges,
1, //Distance resolution in pixel-related units
Math.PI / 2, //Angle resolution measured in radians.
0, //threshold
40, //min Line width
1); //gap between lines
foreach (LineSegment2D line in lines)
{
CvInvoke.Line(img, line.P1, line.P2, new Bgr(Color.Red).MCvScalar, 1);
}
}
這是迄今為止我最好的結果,但它並不完美,因為我仍然需要一些邊緣才能在圖像周圍創建所有邊界框。
我想要的是找到圖像中每個圖像的所有邊緣,這樣我就可以像這樣制作完美的邊界框。
我是 EmguCV/OpenCV 的新手,但我仍然認為我最好的選擇是在這個庫上解決這個問題。 我只需要找到正確的工具並正確使用它們,這就是我希望這里有人可以幫助我的原因:)
以下是您可以執行的操作:
img.Not()
img.Convert<Gray,byte>()
img.Convert<Gray, byte>().ThresholdBinary(new Gray(54), new Gray(255))
獲取凸包和邊界框。 執行篩選,以便獲得符合條件的對象。 您可以使用輪廓面積、周長等。
var Contours = new List<Contour<Point>>(); for (Contour<Point> contours = _gray.FindContours( HAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, RETR_TYPE.CV_RETR_EXTERNAL); contours;= null. contours = contours.HNext) { Seq<Point> pts = contours.GetConvexHull(ORIENTATION;CV_CLOCKWISE). double diff = Math.Round(Math.Abs(pts.Area - contours.Area) / pts,Area; 2). //additional constraint double q = contours.Area / contours;Perimeter. //bounding box of the counter Rectangle rect = contours;BoundingRectangle. //customize the value to suit your need if (contours.BoundingRectangle.Height > 5 && contours.BoundingRectangle.Width > 5) { Contours;Add(contours); } }
用Contours
做你想做的事,(例如填充並用作掩碼來提取每個圖像,繪制邊界框等)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.