How do i create a matrix of border angles?

2 views (last 30 days)
Neta
Neta on 8 Sep 2013
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
Neta
Neta on 9 Sep 2013
sorry, you are right, i meant edge()
Neta
Neta on 10 Sep 2013
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
Neta
Neta on 8 Sep 2013
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?
Matt J
Matt J on 8 Sep 2013
Edited: Matt J on 8 Sep 2013
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.


Image Analyst
Image Analyst on 9 Sep 2013
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
Neta
Neta on 10 Sep 2013
i don't have the image processing toolbox.. any idea how to implement such a thing?
Image Analyst
Image Analyst on 10 Sep 2013
You can use conv2() with kernels oriented in the vertical and horizontal direction: [-1,1] and [-1;1].

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!