How do i create a matrix of border angles?

Hello, My input is a matrix of a black-white border image (created with the function border()) i want to create a matrix corresponding to that matrix, that hold the rotation angle values of the border in each pixels (and NaN in the pixels that are not part of the border). for example- if my image is a vertical line, then i get in the line's pixels values of zero, and the rest of the matrix will be NaN. Thanks!

3 Comments

There is no function border() in base MATLAB or the Image Processing Toolbox. Is your border the border of the image - straight lines at the outer edges - or are they boundaries of objects in an image, like you'd get from the edge() function?
sorry, you are right, i meant edge()
any idea how do i create a boundary matrix or how to calculate the gradient with this edge() matrix that i have? thanks a lot!

Sign in to comment.

Answers (2)

Matt J
Matt J on 8 Sep 2013
Edited: Matt J on 8 Sep 2013
Something like this, maybe
[gx,gy] = gradient(YourImage);
AngleMap = atan2(gx,gy)*180/pi; %in degrees, not radians
AngleMap(~(gx|gy))=nan;

5 Comments

There is a "d" version of atan2 - atan2d - like there is for all the other trig functions. They take or return degrees instead of radians.
Matt J
Matt J on 8 Sep 2013
Edited: Matt J on 8 Sep 2013
Good to know! But it's new. R2012a and prior releases don't have ATAN2D.
I know people here in answer have asked about it using R2010b, at least sind() link - surprised that they didn't add tangent when they were adding the others.
Hey, thanks for the answer, the problem is that the gradient at the border is zero (it's a local max), and then when i clear the cells around with the 3 code line i left with nothing but zeros. any idea how to make the border pixels itself to contain the angle values?
Here's another variation which works better,
gx=diff(YourImage,1,2); gx(:,end+1)=0;
gy=diff(YourImage,1,1); gy( end+1,:)=0;
AngleMap = atan2d(gx,gy)+90;
AngleMap(~YourImage)=nan

Sign in to comment.

Why not just use imgradient() in the Image Processing Toolbox. It gives you both gradient magnitude and gradient direction, like this example from the help shows:
% Read image and compute gradient magnitude
% and gradient direction using Prewitt's gradient operator.
I = imread('coins.png');
[Gmag, Gdir] = imgradient(I,'prewitt');
figure
imshowpair(Gmag, Gdir, 'montage');
title('Gradient Magnitude, Gmag (left), and Gradient Direction, Gdir (right), using Prewitt method')
axis off;

2 Comments

i don't have the image processing toolbox.. any idea how to implement such a thing?
You can use conv2() with kernels oriented in the vertical and horizontal direction: [-1,1] and [-1;1].

Sign in to comment.

Categories

Asked:

on 8 Sep 2013

Community Treasure Hunt

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

Start Hunting!