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,[])

채택된 답변
Image Analyst
2025년 4월 29일
편집: Image Analyst
2025년 4월 29일
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.
Image Analyst
2025년 5월 3일
편집: Image Analyst
2025년 5월 3일
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)
