필터 지우기
필터 지우기

How to get the thresholded gradient of an image?

조회 수: 7 (최근 30일)
John Snow
John Snow 2014년 1월 3일
댓글: Farah Yousef 2021년 1월 11일
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

답변 (2개)

Image Analyst
Image Analyst 2014년 1월 3일
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
Image Analyst
Image Analyst 2021년 1월 10일
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);
Farah Yousef
Farah Yousef 2021년 1월 11일
yes it works , thank you.

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


Youssef  Khmou
Youssef Khmou 2014년 1월 3일
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)
  댓글 수: 1
John Snow
John Snow 2014년 1월 4일
I tried it but the result wasnt like figure b

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

Community Treasure Hunt

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

Start Hunting!

Translated by