How to mark threshold value in image histogram?

My input is gray scale image. After drawing histogram of that image i want to mark the threshold value in the histogram using any red mark or line. I am using simple Otsu's thresholding method for segmentation.

댓글 수: 1

Rik
Rik 2023년 1월 23일
편집: Rik 2023년 1월 23일
A simple hold('on') followed by a call to plot should do it, or am I missing something? What did you try?
Also, this is your 68th question. By now you should be aware of the advice here. Try to make a MWE so we can run your code without any other dependencies and can reproduce your issue. Use the tools explained here to format your post properly.

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

 채택된 답변

Image Analyst
Image Analyst 2023년 1월 23일

0 개 추천

Try xline. Full Demo:
% Demo by Image Analyst
clc; % Clear the command window.
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 = 22;
markerSize = 40;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
folder = [];
baseFileName = 'cameraman.tif';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% 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.
fprintf('It is not really gray scale like we expected - it is color\n');
% Extract the blue channel.
grayImage = rgb2gray(grayImage);
end
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(2, 1, 1);
imshow(grayImage);
impixelinfo;
axis('on', 'image');
title('Original Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Update 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)
% Maximize window.
g = gcf;
g.WindowState = 'maximized';
drawnow;
%--------------------------------------------------------------------------------------------------------
% Get threshold
threshold = 255 * graythresh(grayImage);
%--------------------------------------------------------------------------------------------------------
% Display histogram
subplot(2, 1, 2);
imhist(grayImage);
grid on;
xline(threshold, 'Color', 'r', 'LineWidth', 2)
%--------------------------------------------------------------------------------------------------------
% Display threshold on histogram.
caption = sprintf('Histogram with threshold of %.1f gray levels', threshold);
title(caption, 'FontSize', fontSize)

댓글 수: 6

@Image Analyst Why my red line is not upto the height? I am using R2022a and histogram() function.
The red line is optional. You can delete it, or replace xline() by line() if you want. Or even with plot(). If it works other than that, can you Accept the Answer? 🙂
Zara Khan
Zara Khan 2023년 1월 25일
편집: Zara Khan 2023년 1월 25일
@Image Analyst want this red line. But the fact is its not going upto the top like your. Please see the attached file.
OK, so did you try any of the alternatives I listed? If not, why did you not try them? I expected you would.
yl = ylim;
line([threshold, threshold], [yl(1), yl(2)], 'LineWidth', 2, 'Color', 'r');
Full demo (tested - it works):
% Demo by Image Analyst
clc; % Clear the command window.
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 = 22;
markerSize = 40;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
folder = [];
baseFileName = 'cameraman.tif';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% 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.
fprintf('It is not really gray scale like we expected - it is color\n');
% Extract the blue channel.
grayImage = rgb2gray(grayImage);
end
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(2, 1, 1);
imshow(grayImage);
impixelinfo;
axis('on', 'image');
title('Original Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Update 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)
% Maximize window.
g = gcf;
g.WindowState = 'maximized';
drawnow;
%--------------------------------------------------------------------------------------------------------
% Get threshold
threshold = 255 * graythresh(grayImage);
%--------------------------------------------------------------------------------------------------------
% Display histogram
subplot(2, 1, 2);
imhist(grayImage);
grid on;
yl = ylim;
line([threshold, threshold], [yl(1), yl(2)], 'LineWidth', 2, 'Color', 'r');
%--------------------------------------------------------------------------------------------------------
% Display threshold on histogram.
caption = sprintf('Histogram with threshold of %.1f gray levels', threshold);
title(caption, 'FontSize', fontSize)
@Image Analyst yes I have tried. Thank you once again.
Then I'd call tech support since line() and xline() are not working the same way for you as for everyone else.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Red에 대해 자세히 알아보기

질문:

2023년 1월 23일

댓글:

2023년 1월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by