Why should I rotate an image before applying canny filter?

2 views (last 30 days)
Hello. Please help me, Matlab genius!
1) I am using a code below to detect a terrain slope from images. By the way, why should I rotate the image before applying 'canny' filter? Is it okay to skip the 'imrotate'?
2) I took some pictures outside(asphalt road) and I used this code below. Then, I can see a green line on the image. However, I should open the plot window to know the gradient of the green line. Is there any possible way to display the gradient information on the figure window? or can I collect the gradient information in simple way?
3) And I only need a gradient line in the center of the image. How I only have a center line?
I = imread('IMG.tif');
rotI = imrotate(I, 0 ,'crop'); % <<<<< why I need to rotate?????
BW = edge(rotI,'canny');
[H,T,R] = hough(BW);
xlabel('\theta'), ylabel('\rho');
axis on, axis normal, hold on;
P = houghpeaks(H,5,'threshold',ceil(0.3*max(H(:))));
x = T(P(:,2));
y = R(P(:,1));
% Find lines and plot them
lines = houghlines(BW,T,R,P,'FillGap',5,'MinLength',7);
figure, imshow(rotI), hold on
max_len = 0;
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
% plot beginnings and ends of lines
% determine the endpoints of the longest line segment
len = norm(lines(k).point1 - lines(k).point2);
if ( len > max_len)
max_len = len;
xy_long = xy;
Thank you for your time!! Any comment will be very happy for me!

Answers (1)

Kushagr Gupta
Kushagr Gupta on 7 Nov 2016
To answer the first question:
Canny edge detection by itself does not require rotation of images as can be seen in the edge detection example available at the following link:
That being said, it depends on the application for which it is being used and developed which might necessitate rotation of images. However, even in the code snippet provided as the second argument to the 'imrotate' function is 0, the image is not being rotated and hence the variables I and rotI have the same data, which can be cross checked by plotting the difference of the two images, shown as follows: >> imshow(I-rotI) % pops up a black image
Documentation of 'imrotate' is available at the following link:
The answer to your second question lies in the way functions ' hough ', ' houghpeaks ' and ' houghlines ' are used. I would recommend going through their documentation (inserted as links in the names) to understand the outputs of these functions. In a nutshell, 'hough' function can be used to find line segments in an image with a particular orientation and within certain thresholds. It can be modified as the user wants, to display gradients or other information in certain areas of the image.
Hope this helps!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!