I have a mini project to do about homography, but I am really a beginner in Matlab. I have calculated the homography matrix easily by taking a set pixels from both images but the problem is how to apply this matrix to all pixels of the original image (I think it's difficult to treat each pixel individually).

Here is the original image :

enter image description here

I want to apply the homography to this image (I have already extracted the homography matrix) to get a frontal view of the cover.

So with my weak knowledge of Matlab, I applied the following operations:

  • I get 4 points from the corners of the original image and the corresponding points in the output image
  • I have estimated the homography :

     0.9638   -0.0960   52.5754
     0.2449    1.3808  -17.0081
    -0.0001    0.0013    1.0000
  • then I have applied it on the image using this poor Matlab code:

    function [Ired] = affect(I,P)
    for i=1:308
    for j=1:288
    v1 = [j;i;1];
    v2 = P*v1;
    v3 =v2/v2(3,1);
    if (v3(1,1) <0 || v3(2,1)<0 || v3(3,1)<0)


and the result is the following:

enter image description here

  • but the problem with this code is it takes a lot of time (sometimes 10 minutes) to perform the operation on the image, especially if the size of the image is high. Is there a way to implement this code without loops to make it faster?

  • I get these black dots all over the image, I can do a dilation but the image will be distorted, do you have an idea how to eliminate this dots without distorting the image?

I cannot use predefined Matlab functions in this project.

  • $\begingroup$ Did you find the solution, i'm facing the same problem $\endgroup$
    – user16248
    Commented Jun 15, 2015 at 2:28

3 Answers 3


What do you mean by "apply this matrix to all pixels of the image without treating them one by one"?

The image is made of pixels, each one with its x-y location. The homography maps a coordinate on a plane into another one so you actually HAVE to apply it to every single pixel

(x' ; y' ; 1) = H (x ; y ; 1)

where H is your 3x3 homography matrix. Doing this you'll get the new image (plus some black areas), simply iterate the equation for every pixel in the image.

(sorry for the bad formatting of the formula, wrote it down fast)

  • $\begingroup$ Yes’ i will get a new set of pixels ’ but how to affect the original image ’ is there a function in matlab that can do that ’ sorry but i am new to matlab $\endgroup$
    – Yesbra Bra
    Commented Feb 24, 2015 at 8:11
  • $\begingroup$ Also, how to get a two dimentional array representaion of the image (x and y locations ) because i have a matrix of 900 by 650 by 3 ? $\endgroup$
    – Yesbra Bra
    Commented Feb 24, 2015 at 8:15
  • $\begingroup$ Ok, now i got what you were looking for: have a look at IMWARP it.mathworks.com/help/images/ref/imwarp.html $\endgroup$
    – David
    Commented Feb 24, 2015 at 8:19
  • $\begingroup$ The 3 represent colors channels, so you have a 900x650 matrix for every basic color (red green and blue). If you try converting it to greyscale image with rgb2gray you'll get only one matrix. You can work in greyscale image and then re-convert it back to color or you can apply imwarp to every color channel, will lead to the same result $\endgroup$
    – David
    Commented Feb 24, 2015 at 8:21
  • $\begingroup$ sorry for the delay , i made some progress but i am not allowed to use predefined transformation matlab functions , i have edited the question for this purpose $\endgroup$
    – Yesbra Bra
    Commented Feb 28, 2015 at 13:41

You should use the function maketform to create your homography, and apply it on image by using imtransform. Don't forget to apply transpose on your matrix, because Matlab works on pixels as rows, not as columns.

   A = transpose([....])  %Your matrix in here
   t = maketform( A, 'perspective');
   imOut = imtransform(im,t);
  • $\begingroup$ thank you , but i forgot to specicify that the use of predifined function in matlab is not possible in my project . $\endgroup$
    – Yesbra Bra
    Commented Feb 28, 2015 at 13:39
  • $\begingroup$ iactually i have edited the question to make this clear for every body $\endgroup$
    – Yesbra Bra
    Commented Feb 28, 2015 at 13:40

You must set the final size of your image. Take a look here.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.