Thread Subject:
angle between 2 lines on image

Subject: angle between 2 lines on image

From: Kurt

Date: 5 Feb, 2011 10:51:03

Message: 1 of 9

Hello everyone,

I have an image with 2 lines , this can be seen at :
http://img46.imageshack.us/img46/5347/imagekci.jpg

I am trying to compute the angle between these 2 lines. But I think the angle between these 2 lines should be small , but instead I get 175 degrees.

I'm not sure if I'm missing something. Any help is welcome.
--kurtis

My code is :

%-------------------------------------------------------
clc;clear all;close all;

     lines = [144 58 310 35
                 464 126 332 134 ]
  
     u1 =lines(:,1);
     v1 =lines(:,2);
     u2 =lines(:,3);
     v2 = lines(:,4) ;
     
     e1 = [ u1 v1 ]
     e2 = [ u2 v2 ]
     
     midpt =[ (u1+u2)/2 (v1+v2)/2 ]
     
[m n]=size(u1);
One = ones(m,1)

 e1_i = (horzcat(e1,One))' % homogeneous coordinates
 e2_i = (horzcat(e2,One))' % homogeneous coordinates

 l_ht_i = cross(e1_i,e2_i)
 
u_l_ht_i = l_ht_i(1,:)
v_l_ht_i = l_ht_i(2,:)

 colsPerIter =1 ;
for i = 1:size(l_ht_i,2)
           from = (i-1)*colsPerIter+1
           to = from+colsPerIter-1
           
 l_i{i} = l_ht_i(:,from:to) / (sqrt( (u_l_ht_i(:,from:to) )^2 + (v_l_ht_i(:,from:to))^2 ) )

end

l_i = cell2mat(l_i) % normalized homogeneous representation of lines

li_1 =l_i(1,1);
li_2 =l_i(2,1);
lj_1 =l_i(1,2);
lj_2 =l_i(2,2);
theta_ij = acosd( (li_1*lj_1) + (li_2*lj_2) ) % compute angle between the 2 lines

%---------------------------------------------------------------------------------------------------------

Subject: angle between 2 lines on image

From: ImageAnalyst

Date: 5 Feb, 2011 16:12:13

Message: 2 of 9

Well one man's 175 degrees is another man's 5 degrees. Just subtract
the number from 180 if the number is more than 90.

Subject: angle between 2 lines on image

From: Kurt

Date: 5 Feb, 2011 17:26:03

Message: 3 of 9

I missed that....thanks again pal.

Is it possible for you to give me your general view and let me know if my way of getting the angle seems ok ?

take care
--kurtis

Subject: angle between 2 lines on image

From: Kurt

Date: 5 Feb, 2011 17:35:04

Message: 4 of 9

hey pal,

just to let you know. If you remember the 'local' line detection I was trying to do with the floor mat image, where you recommended me to the hough.

I was basically trying to implement an algorithm known as 'moving edges algorithm'm tracks a line in an image sequence given an estimate of it from a previous frame. Let me know if you heard of this and your thoughts if any.....

cheers again
kurtis

Subject: angle between 2 lines on image

From: ImageAnalyst

Date: 5 Feb, 2011 17:47:12

Message: 5 of 9

kurtis:
I don't really know much about motion analysis or motion estimation.
I'd go here though if I needed to find out more about it.

http://iris.usc.edu/Vision-Notes/bibliography/contentsmotion-i.html#Motion%20Analysis%20--Low-Level,%20Image%20Level%20Analysis,%20Mosaic%20Generation,%20Super%20Resolution,%20Shape%20from%20Motion

Subject: Question maximum finding the maximum gradient

From: Kurt

Date: 6 Feb, 2011 21:35:04

Message: 6 of 9

ImageAnalyst,

I have a question I'd like to ask,

If you remember my post a couple weeks ago ,

(http://www.mathworks.com/matlabcentral/newsreader/view_thread/301528#815071)

I was asking about searching for the local maximum gradient point along a search line . My question is when trying to find the maximum gradient value of a point on the image, is it helpful if I compute the gradient magnitude instead for this purpose instead of just using the (derivatives) gradient image obtained from the convolution filtering from what you showed me in the code with improfile?

take care,
kurt

Subject: Question maximum finding the maximum gradient

From: ImageAnalyst

Date: 6 Feb, 2011 21:41:30

Message: 7 of 9

On Feb 6, 4:35 pm, "Kurt " <rerty...@gmail.com> wrote:
> ImageAnalyst,
>
> I have a question I'd like to ask,
>
> If you remember my post a couple weeks ago ,
>
> (http://www.mathworks.com/matlabcentral/newsreader/view_thread/301528#...)
>
> I was asking about searching for the local maximum gradient point along a search line . My question is when trying to find the maximum gradient value of a point on the image, is it helpful if I compute the gradient magnitude instead for this purpose instead of just using the  (derivatives) gradient image obtained from the convolution filtering from what you showed me in the code with improfile?
>
> take care,
> kurt

--------------------------------------------------------
Yes, you could do that. In fact you can get the gradient directly
from the convolution, besides to getting it via the gradient()
function. Both will work.

Subject: Question maximum finding the maximum gradient

From: Kurt

Date: 6 Feb, 2011 22:28:03

Message: 8 of 9

hi pal,

I'm a bit confused.

---------------------------------------------------------------
An example I found does it like this :

 [dx,dy] = gradient(G); % G is a 2D Gaussian
 Ix = conv2(I,dx,’same’); Iy = conv2(I,dy,’same’);
----------------------------------------------------------------

-----------------------------------------------------------
Another example does it like this :

[gx,gy] = gradient(J);
mag = sqrt(gx.*gx+gy.*gy);
--------------------------------------------------------------

In this first example will the magnitude just be :
mag = sqrt(Ix.*Ix+Iy.*Iy);

if so why is no conv2 used in example 2?

Also, can you let me know what is an example of a 2D Gaussian I could possibly use?

Subject: Question maximum finding the maximum gradient

From: ImageAnalyst

Date: 6 Feb, 2011 22:55:11

Message: 9 of 9

I don't know why they're using conv2() in your first example. You
second example looks right. But that's the magnitude of the x
gradient and y gradient combined. Close but not exactly the same as
using the laplacian kernel
kernel = [-1 -1 -1; -1 8 -1; -1 -1 -1];
gradientImage = conv2(grayImage, kernel, 'same');
This way is like taking the average of the gradient in all 8
directions while the first method is like taking the average of the
gradient in only the vertical and horizontal directions. However, it
seems like the gradient gives a better looking image, as this demo
shows, at least for the cameraman image.
ImageAnalyst

% IMPORTANT: The newsreader may break long lines into multiple lines.
% Be sure to join any long lines that got split into multiple single
lines.
% These can be found by the red lines on the left side of your
% text editor, which indicate syntax errors, or else just run the
% code and it will stop at the split lines with an error.

% Change the current folder to the folder of this m-file.
if(~isdeployed)
        cd(fileparts(which(mfilename)));
end
clc; % Clear command window.
clear; % Delete all variables.
close all; % Close all figure windows except those created by
imtool.
imtool close all; % Close all figure windows created by imtool.
workspace; % Make sure the workspace panel is showing.
fontSize = 16;
grayImage = double(imread('cameraman.tif'));
% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')

[gx,gy] = gradient(double(grayImage));
gradientImage = sqrt(gx.*gx+gy.*gy);
% gradientImage has only positive values.
% Display the gradient image.
subplot(2, 2, 2);
imshow(gradientImage, []);
title('Gradient Image', 'FontSize', fontSize);

kernel = [-1 -1 -1; -1 8 -1; -1 -1 -1]/8;
conv2Image = conv2(grayImage, kernel, 'same');
% conv2Image has both positive and negative values.
% Square it so we can compare to the gradientImage.
conv2Image2 = sqrt(conv2Image .* conv2Image);
% Display the conv2Image2 image.
subplot(2, 2, 3);
imshow(conv2Image2, []);
title('Conv2 Image', 'FontSize', fontSize);

diffImage = conv2Image - gradientImage;
% Display the difference image.
subplot(2, 2, 4);
imshow(diffImage, []);
caption = sprintf('Difference between Conv2\nandGradient Image.');
title(caption, 'FontSize', fontSize);

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
angle Hakim 9 Nov, 2011 13:43:05
rssFeed for this Thread

Contact us