在另一个图像中寻找图像的形状

时间:2020-08-01 16:25:47

标签: c# opencv computer-vision emgucv

我正在尝试确定在图像中找到形状位置的最佳方法。 我有一个带有白色轮廓的拼图块的图像。

我需要以某种方式在另一张给定的图像中找到相同的白色轮廓,并能够输出边界框的位置。

什么是最好的方法? OpenCV,emgucv,ML?

例如一块拼图 https://imgur.com/a/PJJ6ixL

谢谢

1 个答案:

答案 0 :(得分:0)

对于我的回答,我假设您要检测的其他部分将与给出的示例部分具有完全相同的白色轮廓。我只是在emgucv中使用了“寻找轮廓”方法来获取拼图的轮廓。

//Read in the image
Mat puzzlePiece = CvInvoke.Imread("Puzzle\Piece\path", ImreadModes.AnyColor);

//Create a grayscale version of the image
Mat puzzlePieceGray = new Mat();
CvInvoke.CvtColor(puzzlePiece, puzzlePieceGray, ColorConversion.Bgr2Gray);

//Black image to draw contours on
Image<Bgr, byte> output = new Image<Bgr, byte>(puzzlePiece.Width, puzzlePiece.Height, 
    new Bgr(Color.Black));

VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();

//Find and draw the found contours
CvInvoke.FindContours(puzzlePieceGray, contours, null, RetrType.External, 
    ChainApproxMethod.ChainApproxSimple);
CvInvoke.DrawContours(output, contours, -1, new MCvScalar(255, 0, 0));

//Display the found contours
CvInvoke.Imshow("Contours Drawn", output);
CvInvoke.WaitKey(0);

原始图像

enter image description here

具有轮廓的图像

enter image description here

由于仅提供了输入图像,因此我无法将轮廓的大小与其他片段进行比较,但这是一些示例代码,说明了其工作方式。

if (contours[0].Size == contoursTwo[0].Size)
{
    //do stuff
}
相关问题