Suppose I have 4 corner points : (x1, y1) ; (x2, y2) ;(x3, y3) ; (x4, y4) and a rectangular image size (m,n) How do I display the image such that the image when shown has its corners at the four mentioned points. In other words, four corners can control the angle at which the image is rotated (bear in mind the image edges might not be parallel) Thanks!
相关问题
- How to get the background from multiple images by
- Extract matrix elements using a vector of column i
- Try to load image with Highgui.imread (OpenCV + An
- CV2 Image Error: error: (-215:Assertion failed) !s
- How do I apply a perspective transform with more t
相关文章
- How do I append metadata to an image in Matlab?
- How can I write-protect the Matlab language?
- `std::sin` is wrong in the last bit
- Python open jp2 medical images - Scipy, glymur
- Escape sequence to display apostrophe in MATLAB
- On a 64 bit machine, can I safely operate on indiv
- Converting PIL Image to GTK Pixbuf
- Vertical line fit using polyfit
Assuming the image will not be altered, only 2 points are needed to compute the image rotation. The general case is something like
You need to warp the image for a generalized solution. You can do it as follows:
First, Read the image.
Specify the set of transformed points (in your case,
(x1,y1) ... (x4,y4)
), they arefixedPoints
.Then, estimate the transformation. I choose projective transformation. You can choose affine as well.
Since, you want the image to go to the specified corners, you have to specify the output view. It can be done by constructing a reference 2-D image as follows.
Finally, warp the image.
Show the image.
You should have the corners of your image at the specified points and the box which contains the image will be of the size of the original image.
Another approach very similar to the one proposed by @Parag, is to use the image transformation functions of MATLAB in a straightforward way.
Here is how: First you have to consider as if the image is within a 'unity' rectangle and define initial transformation conditions accordingly:
Note that the
fill_color
variable represents just the color to be used for filling the parts of the canvas that will not be covered by the transformed image. Then you apply a projective transformation from the original to the new rectangle representing the image canvas as follows:As you may notice the transformation is bicubic and returns both the out image (
out_im
) and the new coordinate system represented bydata
anddata
. If you just keep the output image then it will be of the same size as the input image in the original coordinate system (so it will be a little stretched). In order to display the images properly you might use the following command:Here is an example. Let's considered the case of Lena shown below.
After applying the transformation we may display using the correct coordinates as shown below.
If we decide to just display the output image without any reference to the new coordinate system we get the image shown below.
The output rectangle used in this example was:
[-1 -2;2 -1;3 3;-3 1]