Gradient based sharpness identifictaion in an image
    11 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    MechenG
 el 29 de Abr. de 2025
  
    
    
    
    
    Comentada: MechenG
 el 3 de Mayo de 2025
            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 comentarios
Respuesta aceptada
  Image Analyst
      
      
 el 29 de Abr. de 2025
        
      Editada: Image Analyst
      
      
 el 29 de Abr. de 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 comentarios
  Image Analyst
      
      
 el 3 de Mayo de 2025
				
      Editada: Image Analyst
      
      
 el 3 de Mayo de 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.
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



