필터 지우기
필터 지우기

How do I threshold the image automatically based on its energy content?

조회 수: 2 (최근 30일)
Say my threshold is taking in those components that constitute about 75% of energy of image and making the remaining as zero.How do I do that?

채택된 답변

Image Analyst
Image Analyst 2018년 9월 14일
D_coder, try this:
clc; % Clear the command window.
clearvars;
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
grayImage = imread('cameraman.tif');
subplot(2, 2, 1);
imshow(grayImage);
title('Original Image', 'FontSize', fontSize);
sortedGrayLevels = double(sort(grayImage(:), 'descend'));
% Sum up the energy from most energetic to least energy.
percentage = cumsum(sortedGrayLevels);
percentage = percentage / percentage(end);
subplot(2, 2, 2);
plot(percentage, 'b-', 'LineWidth', 2);
grid on;
title('Cumulative Distribution Function', 'FontSize', fontSize);
% Find the brighter 75%
% Find the gray level where 75 percent is
index75 = find(percentage >= 0.75, 1, 'first')
grayLevel75 = grayImage(index75)
% threshold the most energy 75%
binaryImage = grayImage > grayLevel75;
subplot(2, 2, 3);
imshow(binaryImage);
title('Brighter 75%', 'FontSize', fontSize);
% Find the darker 75%
% Find the gray level where 75 percent is
index75 = find(percentage >= 0.25, 1, 'first')
grayLevel25 = grayImage(index75)
% threshold the most energy 75%
binaryImage25 = grayImage < grayLevel25;
subplot(2, 2, 4);
imshow(binaryImage25);
title('Darker 75%', 'FontSize', fontSize);
Each binary image will contain 75% of the energy. It just depends if you wanted to start with the darker pixels and work brighter, or start with the brightest pixels and work darker.
  댓글 수: 2
Christie John Jacob
Christie John Jacob 2020년 2월 3일
편집: Image Analyst 2020년 2월 3일
While using this code matlab displayed an error message that there is error in imshow function
' Error using images.internal.imageDisplayValidateParams>validateCData (line 119)
If input is logical (binary), it must be two-dimensional.
Error in images.internal.imageDisplayValidateParams (line 27)
common_args.CData = validateCData(common_args.CData,image_type);
Error in images.internal.imageDisplayParseInputs (line 78)
common_args = images.internal.imageDisplayValidateParams(common_args);
Error in imshow (line 245)
images.internal.imageDisplayParseInputs({'Parent','Border','Reduce'},preparsed_varargin{:});
Error in gray (line 27)
imshow(binaryImage) '
Why is this coming ?
Image Analyst
Image Analyst 2020년 2월 3일
I just copied and ran the code and it ran beautifully. You must have altered it somehow. Maybe you read in a true color RGB image instead of a gray scale image to fix that, put this right after the call to imread():
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
grayImage = rgb2gray(grayImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
% grayImage = grayImage(:, :, 2); % Take green channel.
end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by