"Region grow"

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
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
Marlene on 23 Dec 2011
is similar to the difference between the pixel and the neighboring pixel is less than T. the image has 256 gray levels. I'm not able to write the code to add similar pixels into a new image (which is binary).
Image Analyst
Image Analyst on 23 Dec 2011
See my demo below which does this.

Sign in to comment.

Answers (4)

Sean de Wolski
Sean de Wolski on 22 Dec 2011
doc blockproc;
doc colfilt

2 Comments

Marlene
Marlene on 22 Dec 2011
but I do not understand how the function blocksproc works. What is the fun parameter?
Walter Roberson
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.

Sign in to comment.

Walter Roberson
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.

2 Comments

Marlene
Marlene on 23 Dec 2011
Hi Walter,
My image is the gray levels. If the neighboring pixels are not similar were to zero in the new image. If they are similar to the value 1. My problem is I do not know how to write code to put the pixel(i, j)being analyzed in new image with value 1 and a pixel, for example, (i, j +1) is added to the new image with the value 1 if this is similar to pixel (i, j) of the gray image.
Marlene
Marlene on 23 Dec 2011
I wrote this code
A=zeros(size(imgm));
S=bwareaopen(B1,5); %seeds
% figure,imshow(S,[])
T=0.02; %threshold
J = find(S); %position of seeds
S1 = img2(J); %array of seeds
A=S;
for i=2:u-1
for j=2:t-1
for k = 1:length(S1)
seed(k) = S1(k);
if abs(seed(k)-img2(i-1,j))<=T
A(i-1,j)=1;
else if abs(seed(k)-img2(i,j-1))<=T
A(i,j-1)=1;
else if abs(seed(k)-img2(i+1,j))<=T
A(i+1,j)=1;
else if abs(seed(k)-img2(i,j+1))<=T
A(i,j+1)=1;
else A(i,j)=0;
end
end
end
end
end
end
end
but there are seeds that are not growing and points that are not seeds to grow ... And I just want to grow the seeds of neighboring pixels. Anyone know where I'm wrong?

Sign in to comment.

Image Analyst
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
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:
  1. Construct a structuring element for the 4 neighbors
  2. Do a morphological erosion
  3. Subtract from the original image
  4. 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
Marlene on 23 Dec 2011
Dear analist image, the demo is spectacular, but not for my problem. I got a better explanation for what I have to do:
As a simple explanation see this small generic framework with two different levels of intensity (white and gray) (square1).
http://s1178.photobucket.com/albums/x378/msmaxado/
The square marked with an X represents a pixel that was defined as seed. The type of connectivity used is 4, or just want to be analyzed pixels that are above, right, left or bottom despising the pixels that are diagonally. So the first cycle and the execution of the algorithm I want to be aggregated two pixels (A): what is right and what is down. The other two pixels (top and left) have a difference of intensity greater than the tax threshold and is therefore dismissed as constituents of the gray zone.
http://s1178.photobucket.com/albums/x378/msmaxado/
This second image exemplifies the above (square2) .
The second cycle of the algorithm will be the starting point of the first detected points (which is right) and do the same that was exposed on the first cycle. In this case detects the pixel that is below (A2), ie, the points not previously aggregated aggregates are again (it's called tagging). The third cycle will be the starting point added the second point (the A below the seed) and so on.
You can help me?
Image Analyst
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
Marlene on 24 Dec 2011
It's different because my teacher really want to grow a region of code. I've done the segmentation and imextendedmin EM algorithm, only the first segmentation getting good background and delete objects in the second ...
Image Analyst
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
Marlene on 26 Dec 2011
These are my pictures,
http://s1178.photobucket.com/albums/x378/msmaxado/
The first is that the image of birth for the seeds - the second image.
I want to grow the white dots of second image(seeds) to something that looks like the white regions and separated from the third image.
Marlene
Marlene on 27 Dec 2011
Image Analiyst you have news for me?

Sign in to comment.

Asked:

on 22 Dec 2011

Community Treasure Hunt

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

Start Hunting!