apply region growing code to my image
10 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Image Analyst
on 27 Jan 2023
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 Comments
Image Analyst
on 30 Jan 2023
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');
More Answers (1)
Benjamin
on 29 Jan 2023
Edited: Image Analyst
on 29 Jan 2023
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.
0 Comments
See Also
Categories
Find more on Explore and Edit Images with Image Viewer App in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!