Search code examples
c#opencvresizeaffinetransform

resize rectangles using cvTransform and cvGetAffineTransform openCv


iam dealing with multiscale images , 3 sizes : original , half & double i extract the connected components from each of them separately then do some computations

finally i want to map the bounding rectangles of the half & double scale connected components to their original location and size in the original image

here's my code of resizing and relocating the bounding rectangle

        Matrix<int> src = new Matrix<int>(3, 2,3);
        Matrix<int> dst = new Matrix<int>(3, 2,2);

        IntPtr mat = CvInvoke.cvCreateMat(2, 3, MAT_DEPTH.CV_32F);

        src[0,0]=componentBRect.X;
        src[0,1]=componentBRect.Y ;
        src[1,0]=componentBRect.Right;
        src[1,1]=componentBRect.Y;
        src[2,0]=componentBRect.X;
        src[2,1]=componentBRect.Bottom;





        CvInvoke.cvGetAffineTransform(img2, img, mat); //img is the original image & img2 has been resized to 1/2 imgWidth and 1/2 imgHeight

        CvInvoke.cvTransform(src.Ptr, dst.Ptr, mat, IntPtr.Zero);

it's supposed to get dst points reszied and relocated but unfortunately the same points are returned again , i think there's a problem with getAffineTransform as it returns an identity matrix

 mat= 
 |1 0 0 |
 |0 1 0 | !!

Any Suggestions ?? or is there another way to do what i want without using Affine transformation

Thanks in advance


Solution

  • I have solved it,

    I simply found another function - it's cvConvertScale

           for (int i = 0; i < Brectangles.Count; i++) {
    
                src[0, 0] = Brectangles[i].X;    //top left
                src[0, 1] = Brectangles[i].Y;
                src[1, 0] = Brectangles[i].Right;  //top right
                src[1, 1] = Brectangles[i].Y;
                src[2, 0] = Brectangles[i].X;    //bottom left
                src[2, 1] = Brectangles[i].Bottom;
    
    
                CvInvoke.cvConvertScale(src, dst, rescaleFactor, 0); // rescaleFactor variable is 2 for half size & 0.5 for double size
    
                temp.X = dst[0, 0];
                temp.Y = dst[0, 1];
                temp.Width = dst[1, 0] - dst[0, 0];
                temp.Height = dst[2, 1] - dst[0, 1];
    
                Brectangles[i] = temp;
    
    
             }
    

    It resizes the rectangle and relocates it in its proper position is the new size