3

I have an image from which I want to get a vertical ROI, apply some transformations and add to another image.

I read a lot of questions and answer on StackOverflow and other forums, but I'm still stuck with this problem. For the moment I'm using the C interface of OpenCV, but I could use the C++ one if needed (I would have to write a conversion function, since I'm working with CGImageRef in Cocoa).

To get from the top image (see below) to the bottom image, I guess I have to :

  • Get the ROI on the first image ;
  • Scale it down ;
  • Get the intersection points on the lines between the center and the 2 circles for my "width" angle (the angle is fixed) ;
  • Distort the image so the corners stick to my intersection points ;
  • Rotate around the center point and put it in the output image.

ROI rotate and distort

For the moment, I manage well to do this :

  • Getting the ROI ;
  • Scaling it with cvResize ;
  • Getting the intersection points shouldn't be too complicated, as it is pure geometry and I implemented it yet for another purpose.

But, I have no idea at all of how to distort the resulting image of my ROI, and I don't know if it is even possible in OpenCV. Would I have to use a kind of perspective correction ?

And, I've been trying the few good posts solutions I found by here to rotate with the rotated bounding box, but with no good results for the moment.

EDIT :

Well, I managed to do the first part of the work :

  • Getting a ROI in a basis image ;
  • Rotating and placing it at a fixed distance from the center.

Circle SlitScan

I used the method explained and coded in this post : https://stackoverflow.com/a/16285286/1060921

I only added a variable to set the rotation point and get my inner circle.

NB : I set the ROI BEFORE to call the method, so the ROI in the post method is... the image size. Then I place it at the center of my final image with a cvAdd.

Here I get one pixel slices of my camera input. What I want to do now is to distort bigger slices, for example from 2 pixels on the inner circle to 5 pixels on the outer one.

Community
  • 1
  • 1
Benoît Lahoz
  • 1,270
  • 1
  • 18
  • 43

1 Answers1

1

See this tutorial which uses warpPerspective to correct perspective distortion.

EDIT: In your case warpAffine should be better and simpler solution.

So, you could do something like this, just use four points instead of three:

Point2f srcTri[3];
Point2f dstTri[3];

Mat rot_mat( 2, 3, CV_32FC1 );
Mat warp_mat( 2, 3, CV_32FC1 );
Mat src, warp_dst, warp_rotate_dst;

/// Load the image
src = imread( ... );

/// Set the dst image the same type and size as src
warp_dst = Mat::zeros( src.rows, src.cols, src.type() );

/// Set your 3 points to calculate the  Affine Transform
srcTri[0] = Point2f( 0,0 );
srcTri[1] = Point2f( src.cols - 1, 0 );
srcTri[2] = Point2f( 0, src.rows - 1 );

dstTri[0] = Point2f( src.cols*0.0, src.rows*0.33 );
dstTri[1] = Point2f( src.cols*0.85, src.rows*0.25 );
dstTri[2] = Point2f( src.cols*0.15, src.rows*0.7 );

/// Get the Affine Transform
warp_mat = getAffineTransform( srcTri, dstTri );

/// Apply the Affine Transform just found to the src image
warpAffine( src, warp_dst, warp_mat, warp_dst.size() );
Simon G.
  • 370
  • 1
  • 8