
|
How to use cvTransform?
It seems I am missing some basics, however, I haven't found any info on the net.
I have an affine transformation matrix which I am going to use to transform images with cvWarpAffine.
I have an image to warp, and I need to esimate the resulting image size in order to correctly allocate the memory.
My method is the following:
Estimate new coordinates for the corners and determine the surrounding rectangle.
I have created the matrix with the image corners' coordinates
/0 , 0\
|w, 0|
|0, h|
\w, h/
where w and h are the width and height of the source image.
Then I was going to feed it to cvTransform, find a maximum and minimum values for each coordinate and estimate new width and height.
However, cvTransform was returning errors.
The failed C++ code is below
IplImage *src=cvLoadImage("images/2/1.JPG");
// create initial matrix.
CvMat *corners=cvCreateMat(4,2,CV_64FC1);
// fill it
cvmSet(corners,0,0,0.); cvmSet(corners,0,1,0.); //(0,0)
cvmSet(corners,1,0,double(src->width)); cvmSet(corners,1,1,0.); //(w,0)
cvmSet(corners,2,0,0.); cvmSet(corners,2,1,double(src->height)); //(0,h)
cvmSet(corners,3,0,double(src->width)); svmSet(corners,3,1,double(src->height)); //(w,h)
CvMat *corners_tr=cvCreateMat(4,2,CV_64FC1);
// and calculate the transformation
cvTransform(corners,corners_tr,res);
// then find min and max
int minh=cvmGet(corners_tr,0,0);
int maxh=minh,minw=minh,maxw=minh;
for(i=0;i<4;i++){
int c=cvmGet(corners_tr,0,i);
if(maxw < c) maxw=c;
if(minw > c) minw=c;
c=cvmGet(corners_tr,1,i);
if(maxh < c) maxh=c;
if(minh > c) minh=c;
}
// and calculate dst_size
CvSize dst_size=cvSize(maxw-minw,maxh-minh);
I tried transposing the matrix 'corners' or adding a new row with 1s - no success.
|

|
Re: How to use cvTransform?
Vladimir Eremeev wrote:
It seems I am missing some basics, however, I haven't found any info on the net.
I have an affine transformation matrix which I am going to use to transform images with cvWarpAffine.
I have an image to warp, and I need to esimate the resulting image size in order to correctly allocate the memory.
My method is the following:
Estimate new coordinates for the corners and determine the surrounding rectangle.
I have created the matrix with the image corners' coordinates
/0 , 0\
|w, 0|
|0, h|
\w, h/
where w and h are the width and height of the source image.
Then I was going to feed it to cvTransform, find a maximum and minimum values for each coordinate and estimate new width and height.
However, cvTransform was returning errors.
The failed C++ code is below
IplImage *src=cvLoadImage("images/2/1.JPG");
// create initial matrix.
CvMat *corners=cvCreateMat(4,2,CV_64FC1);
// fill it
cvmSet(corners,0,0,0.); cvmSet(corners,0,1,0.); //(0,0)
cvmSet(corners,1,0,double(src->width)); cvmSet(corners,1,1,0.); //(w,0)
cvmSet(corners,2,0,0.); cvmSet(corners,2,1,double(src->height)); //(0,h)
cvmSet(corners,3,0,double(src->width)); svmSet(corners,3,1,double(src->height)); //(w,h)
CvMat *corners_tr=cvCreateMat(4,2,CV_64FC1);
// and calculate the transformation
cvTransform(corners,corners_tr,res);
// then find min and max
int minh=cvmGet(corners_tr,0,0);
int maxh=minh,minw=minh,maxw=minh;
for(i=0;i<4;i++){
int c=cvmGet(corners_tr,0,i);
if(maxw < c) maxw=c;
if(minw > c) minw=c;
c=cvmGet(corners_tr,1,i);
if(maxh < c) maxh=c;
if(minh > c) minh=c;
}
// and calculate dst_size
CvSize dst_size=cvSize(maxw-minw,maxh-minh);
I tried transposing the matrix 'corners' or adding a new row with 1s - no success.
I think ur roblom is that u r using 4x2 vector where as u should be using 4x1 3-channel vector with (x,y,1) cordinates :)
I ve used this function (for perspective transformation using a homography to map form one image to another) but for me it gives unexpected values. and when i used cvPerspectiveTransform() it all the transformed cordinates as 0,0.
i've tried to manually transform one point using cvGEMM() function acording to the equation p' = H x p and it works fine but when i give the data to this function it gives wrong cordinates..... is there any idea about it? 
|

|
Re: How to use cvTransform?
I've been confronted with this exact same problem and finally found a solution:
Use cvProjectiveTransforms()/cvTransform() (I've only tried this with the first function, but i guess it should olso work in the second one) and instead of providing a 3 channel array of source points as documented, use a 2 channel array with just the x and y coordinates.
shanaka wrote:
Vladimir Eremeev wrote:
It seems I am missing some basics, however, I haven't found any info on the net.
I have an affine transformation matrix which I am going to use to transform images with cvWarpAffine.
I have an image to warp, and I need to esimate the resulting image size in order to correctly allocate the memory.
My method is the following:
Estimate new coordinates for the corners and determine the surrounding rectangle.
I have created the matrix with the image corners' coordinates
/0 , 0\
|w, 0|
|0, h|
\w, h/
where w and h are the width and height of the source image.
Then I was going to feed it to cvTransform, find a maximum and minimum values for each coordinate and estimate new width and height.
However, cvTransform was returning errors.
The failed C++ code is below
IplImage *src=cvLoadImage("images/2/1.JPG");
// create initial matrix.
CvMat *corners=cvCreateMat(4,2,CV_64FC1);
// fill it
cvmSet(corners,0,0,0.); cvmSet(corners,0,1,0.); //(0,0)
cvmSet(corners,1,0,double(src->width)); cvmSet(corners,1,1,0.); //(w,0)
cvmSet(corners,2,0,0.); cvmSet(corners,2,1,double(src->height)); //(0,h)
cvmSet(corners,3,0,double(src->width)); svmSet(corners,3,1,double(src->height)); //(w,h)
CvMat *corners_tr=cvCreateMat(4,2,CV_64FC1);
// and calculate the transformation
cvTransform(corners,corners_tr,res);
// then find min and max
int minh=cvmGet(corners_tr,0,0);
int maxh=minh,minw=minh,maxw=minh;
for(i=0;i<4;i++){
int c=cvmGet(corners_tr,0,i);
if(maxw < c) maxw=c;
if(minw > c) minw=c;
c=cvmGet(corners_tr,1,i);
if(maxh < c) maxh=c;
if(minh > c) minh=c;
}
// and calculate dst_size
CvSize dst_size=cvSize(maxw-minw,maxh-minh);
I tried transposing the matrix 'corners' or adding a new row with 1s - no success.
I think ur roblom is that u r using 4x2 vector where as u should be using 4x1 3-channel vector with (x,y,1) cordinates :)
I ve used this function (for perspective transformation using a homography to map form one image to another) but for me it gives unexpected values. and when i used cvPerspectiveTransform() it all the transformed cordinates as 0,0.
i've tried to manually transform one point using cvGEMM() function acording to the equation p' = H x p and it works fine but when i give the data to this function it gives wrong cordinates..... is there any idea about it? 
|