How to get the thresholded gradient of an image?

I have an image and I want to get the thresholded gradient of the smoothed image the average filter is 5*5 but I cant find out how to get the gradient because the command imgradient doesnt work for my matlab version R2011b
Here is my code
img = mat2gray( imread ('image')); %to make it grayscale
smooth = imfilter (img, (ones (5)/25)); %average filter 5*5

Risposte (2)

You can use imfilter or conv2(). Either way, you'll need some negative weights to get the gradient, as I'm sure you know. Try my dog filter (Difference of Gaussians), attached below in blue text. The "harshest" gradient is the Laplacian which is just
kernel = [-1, -1, -1; -1, 8, -1; -1, -1, -1];
laplacianImage = conv2(double(grayImage), kernel);
imshow(laplacianImage, []);
You can get smoother gradients using the dog filter or smoother kernels, for example the Sobel filter.

8 Commenti

what if the kernel used is [1 1 1;1 -8 1;1 1 1] , the result should be ? ( compared to the first kernel)
I added the rest of the code but the result is not the same as illustrated What is needed is to do a 5*5 average filter and then threshold gradient of the value 0.15 to achieve the illustrated image (B) Here is my code so far
img = mat2gray( imread ('image1.tif')); %to make it grayscale
smooth = imfilter (img, (ones (5)/25)); %average filter 5*5
kernel = [-1, -1, -1; -1, 8, -1; -1, -1, -1];
laplacianImage = conv2(double(smooth), kernel);
final = im2bw (laplacianImage,0.15);
imshow(final, []);
Here is the required result (b)[the one on the right]
and Here is the original image on the left that im using
NOTE: I tried replacing the laplacian with sobel and also tried the dog_filter file and also tried the command gradient but I still couldn't get the result
Image Analyst
Image Analyst il 4 Gen 2014
Modificato: Image Analyst il 4 Gen 2014
Youssef, if the kernel is negative, you'll just get the negative of the image you'd get otherwise - it's as simple as that. This will look very very similar to the positive image since the image is mostly zero anyway and the Laplacian gives both a positive spike and negative spike - it's just that the location of the spikes is changed but since they're so close together, it looks pretty much the same.
John, you may have to play around with different parameters to get the look you want. The paper should say what they used.
ok thanks for the explanation
I solved this one if somebody in the future needs the solution.
I applied a sobel gradient and for the threshold I multiplied the 0.15 with the maximum value of the gradient image. Thank you for all the replys
@john snow
can you please show me the code that you used ?
It would be like this. Please "Vote" for my Answer if it helped you.
Of course my answer also worked. The Sobel is an edge detector which is not like a true gradient like the Laplacian, but it does find edges pretty well. Canny is another one like that.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fprintf('Beginning to run %s.m ...\n', mfilename);
grayImage = imread('coins.png');
subplot(2, 2, 1);
imshow(grayImage);
title('Original Image')
% Calculate the x- and y-directional gradients. By default, imgradientxy uses the Sobel gradient operator.
[Gmag, Gdir] = imgradient(grayImage);
% Display the directional gradients.
subplot(2, 2, 2);
imshow(Gmag, []);
title('Magnitude using Sobel Method')
threshold = 0.15 * max(Gmag(:))
binaryImage = Gmag > threshold;
subplot(2, 2, 3);
imshow(binaryImage, []);
caption = sprintf('Thresholded at %.1f', threshold);
title(caption)
fprintf('Done running %s.m.\n', mfilename);
yes it works , thank you.

Accedi per commentare.

you can try this essay :
I=im2double(imread('image1.tif'));
k=-1*ones(5);k(3,3)=23;
J=conv2(I,k);
figure, imshow(J)

Categorie

Richiesto:

il 3 Gen 2014

Commentato:

il 11 Gen 2021

Community Treasure Hunt

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

Start Hunting!

Translated by