Search code examples
c#opencvimage-processingsignal-processingemgucv

How to align ROI bounding rectangle to main image coordinate


I'am decoding a QR code from an image, since the image is large and have other unwanted graphics I'm drawing an roi around the code and decoding it. Everything works, but when i try to draw a bounding box around the QR code, it is drawn somewhere else. How can I align the rectangle in the same area of the main image?

This is what im getting - The red rectangle is the roi, the green one is the bounding box.

enter image description here

Here is the code :

     Image<Gray, byte> Gray_Image = My_Image.Convert<Gray, byte>();
    Gray_Image.ROI = Coderect;
         Gray_Image._Not();

        CvInvoke.cvThreshold(Gray_Image,Gray_Image,50,255.0,Emgu.CV.CvEnum.THRESH.CV_THRESH_BINARY);

        Gray_Image.Dilate(5);

        

        StructuringElementEx element = new StructuringElementEx(3, 3, 1, 1, Emgu.CV.CvEnum.CV_ELEMENT_SHAPE.CV_SHAPE_RECT);
        CvInvoke.cvMorphologyEx(Gray_Image, Gray_Image, IntPtr.Zero, element, Emgu.CV.CvEnum.CV_MORPH_OP.CV_MOP_CLOSE, 18);

        var contour = Gray_Image.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_EXTERNAL);
      

        CvInvoke.cvRectangle(My_Image, new Point(contour.BoundingRectangle.X, contour.BoundingRectangle.Y), new Point(contour.BoundingRectangle.X + contour.BoundingRectangle.Width, contour.BoundingRectangle.Y + contour.BoundingRectangle.Height), new MCvScalar(0, 255, 0), 4, Emgu.CV.CvEnum.LINE_TYPE.EIGHT_CONNECTED, 0);
    


        pictureBox1.Image = My_Image.Bitmap;

Solution

  • I found the solution. Since I know the (roiX,roiY) of the roi and i also know the (bbX,bbY,bbW,bbH) of the bounding box. By adding the X of roi and X of bounding box i got the X of the location, similarly for Y.

    I simply had to do the following :

        cvInvoke.cvRectangle(My_Image, new Point(roiX+bbX, roiY+bbY), new Point((roiX+bbX)+bbW,(roiY+bbY)+bbH), new McvScalar(0,255,0), Line_Type.Eight_Connected,0);