The attached images are my adaptive threshold output. When i use imbinarise, i get completely black pixels. Please can someone help me to create a binary mask from the attached output images.

 Respuesta aceptada

Image Analyst
Image Analyst el 13 de En. de 2022

0 votos

Try adjusting the threshold value T in imbinarize() until you get the threshold you want:
BW = imbinarize(I,T) creates a binary image from image I using the threshold value T. T can be a global image threshold, specified as a scalar luminance value, or a locally adaptive threshold, specified as a matrix of luminance values.

6 comentarios

Elysi Cochin
Elysi Cochin el 13 de En. de 2022
Editada: Elysi Cochin el 17 de En. de 2022
sir if i use multithresh, how can i create a black and white mask from both the regions
Image Analyst
Image Analyst el 13 de En. de 2022
Editada: Image Analyst el 13 de En. de 2022
That was yanqi's suggestion, not mine. I suggested using a single threshold. Try adjusting the T value. Or you could just use a fixed threshold.
% 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 = 'im2.bmp';
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.
% Extract the blue channel.
grayImage = grayImage(:, :, 3);
end
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(2, 2, 1);
imshow(grayImage);
impixelinfo;
axis('on', 'image');
title('Original Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
hold on
% Maximize window.
g = gcf;
g.WindowState = 'maximized'
drawnow;
%--------------------------------------------------------------------------------------------------------
% Get a histogram
subplot(2, 2, [2, 4]);
imhist(grayImage);
grid on;
title('Histogram of Original Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
%--------------------------------------------------------------------------------------------------------
% Binarize the image.
thresholdValue = 149
xline(thresholdValue, 'Color', 'r', 'LineWidth', 2);
mask = grayImage > thresholdValue;
% Display mask image.
subplot(2, 2, 3);
imshow(mask);
impixelinfo;
axis('on', 'image');
drawnow;
caption = sprintf('Threshold = %d pixels', thresholdValue);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
Elysi Cochin
Elysi Cochin el 13 de En. de 2022
Sir but in some case i also need the 2 regions separately. Thats why i used multithresh. But how can i get the mask of the 2 regions separately? How is the mask generated from the output of imquantize or label2rgb
Image Analyst
Image Analyst el 13 de En. de 2022
Then use multithresh but just get two masks, one with the lower threshold and one with the higher threshold.
Elysi Cochin
Elysi Cochin el 13 de En. de 2022
Sir please can you show how the mask is obtained using lower threshold and higher threshold
% 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 = 'im2.bmp';
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.
% Extract the blue channel.
grayImage = grayImage(:, :, 3);
end
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(2, 2, 1);
imshow(grayImage);
impixelinfo;
axis('on', 'image');
title('Original Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
hold on
% Maximize window.
g = gcf;
g.WindowState = 'maximized'
drawnow;
%--------------------------------------------------------------------------------------------------------
% Get a histogram
subplot(2, 2, 2);
imhist(grayImage);
grid on;
title('Histogram of Original Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
%--------------------------------------------------------------------------------------------------------
% Set thresholds for each level.
thresholdValue1 = 149
xline(thresholdValue1, 'Color', 'r', 'LineWidth', 2);
thresholdValue2 = 169
xline(thresholdValue2, 'Color', 'r', 'LineWidth', 2);
%--------------------------------------------------------------------------------------------------------
% Binarize the image.
mask1 = grayImage > thresholdValue1;
% Display mask image.
subplot(2, 2, 3);
imshow(mask1);
impixelinfo;
axis('on', 'image');
drawnow;
caption = sprintf('Threshold = %d pixels', thresholdValue1);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
%--------------------------------------------------------------------------------------------------------
% Binarize the image.
mask2 = grayImage > thresholdValue2;
% Display mask image.
subplot(2, 2, 4);
imshow(mask2);
impixelinfo;
axis('on', 'image');
drawnow;
caption = sprintf('Threshold = %d pixels', thresholdValue2);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');

Iniciar sesión para comentar.

Más respuestas (1)

KSSV
KSSV el 12 de En. de 2022
It looks like imbinarize is working fine.
I = imread('https://in.mathworks.com/matlabcentral/answers/uploaded_files/860265/im3.bmp') ;
imshow(I)
I1 = imbinarize(I) ;
imshow(I1)

3 comentarios

I = imread('https://in.mathworks.com/matlabcentral/answers/uploaded_files/860260/im2.bmp') ;
I1 = imbinarize(I) ;
imshow(I1)
Elysi Cochin
Elysi Cochin el 12 de En. de 2022
when we use imbinarize we get the green outline, is it possible to get the red outline
yanqi liu
yanqi liu el 13 de En. de 2022
yes,sir,may be use multi threshold,if possible,may be upload this origin image file

Iniciar sesión para comentar.

Categorías

Más información sobre Image Processing and Computer Vision en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 12 de En. de 2022

Editada:

el 17 de En. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by