select a pixel on the grayscale image to get a mask

5 vues (au cours des 30 derniers jours)
Alberto Acri
Alberto Acri le 1 Fév 2023
Modifié(e) : Abhishek le 16 Fév 2023
I would need to understand how to select the pixel from the figure on the left (e.g., with the ginput command) in order to get the figure on the right.
I am currently using this demo by Image Analyst, but I would like to select the region of interest when the original grayscale image appears.
% 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 = 16;
markerSize = 40;
%--------------------------------------------------------------------------------------------------------
% READ IN TEST IMAGE
folder = [];
baseFileName = 'example_my.jpg';
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, 2, 1);
figure
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;
%--------------------------------------------------------------------------------------------------------
% Threshold to create mask
lowThreshold = 31;
highThreshold = 255;
% Interactively and visually set a threshold on a gray scale image.
% https://www.mathworks.com/matlabcentral/fileexchange/29372-thresholding-an-image?s_tid=srchtitle
% [lowThreshold, highThreshold] = threshold(lowThreshold, highThreshold, grayImage);
mask = grayImage >= lowThreshold & grayImage <= highThreshold;
% xline(lowThreshold, 'Color', 'r', 'LineWidth', 2)
% Get rid of blobs touching the border.
% mask = imclearborder(mask);
% Take 2 largest blobs.
mask = bwareafilt(mask, 2);
% Ask user to click on the blob they want to keep.
% % uiwait(helpdlg('Click on the blob you want to keep'));
% Extract only the blob they clicked on.
mask = bwselect(mask, 8);
% Fill the blobs.
mask = imfill(mask, 'holes');
% subplot(2, 2, 3);
figure
imshow(mask);
impixelinfo;
axis('on', 'image');
title('Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
g = gcf;
g.WindowState = 'maximized';
drawnow;

Réponses (1)

Abhishek
Abhishek le 16 Fév 2023
Modifié(e) : Abhishek le 16 Fév 2023
Hi Alberto,
I understand that you want to interactively select pixels in figures for certain masking application. The code you posted require two coordinate values: one for low threshold and another for high threshold.
You can achieve it through ‘ginput’ function with argument n=2 for high and low threshold. Note that first point you select will correspond to high threshold. Refer to the code below: -
%--------------------------------------------------------------------------------------------------------
% Threshold to create mask
%lowThreshold = 31;
%highThreshold = 255;
[threshold_x, threshold_y] = ginput(2);
highThreshold = grayImage(uint8(threshold_x(1)), uint8(threshold_y(1))); %lighter area
lowThreshold = grayImage(uint8(threshold_x(2)), uint8(threshold_y(2))); %darker area
Using the code, I'm getting following result.
For more information on ginput, you can refer to this documentation page: Identify axes coordinates - MATLAB ginput (mathworks.com)

Catégories

En savoir plus sur 3-D Volumetric Image Processing dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by