How to calculate gradient features of an image?

My code is:
a = imread('C:\Users\DELL\Desktop\01_test.tif');
[Gx, Gy] = gradient(a);
[Gmag, Gdir] = gradient(Gx, Gy);
figure, imshow(Gmag, []), title('Gradient magnitude') figure, imshow(Gdir, []), title('Gradient direction') title('Gradient Magnitude (Gmag) and Gradient Direction (Gdir) using Sobel method') figure; imshowpair(Gx, Gy, 'montage'); axis off; title('Directional Gradients, Gx and Gy, using Sobel method')
error is:
??? Error using ==> rdivide Integers can only be combined with integers of the same class, or scalar doubles.
Error in ==> gradient at 75 g(2:n-1,:) = (f(3:n,:)-f(1:n-2,:))./h(:,ones(p,1));

댓글 수: 1

Guillaume
Guillaume 2014년 10월 22일
Please use the {} Code button to format your code.
And what is your question exactly?

댓글을 달려면 로그인하십시오.

답변 (2개)

Guillaume
Guillaume 2014년 10월 22일

1 개 추천

Possibly, you meant to use imgradient or imgradientxy.
You left out the important bit of the error, which is the one that told you on which line of your code the error occurred. I assume it's the
[Gmag, Gdir] = gradient(Gx, Gy);
line that gives you the error, since the 2nd argument to gradient must be a scalar value.

댓글 수: 10

Maninder
Maninder 2014년 10월 22일
편집: Maninder 2014년 10월 22일
I have matlab 7.12.0(R2011a) and this version not support imgradient or imgradientxy function. Acc to this syntax is: [FX,FY] = gradient(F); where F is a vector not a matrix, an image i have taken is in matrix form. So, i am unable to solve this problem. please send me the code.
gradient in 2011a works on vectors and matrices
We still don't know which line of your code is giving the error as you've not told us.
A possible cause for error is that your image is of type uint8 or some other integer type, which gradient may not work with. Assuming, it is a greyscale image, convert it to double:
[Gx, Gy] = gradient(double(a));
Maninder
Maninder 2014년 10월 22일
편집: Maninder 2014년 10월 22일
error in following line: [Gmag, Gdir] = gradient(Gx, Gy); ??? Error using ==> gradient at 50 Invalid inputs to GRADIENT.
i am using uint8 type
As said in my original answer, the 2nd argument to gradient must be a scalar value and indicates the scaling of the 1st argument. So your
[Gmag, Gdir] = gradient(Gx, Gy);
is never going to work.
What are you trying to calculate there?
Maninder
Maninder 2014년 10월 22일
I am tryimg to calculate magnitude of an image..
it looks to me that you're trying to replace some code that uses some features of the image processing toolbox with one that doesn't. You can't just replace a function name by another and hope for the best. The code that would work is:
[Gx, Gy] = imgradientxy(a);
[Gmag, Gdir] = imgradient(Gx, Gy);
Now if you don't have access to these functions, your best bet (short of upgrading) is to rewrite these functions yourself.
The first one, you can replace with gradient, as for the magnitude/direction, it's not particularly difficult to implement. See hypot and atan2d, it's basic trigonometry.
Maninder
Maninder 2014년 10월 22일
thanku so much... i'll try...
how i can calculate high gradient values of image
how to calulate the average gradient ?
i have calculated Gx,Gy,Gmag,Gdir

댓글을 달려면 로그인하십시오.

Munshida P
Munshida P 2019년 8월 24일

0 개 추천

how to calculate average gradient of an image

댓글 수: 6

[Gmag, Gdir] = imgradient(YourGrayscaleImage, 'prewitt');
average_gradient = mean2(Gmag);
this equation is used to get the clarity of the image ?isnt it?
Walter Roberson
Walter Roberson 2019년 8월 24일
편집: Walter Roberson 2019년 8월 24일
No, I would not think so. It gives some vague information about how abrupt changes are in the image, but that does not relate much to "clarity". Consider that you can have a very clear picture of a chess board: with the sharp transitions between black and white, you would have a number of high-magnitude gradient edges; does that mean the image is not clear?
yaaaa....i understand
how can i calculate this?please help me sir
Capture.PNG
[Gmag, Gdir] = imgradient(YourGrayscaleImage, 'prewitt');
average_gradient = sum(double(Gmag(:))) ./ ((size(Gmag,1)-1) .* ((size(Gmag,2)-1).*sqrt(2));
Thank you sir. I will try it now

댓글을 달려면 로그인하십시오.

질문:

2014년 10월 22일

댓글:

2019년 8월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by