how can I find the centroid of an object in a image and extract a portion of it around that centre point
7 views (last 30 days)
Show older comments
Hello, I ll b vry grateful to u if u help me find the centroid of a mango image(image also has black background). n that i need to extract a strip of mango image around centroid. if i find centroid in bw image, den how can I interrelate it with color image so as to extract its strip? Please guide me. looking fwd to some ideas. ty
6 Comments
Ujjwal
on 27 Feb 2017
Image Analystm I would really appreciate if you can help me in any way for following problem.
I need to extract object blobs from a given image. I guess I can find the object features using MSER or SURF algorithms. After using the command to extract the object blobs, i get the information about the object in terms of structure, something like 1*19 objects. How can I extract useful information (say centroid or any other propoerties) from the MSER object so that this information can be used to find the blob in next similar image?
Thanks in advance!
Accepted Answer
Image Analyst
on 21 Jan 2017
Try the attached m-file. It produces a rectangular ring mask. Adapt as needed. I'm still interested in why you need such a bizarre mask. Care to explain why?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/176776/image.png)
4 Comments
Image Analyst
on 2 Mar 2017
mangos naturally vary in color. You could use histograms and/or spatial filters to try to find "unusual" looking areas.
More Answers (1)
Image Analyst
on 12 Jan 2017
Perhaps you mean this. Let me know if not, and post your image.
% Cleanup/initialization
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 long g;
format compact;
fontSize = 20;
% Check that user has the specified Toolbox installed and licensed.
hasLicenseForToolbox = license('test', 'image_toolbox'); % license('test','Statistics_toolbox'), license('test','Signal_toolbox')
if ~hasLicenseForToolbox
% User does not have the toolbox installed, or if it is, there is no available license for it.
% For example, there is a pool of 10 licenses and all 10 have been checked out by other people already.
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 color demo image.
folder = pwd;
folder = fileparts(which('peppers.png')); % Determine where demo folder is (works with all versions).
baseFileName = 'MANGO.png';
% 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
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorChannels should be = 3.
[rows, columns, numberOfColorChannels] = size(rgbImage);
% Display the original color image.
subplot(2, 3, 1);
imshow(rgbImage);
axis on;
title('Original Color Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% 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')
% Get the binary image. Background is exactly 0, so let's just get non-zero pixels.
binaryImage = max(rgbImage, [], 3) > 0;
% Just in case there is noise, fill the blobs and extract the largest blob only.
binaryImage = imfill(binaryImage, 'holes'); % Fill holes.
binaryImage = bwareafilt(binaryImage, 1); % Extract largest blob.
% Display the image.
subplot(2, 3, 2);
imshow(binaryImage);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
% Label the image
[labeledImage, numBlobs] = bwlabel(binaryImage);
% Measure Centroid
props = regionprops(labeledImage, 'Centroid');
xCentroid = props.Centroid(1);
yCentroid = props.Centroid(2);
% Put a cross on it.
hold on;
plot(xCentroid, yCentroid, 'b+', 'MarkerSize', 50);
% Compute the Euclidean Distance Transform
edtImage = bwdist(~binaryImage);
% Display the image.
subplot(2, 3, 3);
imshow(edtImage, []);
title('Euclidean Distance Transform Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
% Put a cross on it.
hold on;
plot(xCentroid, yCentroid, 'b+', 'MarkerSize', 50);
% Find the max value of it.
maxEDT = max(edtImage(:))
% Find a strip between 50 and 130 pixels away from the centroid.
mask = edtImage > (maxEDT - 130) & edtImage < (maxEDT - 50);
% Display the mask image.
subplot(2, 3, 4);
imshow(mask, []);
title('Ring Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
% Mask the original color image using bsxfun() function and display it.
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask, 'like', rgbImage));
% Display the mask image.
subplot(2, 3, 5);
imshow(maskedRgbImage, []);
title('Masked Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/176570/image.png)
9 Comments
Image Analyst
on 20 Jan 2017
Yes I did mean that. Do you want a box ring 10 pixels wide (total width)? Or 100 pixels wide? Or 500 Pixels wide? What about the width of the ring itself? 1 pixel wide? 100 pixels wide? Maybe just post a photo where you've drawn on it so we can see where you're drawing it.
Image Analyst
on 21 Jan 2017
I guess you're not going to answer me so I'll have to take time to code up a guess. See my other suggest answer.
See Also
Categories
Find more on Computer Vision with Simulink 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!