"Region grow"
조회 수: 16 (최근 30일)
이전 댓글 표시
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
답변 (4개)
Sean de Wolski
2011년 12월 22일
doc blockproc;
doc colfilt
댓글 수: 2
Walter Roberson
2011년 12월 23일
fun is a function handle of a routine that will analyze the individual block and return the new value.
Walter Roberson
2011년 12월 23일
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
2011년 12월 23일
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?
댓글 수: 0
Image Analyst
2011년 12월 23일
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);
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!