how can I find the centroid of an object in a image and extract a portion of it around that centre point

19 次查看(过去 30 天)
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 个评论
Ujjwal
Ujjwal 2017-2-27
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!

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2017-1-21
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?
  4 个评论
swathi
swathi 2017-3-2
Well nothing like that.. Our concern is about the space needed. And processing whole color image is going to be cumbersome. Anyway thank you so much.. Now our focus is to find the spot pixels in the strip extracted. In this regard if you could help me, i will be grateful to you.

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2017-1-12
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;
  9 个评论
Image Analyst
Image Analyst 2017-1-20
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Computer Vision with Simulink 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by