Gradient based sharpness identifictaion in an image
显示 更早的评论
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,[])

采纳的回答
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);
Beginning to run LiveEditorEvaluationHelperEeditorId.m ...
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 个评论
Thank you very much!
Hi, Is it possible to apply mask before performing imgradient ()?
Yes, but since the mask I made was gotten by thresholding the gradient image, how would you get your mask if you had no gradient image yet?
Of course you could make up a mask somehow, for example by hand drawing using functions like in the attached demos, and then you could use those masks to mask the image before getting the gradient of it, but the sharp edges caused by doing that will give you high gradient values at the edges of the mask.
I guess I'm just not sure what you're thinking. Why do you think it would be something good to do?
Hi, Actually this is not to estimate the gradient. I have to make some video files from the original images, for that it would be better I remove the outer circle.
You can replace the black outer circle color with a different color, or crop it a little smaller but not make it round. I've never seen a round video. Videos are rectangular. So I'm still not sure what you're thinking. Do you have an example of a round video or one that shows what you want?
Yes, I think I will go ahead with replacing it with the different color.
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.
Fabulous! Thank you very much
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Object Analysis 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
