Regiongrowing function not working
4 views (last 30 days)
Show older comments
Gaurav Kumar
on 7 Nov 2017
Commented: Image Analyst
on 8 Nov 2017
While using regiongrowing function in Matlab from a point selected using ginput, an error is thrown "Undefined function or variable 'regiongrowing'.". I am using Matlab2015. What is the possible solution for this. Thanks
0 Comments
Accepted Answer
Image Analyst
on 7 Nov 2017
regiongrowing is not a built-in function. You must add your regiongrowing custom function you wrote to the path with the Set Path button on the Home tab of the tool ribbon, or else have it in your current folder.
5 Comments
Image Analyst
on 8 Nov 2017
I don't think you need region growing. I think you can just call imgradient(), then threshold to find edges (steep gradients). Then you fill it and use bwselect() to extract out the region they clicked on. Then use that as a mask to replace the original pixel values with the blurred pixel values.
Image Analyst
on 8 Nov 2017
See this code:
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 short g;
format compact;
fontSize = 25;
%===============================================================================
% Get the name of the image the user wants to use.
baseFileName = 'imgb.jpg';
% Get the full filename, with path prepended.
folder = pwd
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
errorMessage = sprintf('This image does not exist:\n%s', fullFileName);
errordlg(errorMessage);
return;
end
%===============================================================================
% Get the name of the image the user wants to use.
baseFileName = 'coins.png';
folder = fileparts(which(baseFileName)); % Determine where demo folder is (works with all versions).
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
%===============================================================================
% Read in demo image.
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.
% Use weighted sum of ALL channels to create a gray scale image.
% grayImage = rgb2gray(grayImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the original image.
subplot(3, 3, 1);
imshow(grayImage, []);
axis on;
caption = sprintf('Original Color Image, %s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0.05 1 0.95]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
message = 'Click on one coin';
uiwait(msgbox(message));
[x, y] = ginput(1)
% Create the blurred image.
windowWidth = 13;
blurryImage = conv2(grayImage, ones(windowWidth)/windowWidth^2, 'same');
% Display the image.
subplot(3, 3, 2);
imshow(blurryImage, []);
axis on;
caption = sprintf('Blurred Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo();
drawnow;
% Create the gradient image.
gradientImage = imgradient(grayImage);
% Display the image.
subplot(3, 3, 3);
imshow(gradientImage, []);
axis on;
caption = sprintf('Gradient Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo();
drawnow;
% Display the histogram of the gradient image.
subplot(3, 3, 4);
histogram(gradientImage);
grid on;
binaryImage = gradientImage > 40;
% Fill them to get rid of noise.
binaryImage = imfill(binaryImage, 'holes');
% Display the mask image.
subplot(3, 3, 5);
imshow(binaryImage, []);
axis on;
caption = sprintf('Binary Mask Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo();
drawnow;
% Extract the one blob they clicked on
oneBlob = bwselect(binaryImage, round(x), round(y), 8);
% Display the mask image.
subplot(3, 3, 6);
imshow(oneBlob, []);
axis on;
caption = sprintf('Binary Mask Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo();
drawnow;
% Make a masked masked image.
maskedImage = grayImage; % Initialize
maskedImage(oneBlob) = blurryImage(oneBlob); % Initialize
% Display the mask image.
subplot(3, 3, 7);
imshow(maskedImage, []);
axis on;
caption = sprintf('Masked Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo();
drawnow;

More Answers (0)
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!