Filter löschen
Filter löschen

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

2 Ansichten (letzte 30 Tage)
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?

Akzeptierte Antwort

Image Analyst
Image Analyst am 14 Sep. 2018
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 Kommentare
Christie John Jacob
Christie John Jacob am 3 Feb. 2020
Bearbeitet: Image Analyst am 3 Feb. 2020
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 am 3 Feb. 2020
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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Image Processing Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by