to find the ROI surrounding the centroid

I have an image that has several objects in it. I have found the centroid of one of the desired objects in them. I want to create ROI surrounding this centroid alone and make other objects zero or null.

 Accepted Answer

Like I said in your question last month (that you accepted - thanks), that's covered in my Image Segmentation Tutorial. If you don't think it does, the post your code and show why not.

6 Comments

I managed to get just the desired object. but the object itself has an incomplete edge i.e., object has black and white color inside it. I need to get the object with unicolor(black) against the background(white).
It's not too hard, just threshold and use bwareaopen() to get rid of small specks. I improved the image by doing a crude background correction by fitting the background to a quadratic.
clc; % Clear the command window.
clearvars
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Read in a standard MATLAB gray scale demo image.
folder = 'C:\Users\BW\Documents\Temporary';
baseFileName = '2bofbt.jpg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- 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 in the search path folders.', 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);
if numberOfColorBands > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the original gray scale image.
subplot(2, 3, 1);
imshow(grayImage, []);
axis on;
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Let's compute and display the histogram.
[pixelCount, grayLevels] = imhist(grayImage);
subplot(2, 3, 4);
bar(pixelCount);
grid on;
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
%===== BACKGROUND CORRECTION ================================
% Do a crude background correction by fitting to a quadratic.
verticalProfile = sum(grayImage(:,1:200), 2);
subplot(2, 3, 2);
plot(verticalProfile);
y = (1:rows)';
coeffsV = polyfit(y, verticalProfile, 2);
% Correct image vertically.
correctionCurve = polyval(coeffsV, y);
correctionCurve = correctionCurve / max(correctionCurve);
percentageImage = repmat(correctionCurve, [1, columns]);
grayImage = double(grayImage) ./ percentageImage;
imshow(grayImage, []);
% Same for horizontal direction.
horizontalProfile = sum(grayImage(1:300,:), 1);
subplot(2, 3, 2);
plot(horizontalProfile);
x = 1:columns;
coeffsH = polyfit(x, horizontalProfile, 2);
% Correct image horizontally.
correctionCurve = polyval(coeffsH, x);
correctionCurve = correctionCurve / max(correctionCurve);
percentageImage = repmat(correctionCurve, [rows, 1]);
grayImage = uint8(double(grayImage) ./ percentageImage);
imshow(grayImage, []);
title('Background Corrected Grayscale Image', 'FontSize', fontSize);
drawnow;
%===== END OF BACKGROUND CORRECTION ================================
% Let's compute and display the histogram.
[pixelCount, grayLevels] = imhist(grayImage);
subplot(2, 3, 5);
bar(pixelCount);
grid on;
title('Histogram of background corrected image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Binarize the image
binaryImage = grayImage < 120;
% Get rid of small particles.
binaryImage = bwareaopen(binaryImage, 100);
% Fill holes.
binaryImage = imfill(binaryImage, 'holes');
% Display the image.
subplot(2, 3, 3);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize);
% Invert so that the "part" is black instead of white.
binaryImage = ~binaryImage;
% Display the image.
subplot(2, 3, 6);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize);
BV
BV on 2 Jun 2013
Edited: BV on 2 Jun 2013
thank you very much. I can see that small holes in the right corner of the original image has been filled. But I want the bigger region(white portion)on the left side of the bone to be filled up. so that the full image of the bone is visible without missing edges.
Perhaps you might want to use the convex hull by using bwconvhull(). Feel free to adapt my code. I've given you a good start, but I'm not going to do the whole project for you. Good luck.
BV
BV on 2 Jun 2013
Edited: BV on 2 Jun 2013
thank you once again) bwconvhull()doesnt work that well. I have a centroid position.I want to create a rectangle of a particular dimension sround this centroid. Is there any function to do make a rect around the centroid? I don know how to use the centroid in 'imrect()'.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!