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 件のコメント
MechenG
2025 年 4 月 30 日
Thank you very much!
MechenG
2025 年 5 月 3 日
Hi, Is it possible to apply mask before performing imgradient ()?
Image Analyst
2025 年 5 月 3 日
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?
MechenG
2025 年 5 月 3 日
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.
Image Analyst
2025 年 5 月 3 日
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?
MechenG
2025 年 5 月 3 日
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.
MechenG
2025 年 5 月 3 日
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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
