您是OpenCV的新手,我在Unity中使用JavaCV插件来检测布局规划。
我想在平面图图像中检测窗户,门和其他一些物体的位置。首先我使用模板匹配进行检测,但后来我发现如果对象放置在不同的方向或对象的大小不匹配,它将无法工作。我正在使用设备相机拍摄图像,因此图像可能会有点偏斜,因此我放弃了这种技术。
以下是参考图像,突出显示的部分显示使用模板匹配找到的窗口。
模板检测输出:Floor Plan Image
所以现在我正在尝试使用特征检测,但是我无法获得图像中对象的位置,也无法将它用于同一对象的多次出现。
以下是插件附带的功能检测代码模板。
void Start (){
Texture2D imgTexture = Resources.Load ("lena") as Texture2D;
Mat img1Mat = new Mat (imgTexture.height, imgTexture.width, CvType.CV_8UC3);
Utils.texture2DToMat (imgTexture, img1Mat);
Debug.Log ("img1Mat dst ToString " + img1Mat.ToString ());
Mat img2Mat = new Mat (imgTexture.height, imgTexture.width, CvType.CV_8UC3);
Utils.texture2DToMat (imgTexture, img2Mat);
Debug.Log ("img2Mat dst ToString " + img2Mat.ToString ());
float angle = UnityEngine.Random.Range (0, 360), scale = 1.0f;
// Till this line is code is for unity image loading
Point center = new Point (img2Mat.cols () * 0.5f, img2Mat.rows () * 0.5f);
Mat affine_matrix = Imgproc.getRotationMatrix2D (center, angle, scale);
Imgproc.warpAffine (img1Mat, img2Mat, affine_matrix, img2Mat.size ());
FeatureDetector detector = FeatureDetector.create (FeatureDetector.ORB);
DescriptorExtractor extractor = DescriptorExtractor.create (DescriptorExtractor.ORB);
MatOfKeyPoint keypoints1 = new MatOfKeyPoint ();
Mat descriptors1 = new Mat ();
detector.detect (img1Mat, keypoints1);
extractor.compute (img1Mat, keypoints1, descriptors1);
MatOfKeyPoint keypoints2 = new MatOfKeyPoint ();
Mat descriptors2 = new Mat ();
detector.detect (img2Mat, keypoints2);
extractor.compute (img2Mat, keypoints2, descriptors2);
DescriptorMatcher matcher = DescriptorMatcher.create (DescriptorMatcher.BRUTEFORCE_HAMMINGLUT);
MatOfDMatch matches = new MatOfDMatch ();
matcher.match (descriptors1, descriptors2, matches);
Mat resultImg = new Mat ();
//What should be the alternate function to drawmatches to draw rectangle arround the matched pattern
Features2d.drawMatches (img1Mat, keypoints1, img2Mat, keypoints2, matches, resultImg);
//Ignore below portion its for unity rendering
Texture2D texture = new Texture2D (resultImg.cols (), resultImg.rows (), TextureFormat.RGBA32, false);
Utils.matToTexture2D (resultImg, texture);
gameObject.GetComponent<Renderer> ().material.mainTexture = texture;}
它使用drawMatched函数绘制显示匹配的关键点。
Features2d.drawMatches (img1Mat, keypoints1, img2Mat, keypoints2, matches, resultImg);
而不是这个我需要在匹配区域周围绘制矩形。我该怎么做到这一点。
输出:
Youtube链接: This is what I am exactly trying to achieve, but it will be on the captured image