Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Matlab pixel based detection of center and the angle of an object?

Subject: Matlab pixel based detection of center and the angle of an object?

From: Erdem Tuna

Date: 4 Jun, 2009 08:55:03

Message: 1 of 3

Hello,

I am working on a project, which is related with the tracking of multiple robots of each other. I am planning to do the project with platooning.

To start with, I need to implement a matlab code for determining the center of an object in an image--the pixel of the center of the object--- and the angle of the object according to its center if it is rotated-- rotation angle with respect to x-y plane--.

I would be very pleased if you could give advice and guide me to implement the code or post some sort of sample code.

Thank you very much,

Erdem

Subject: Matlab pixel based detection of center and the angle of an object?

From: Image Analyst

Date: 8 Jun, 2009 01:43:01

Message: 2 of 3

Erdem Tuna:
Here's some code to get the centroid. You can't determine a blob's rotation unless you have some reference image.

clc;
clear all;
disp('Running BlobsDemo.m...');
originalImage = imread('coins.png'); % Read in standard MATLAB demo image
% binaryImage = im2bw(originalImage, 0.4); % Threshold to binary
thresholdValue = 100;
binaryImage = originalImage > thresholdValue; % Alternate way to threshold.
binaryImage = imfill(binaryImage, 'holes');

subplot(3,2,1); imagesc(originalImage); colormap(gray(256)); title('Original Image');
subplot(3,2,2); imagesc(binaryImage); colormap(gray(256)); title('Binary Image');

labeledImage = bwlabel(binaryImage, 8); % Label each blob so can do calc on it
coloredLabels = label2rgb (labeledImage, 'hsv', 'k', 'shuffle'); % pseudo random color labels

% Save the labeled image as a tif image.
% imwrite(uint16(labeledImage), 'tif_labels.tif');
% tifimage = imread('tif_labels.tif');
% imtool(tifimage, []); % Use imtool so she can inspect the values.

subplot(3,2,3); imagesc(labeledImage); title('Labeled Image');
subplot(3,2,4); imagesc(coloredLabels); title('Pseudo colored labels');

blobMeasurements = regionprops(labeledImage, 'all'); % Get all the blob properties.
numberOfBlobs = size(blobMeasurements, 1);

% bwboundaries returns a cell array, where each cell
% contains the row/column coordinates for an object in the image.
% Plot the borders of all the coins on the original
% grayscale image using the coordinates returned by bwboundaries.
subplot(3,2,5); imagesc(originalImage); title('Outlines');
hold on;
boundaries = bwboundaries(binaryImage);
for k = 1 : numberOfBlobs
thisBoundary = boundaries{k};
plot(thisBoundary(:,2), thisBoundary(:,1), 'g', 'LineWidth', 2);
end
hold off;

fprintf(1,'Blob # Mean Intensity Area Perimeter Centroid\n');
for k = 1 : numberOfBlobs % Loop through all blobs.
% Find the mean of each blob. (R2008a has a better way where you can pass the original image
% directly into regionprops. The way below works for all versions including earlier versions.)
    thisBlobsPixels = blobMeasurements(k).PixelIdxList; % Get list of pixels in current blob.
    meanGL = mean(originalImage(thisBlobsPixels)); % Find mean intensity (in original image!)
blobArea = blobMeasurements(k).Area; % Get area.
blobPerimeter = blobMeasurements(k).Perimeter; % Get perimeter.
blobCentroid = blobMeasurements(k).Centroid; % Get centroid.
    fprintf(1,'#%d %18.1f %11.1f %8.1f %8.1f %8.1f\n', k, meanGL, blobArea, blobPerimeter, blobCentroid);
end
msgbox('Finished running BlobsDemo.m. Check out the figure window and the command window for the results.');

Subject: Matlab pixel based detection of center and the angle of an object?

From: Erdem Tuna

Date: 10 Jun, 2009 18:14:01

Message: 3 of 3

thank you very much, it is quite helpful

"Image Analyst" <imageanalyst@mailinator.com> wrote in message <h0hqb5$pqs$1@fred.mathworks.com>...
> Erdem Tuna:
> Here's some code to get the centroid. You can't determine a blob's rotation unless you have some reference image.
>
> clc;
> clear all;
> disp('Running BlobsDemo.m...');
> originalImage = imread('coins.png'); % Read in standard MATLAB demo image
> % binaryImage = im2bw(originalImage, 0.4); % Threshold to binary
> thresholdValue = 100;
> binaryImage = originalImage > thresholdValue; % Alternate way to threshold.
> binaryImage = imfill(binaryImage, 'holes');
>
> subplot(3,2,1); imagesc(originalImage); colormap(gray(256)); title('Original Image');
> subplot(3,2,2); imagesc(binaryImage); colormap(gray(256)); title('Binary Image');
>
> labeledImage = bwlabel(binaryImage, 8); % Label each blob so can do calc on it
> coloredLabels = label2rgb (labeledImage, 'hsv', 'k', 'shuffle'); % pseudo random color labels
>
> % Save the labeled image as a tif image.
> % imwrite(uint16(labeledImage), 'tif_labels.tif');
> % tifimage = imread('tif_labels.tif');
> % imtool(tifimage, []); % Use imtool so she can inspect the values.
>
> subplot(3,2,3); imagesc(labeledImage); title('Labeled Image');
> subplot(3,2,4); imagesc(coloredLabels); title('Pseudo colored labels');
>
> blobMeasurements = regionprops(labeledImage, 'all'); % Get all the blob properties.
> numberOfBlobs = size(blobMeasurements, 1);
>
> % bwboundaries returns a cell array, where each cell
> % contains the row/column coordinates for an object in the image.
> % Plot the borders of all the coins on the original
> % grayscale image using the coordinates returned by bwboundaries.
> subplot(3,2,5); imagesc(originalImage); title('Outlines');
> hold on;
> boundaries = bwboundaries(binaryImage);
> for k = 1 : numberOfBlobs
> thisBoundary = boundaries{k};
> plot(thisBoundary(:,2), thisBoundary(:,1), 'g', 'LineWidth', 2);
> end
> hold off;
>
> fprintf(1,'Blob # Mean Intensity Area Perimeter Centroid\n');
> for k = 1 : numberOfBlobs % Loop through all blobs.
> % Find the mean of each blob. (R2008a has a better way where you can pass the original image
> % directly into regionprops. The way below works for all versions including earlier versions.)
> thisBlobsPixels = blobMeasurements(k).PixelIdxList; % Get list of pixels in current blob.
> meanGL = mean(originalImage(thisBlobsPixels)); % Find mean intensity (in original image!)
> blobArea = blobMeasurements(k).Area; % Get area.
> blobPerimeter = blobMeasurements(k).Perimeter; % Get perimeter.
> blobCentroid = blobMeasurements(k).Centroid; % Get centroid.
> fprintf(1,'#%d %18.1f %11.1f %8.1f %8.1f %8.1f\n', k, meanGL, blobArea, blobPerimeter, blobCentroid);
> end
> msgbox('Finished running BlobsDemo.m. Check out the figure window and the command window for the results.');

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us