How can I detect multiple occurrence of of an object in an Image

怎甘沉沦 提交于 2019-12-12 02:32:37

问题


Hi new to OpenCV and I am using JavaCV plugin in Unity to detect floorplan.

I want to detect the position of windows, doors and few other objects in the floorplan image. First I used template matching for detection, but later I figured out it wont work if object is placed in different orientation or size of object is mismatched. I am using the device camera to capture the image hence image may get a little skewed hence I abandoned this technique.

Following is the reference image and the highlighted portion shows the windows found using template matching.

Template detection output: Floor Plan Image

So now I am trying to use feature detection but I am not able to get the position of object in the image, also how can it be used for multiple occurrence of the same object.

Following is the Feature detection code template came with plugin.

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;}

It uses the drawMatched function to draw show the matched key points.

    Features2d.drawMatches (img1Mat, keypoints1, img2Mat, keypoints2, matches, resultImg);

Instead of this I need to draw rectangle around the matched region. How should I achieve this.

Output:

Youtube link: This is what I am exactly trying to achieve, but it will be on the captured image

来源:https://stackoverflow.com/questions/37255339/how-can-i-detect-multiple-occurrence-of-of-an-object-in-an-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!