How to find the angle between the two lines from an region of interest of an image
    3 views (last 30 days)
  
       Show older comments
    
With reference to the mathwork example "Measuring Angle of Intersection", how to find the initial point on each boundary and trace the boundaries. And can you give me examples of each command, as i couldn't able to understand its working.
This program actually starts from "finding the initial point on each boundary of the cropped image". Since, i studied cropping and changing to grey-scaling a image, i haven't included it.
d
im = size(BW);
% horizontal beam
col1 = 4;
row1 = min(find(BW(:,col1)));
% angled beam
row2 = 12;
col2 = min(find(BW(row2,:)));
boundary1 = bwtraceboundary(BW, [row1, col1], 'N', 8, 70);
% set the search direction to counterclockwise, in order to trace downward.
boundary2 = bwtraceboundary(BW, [row2, col2], 'E', 8, 90,'counter');
imshow(RGB); hold on;
% apply offsets in order to draw in the original image
plot(offsetX+boundary1(:,2),offsetY+boundary1(:,1),'g','LineWidth',2);
plot(offsetX+boundary2(:,2),offsetY+boundary2(:,1),'g','LineWidth',2);
ab1 = polyfit(boundary1(:,2), boundary1(:,1), 1);
ab2 = polyfit(boundary2(:,2), boundary2(:,1), 1);
vect1 = [1 ab1(1)]; % create a vector based on the line equation
vect2 = [1 ab2(1)];
dp = dot(vect1, vect2);
% compute vector lengths
length1 = sqrt(sum(vect1.^2));
length2 = sqrt(sum(vect2.^2));
% obtain the larger angle of intersection in degrees
angle = 180-acos(dp/(length1*length2))*180/pi
intersection = [1 ,-ab1(1); 1, -ab2(1)] \ [ab1(2); ab2(2)];
% apply offsets in order to compute the location in the original,
% i.e. not cropped, image.
intersection = intersection + [offsetY; offsetX]
inter_x = intersection(2);
inter_y = intersection(1);
% draw an "X" at the point of intersection
plot(inter_x,inter_y,'yx','LineWidth',2);
text(inter_x-60, inter_y-30, [sprintf('%1.3f',angle),'{\circ}'],...
     'Color','y','FontSize',14,'FontWeight','bold');
interString = sprintf('(%2.1f,%2.1f)', inter_x, inter_y);
text(inter_x-10, inter_y+20, interString,...
     'Color','y','FontSize',14,'FontWeight','bold');
1 Comment
Answers (1)
  Image Analyst
      
      
 on 2 Jan 2014
        I can't do much with this since you didn't supply BW. But anyway, I don't know what it does but I never use bwtraceboundary(). I always get boundaries with bwboundaries() because it is much easier. Would that work for you?
2 Comments
  Image Analyst
      
      
 on 2 Jan 2014
				It looks explained in the comments. Which comment or function is not understandable? You know that functions are described in the help, right?
See Also
Categories
				Find more on Image Processing Toolbox in Help Center and File Exchange
			
	Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

