"Region grow"
Show older comments
Hi, anyone know how do I analyze a pixel and its four neighbors? and if any of these neighbors have a similar intensity to the pixel how do I put it in a new image with the value 1?
3 Comments
Image Analyst
on 23 Dec 2011
How similar is similar? Within how many gray levels? You're just replacing the central pixel with the value 1. So how is this a region grow?
Marlene
on 23 Dec 2011
Image Analyst
on 23 Dec 2011
See my demo below which does this.
Answers (4)
Sean de Wolski
on 22 Dec 2011
doc blockproc;
doc colfilt
2 Comments
Marlene
on 22 Dec 2011
Walter Roberson
on 23 Dec 2011
fun is a function handle of a routine that will analyze the individual block and return the new value.
Walter Roberson
on 23 Dec 2011
0 votes
What do you want to do on the image boundaries?
Is this a truecolor (RGB) image, a grayscale image, a pseudocolor image?
What is to be put in to the new image if the pixel is determined not to be similar?
Think about what you could do with diff(IMG,1)
You may not need blockproc() for this task, if the definition of "similar to" is sufficiently regular.
Image Analyst
on 23 Dec 2011
0 votes
Marlene, just compute the morphological erosion and subtract it from your original, then threshold. It's like 4 lines. You'll need imerode from the Image Processing Toolbox. Do you have that toolbox?
Image Analyst
on 23 Dec 2011
Marlene: I sense you might need more "help" than my last answer so here is a full blown demo. It does what you said in your first comment. Essentially it's still 4 lines:
- Construct a structuring element for the 4 neighbors
- Do a morphological erosion
- Subtract from the original image
- Threshold the absolute value of the difference.
It just has some extra stuff in there to make it a fancy demo with displays, etc.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 14;
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'cameraman.tif';
fullFileName = fullfile(folder, baseFileName);
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
subplot(2, 3, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Rename figure title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Get the minimum in a cross shaped kernel
% This is the min value in the four 4-connected neighbors.
se = [0 1 0; 1 0 1; 0 1 0];
localMinImage = imerode(grayImage, se);
% Display the local min image.
subplot(2, 3, 2);
imshow(localMinImage, []);
title('Local Min Image (Erosion)', 'FontSize', fontSize);
% Subtract it from the original
diffImage = grayImage - localMinImage;
% Display the difference image.
subplot(2, 3, 3);
imshow(diffImage, []);
title('Difference Image', 'FontSize', fontSize);
% Compute and display the histogram of the difference image.
[pixelCount grayLevels] = imhist(abs(diffImage));
subplot(2, 3, 4);
bar(grayLevels, pixelCount);
grid on;
xlim([0 255]);
caption = sprintf('Histogram of the Absolute Value of\nDifference Image');
title(caption, 'FontSize', fontSize);
% Threshold the difference image
% Ask user for a threshold.
defaultThresholdValue = 45;
userPrompt = 'Enter the threshold value';
caUserInput = inputdlg(userPrompt, 'Enter the threshold value',1,{num2str(defaultThresholdValue)});
thresholdValue = round(str2num(cell2mat(caUserInput)));
% Check for a valid integer.
if isempty(thresholdValue)
% They didn't enter a number.
% They entered a character, symbols, or something else not allowed.
thresholdValue = defaultThresholdValue;
message = sprintf('I said it had to be an integer.\nI will use %d and continue.', thresholdValue);
uiwait(warndlg(message));
end
% Threshold the imnage with the user supplied threshold.
binaryImage = abs(diffImage) > thresholdValue;
% Display the binary image.
subplot(2, 3, 5);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize);
6 Comments
Marlene
on 23 Dec 2011
Image Analyst
on 23 Dec 2011
I don't see why this is any different than thresholded erosion, like I did. Perhaps you don't really understand what I did. Maybe you could try my code. If it doesn't work then supply a small matrix as an example input and give the final desired output. With a small modification to my code you could run it again on the output image to grows the binary output image outward.
Marlene
on 24 Dec 2011
Image Analyst
on 24 Dec 2011
If you don't want to use an efficient vectorized method and would rather use a nested for loop method with linked lists or recursion, then fine - you can do that. Why don't you post your image and tell me which blobs you want to keep or how you want to process them.
Marlene
on 26 Dec 2011
Marlene
on 27 Dec 2011
Categories
Find more on Object Analysis 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!