Get the coordinates of a pixel
6 views (last 30 days)
Show older comments
Hi! I'm new in matlab.I try to identify a pixel at the intersection of the four black and white squares and get the coordinates of the pixel. How can i start first, what is the method? Thanks
0 Comments
Accepted Answer
Image Analyst
on 25 Feb 2013
I'd probably first crop the image to the known region where the pattern lives. Then I'd threshold the image to find the gray boxes. Then I'd call imerode a little bit to make sure the gray boxes are separated. Then I'd find the centroids with regionprops() and say the center is at the average of the centroids.
Another method might be to call an edge detection filter to get a cross, then split the cross into two sets of points by ignoring points near the center. Then call polyfit twice and see where the equations of the lines cross using simple algebra.
2 Comments
Image Analyst
on 6 Mar 2013
Edited: Image Analyst
on 6 Mar 2013
I don't see the need for all that cropping. If you crop then you'll lose the location of the squares in the original image. I'd just find the areas based on intensity, eccentricity, and perimeter, like in this code I wrote to work on your image I downloaded:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
% Read in a standard MATLAB color demo image.
folder = 'C:\Users\Jmentday\Documents\Temporary';
baseFileName = '2wp413n.jpg';
% 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. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 2, 1);
imshow(rgbImage);
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Now look at the green channel.
subplot(2, 2, 2);
imshow(greenChannel,[]);
title('Green Channel', 'FontSize', fontSize)
binaryImage = greenChannel > 183;
binaryImage = bwareaopen(binaryImage, 300);
subplot(2, 2, 3);
imshow(binaryImage)
title('Binary Image', 'FontSize', fontSize)
[labeledImage, numberOfBlobs] = bwlabel(binaryImage);
coloredLabels = label2rgb (labeledImage, 'hsv', 'k', 'shuffle'); % pseudo random color labels
subplot(2, 2, 3);
imshow(coloredLabels)
title('Labeled Image', 'FontSize', fontSize)
% Get all the blob properties. Can only pass in originalImage in version R2008a and later.
blobMeasurements = regionprops(labeledImage, greenChannel, 'all');
% Get all the measurements into their own arrays.
allCentroids = [blobMeasurements.Centroid];
xCentroids = allCentroids(1:2:end);
yCentroids = allCentroids(2:2:end);
% allBlobIntensities = [blobMeasurements.MeanIntensity]
allBlobAreas = [blobMeasurements.Area];
allEccentricities = [blobMeasurements.Eccentricity];
allPerimeters = [blobMeasurements.Perimeter];
% Print them out to see what they are
fprintf('Blob #, Area, Perimeter, Eccentricity\n');
for blob = 1 : numberOfBlobs
fprintf('%d, %9.3f, %9.3f, %9.3f, %9.3f, %9.3f\n', blob, ...
allBlobAreas(blob), allPerimeters(blob), allEccentricities(blob),...
xCentroids(blob), yCentroids(blob));
end
% Now I'll demonstrate how to select certain blobs based using the ismember function.
% Let's say that we wanted to find only those blobs
% with an intensity between 150 and 220 and an area less than 2000 pixels.
% This would give us the three brightest dimes (the smaller coin type).
% Get a list of the blobs that meet our criteria and we need to keep.
allowableEccentricityIndexes = (allEccentricities <0.5);
% allowableAreaIndexes = (allBlobAreas > 300) & (allBlobAreas < 600); % Take the small objects.
allowablePerimeterIndexes = (allPerimeters < 150); % Take the small objects.
keeperIndexes = find(allowableEccentricityIndexes & allowableEccentricityIndexes);
% Extract only those blobs that meet our criteria, and
% eliminate those blobs that don't meet our criteria.
% Note how we use ismember() to do this.
keeperBlobsImage = ismember(labeledImage, keeperIndexes);
% Re-label with only the keeper blobs kept.
[labeledImage, numberOfBlobs] = bwlabel(keeperBlobsImage, 8); % Label each blob so we can make measurements of it
% Now we're done. We have a labeled image of blobs that meet our specified criteria.
subplot(2, 2, 4);
coloredLabels = label2rgb (labeledImage, 'hsv', 'k', 'shuffle'); % pseudo random color labels
imshow(coloredLabels);
title('"Keeper" blobs with Centroids Marked', 'FontSize', fontSize);
% Remeasure what we have.
blobMeasurements = regionprops(labeledImage, greenChannel, 'Centroid');
allCentroids = [blobMeasurements.Centroid];
xCentroids = allCentroids(1:2:end);
yCentroids = allCentroids(2:2:end);
hold on;
fprintf('\nBlob #, X Centroid, Y Centroid\n');
for blob = 1 : numberOfBlobs
fprintf('%d, %14.3f, %9.3f\n', blob, ...
xCentroids(blob), yCentroids(blob));
plot(xCentroids(blob), yCentroids(blob), 'w+', 'MarkerSize', 30);
end
More Answers (1)
See Also
Categories
Find more on Image Data Workflows in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!