Gradient based sharpness identifictaion in an image
11 views (last 30 days)
Show older comments
Hi,
I am trying to identify sharply focused portions in the attached image using imgradient operator in the matlab. I have used "sobel" method.
I have to do this operation on many images, I am wondering is there any way to apply some thresolding while implementing imgradient or (any similar operator) in the matlab.
load matlab; imshow(II,[])
0 Comments
Accepted Answer
Image Analyst
on 29 Apr 2025
Edited: Image Analyst
on 29 Apr 2025
Try using imgradient or stdfilt to find regions of high detail. These should be in focus. Of course low detail areas could also be in focus but there's no way of knowing from the image because they're smooth with no details to ascertain focus. Change the filter window width and threshold to whatever works for you - it's just a judgement call.
clc% Initialization steps.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
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 long g;
format compact;
fontSize = 15;
windowSize = 5;
load('focus.mat');
subplot(2, 2, 1);
imshow(II, [])
axis('on', 'image');
impixelinfo;
title('Original image', 'FontSize',fontSize);
% Get a mask so we can mask out the circular boundary edge effect.
mask = II == 0;
mask = imdilate(mask, ones(windowSize));
% imshow(mask);
% Find the local gradient
scanningWindow = ones(windowSize);
detailsImage = imgradient(II);
% detailsImage = stdfilt(II, scanningWindow); % Alternative way of finding detail areas.
% Mask out edge effects
detailsImage(mask) = 0;
% Display image.
subplot(2, 2, 2);
imshow(detailsImage, [])
axis('on', 'image');
impixelinfo;
title('Local Variation', 'FontSize',fontSize);
% Show histogram of the local variation image.
subplot(2, 2, 3);
[counts, grayLevels] = histcounts(detailsImage(:), 100);
% Let's suppress bin 1 because there are so many pixels with gray level of zero.
counts(1) = 0;
bar(grayLevels(1:end-1), counts, 'blue');
grid on;
caption = sprintf('Histogram of local variation image\nwith window size = %d', windowSize);
title(caption, 'FontSize',fontSize);
% Threshold to find areas of high detail (high variation);
thresholdLevel = 25; % Whatever.
xline(thresholdLevel, 'Color', 'r', 'LineWidth', 2); % Put up vertical line on histogram at the threshold level.
focusedRegions = detailsImage > thresholdLevel;
% Display image.
subplot(2, 2, 4);
imshow(focusedRegions, [])
axis('on', 'image');
impixelinfo;
title('Focused Regions', 'FontSize',fontSize);
8 Comments
Image Analyst
on 3 May 2025
Edited: Image Analyst
on 3 May 2025
For one image, to replace it with a new gray scale value
mask = grayImage == 0; % Get pure black
mask = bwareafilt(mask, 1); % Take largest blob which should be the surround.
grayImage(mask) = yourNewGrayLevel;
To have it be some particular RGB color
mask = grayImage == 0; % Get pure black
mask = bwareafilt(mask, 1); % Take largest blob which should be the surround.
% Get individual color channels.
redChannel = grayImage;
greenChannel = grayImage;
blueChannel = grayImage;
% Make mask region be the exact color you want.
redChannel(mask) = yourRedValue;
greenChannel(mask) = yourGreenValue;
blueChannel(mask) = yourBlueValue;
rgbImage = cat(3, redChannel, greenChannel, blueChannel); % Make RGB image from separate color channels.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
