apply region growing code to my image

조회 수: 150 (최근 30일)
Alberto Acri
Alberto Acri 2023년 1월 27일
댓글: Rik 2023년 2월 1일
Hi! I am using this code (see link) although it is a bit old. The example given by the author works:
load example
figure, imshow(cIM, [0 1500]), hold all
poly = regionGrowing(cIM, [], 300); % click somewhere inside the lungs
plot(poly(:,1), poly(:,2), 'LineWidth', 2)
I would like to do the same with my image (example_my.jpg) getting the output on the right.
Is there any parameter I should change?
It always leads me to an error but I can't find a solution.
cIM_1 = im2double(imread('example_my.jpg'));
% grayImage = rgb2gray(cIM_1);
% J = im2uint16(grayImage);
% imshow(J)
figure, imshow(cIM_1, [0 1500]), hold all
poly = regionGrowing(cIM_1, [], 300); % click somewhere inside the lungs
plot(poly(:,1), poly(:,2), 'LineWidth', 2)
  댓글 수: 10
Alberto Acri
Alberto Acri 2023년 2월 1일
Hello @Rik! I wanted to insert the x and y coordinates in your code
mask = RegGrow(cIM_1(:,:,1),'seed',[y x],'MaxDiff',20);
with the command
[x,y] = ginput(1);
but it gives me the following error:
The seed must be a vector with a valid position.
Could you tell me how to solve it? Thanks!
Rik
Rik 2023년 2월 1일
You might need to round the coordinates to make the indices valid.

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

채택된 답변

Image Analyst
Image Analyst 2023년 1월 27일
Try this:
% 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);
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;
%--------------------------------------------------------------------------------------------------------
% Display histogram
subplot(2, 2, [2, 4]);
histogram(grayImage);
grid on;
title('Histogram of Image', 'FontSize', fontSize, 'Interpreter', 'None');
%--------------------------------------------------------------------------------------------------------
% 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);
% Fill the blobs.
% mask = imfill(mask, 'holes');
subplot(2, 2, 3);
imshow(mask);
impixelinfo;
axis('on', 'image');
title('Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
%--------------------------------------------------------------------------------------------------------
% Get the areas, diameters, and centroids.
props = regionprops(mask, 'Area', 'Centroid', 'EquivDiameter')
if isempty(props)
warningMessage = 'No blobs found!'
uiwait(warndlg(warningMessage));
return;
end
allAreas = [props.Area]
allDiameters = [props.EquivDiameter]
xy = vertcat(props.Centroid)
% Plot centroids on image as red crosshairs
xCentroids = xy(:, 1);
yCentroids = xy(:, 2);
hold on;
plot(xCentroids, yCentroids, 'r+', 'LineWidth', 3, 'MarkerSize', 20)
  댓글 수: 10
Alberto Acri
Alberto Acri 2023년 1월 30일
편집: Alberto Acri 2023년 1월 30일
Thank you for your reply. Indeed, the ginput command is preferable to indicating the pixel coordinates.
What I could not do is to apply the ginput command using your code. I thought of inserting it after the Treshold values but I don't know how to continue:
...
lowThreshold = 31;
highThreshold = 255;
[x,y] = ginput(1)
mask = bwselect(mask,y,x);
...
Could you help me with this?
Image Analyst
Image Analyst 2023년 1월 30일
You have to use the thresholds to create the mask first. Of course bwselect won't work until mask is created. So like:
%--------------------------------------------------------------------------------------------------------
% 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);
% Display the mask.
subplot(2, 2, 3);
imshow(mask);
impixelinfo;
axis('on', 'image');
title('Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');

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

추가 답변 (1개)

Benjamin
Benjamin 2023년 1월 29일
편집: Image Analyst 2023년 1월 29일
Region growing is a way to divide an image into smaller parts based on where different colors or shapes are found. It's like selecting starting points to divide the image into.

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by