Get the coordinates of a pixel

3 views (last 30 days)
Jmentday
Jmentday on 25 Feb 2013
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

Accepted Answer

Image Analyst
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
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
Jmentday
Jmentday on 6 Mar 2013
Thanks a lot!!!!! U saved me! I cropped the images because I have 3000, to speed up the execution...........Thanks again

Sign in to comment.

More Answers (1)

Jmentday
Jmentday on 5 Mar 2013
Thanks for the answer and excuse me for the delay but I've tried with another method and I got some problems. I have this image http://i47.tinypic.com/2wp413n.jpg which is the first of the three located into 'mirras1' folder. My goal is to determine the displacement of the squared area on the right(which is moving down). What I've been trying to do was to detect the coordinates of a pixel in that area using correlation and calculating the displacement using the 'y' coordinates. Before correlation the image is cropped (because the image is very large 1600x1200). My problem is that if I choose a different value on x to crop I will get two maximum values for the last image after correlation. This is the code:
%clear all variables values
clear;
%close the active figure window
close;
%set curent folder
cd D:\Matlab\DigImgProcess
cd mirras1
imagefiles = dir('*.jpg');
% Number of files found
nfiles = length(imagefiles);
for i=1:nfiles
%create the pattern matrix
w=ones(36); w(1:18,1:18)=0; w(19:36,19:36)=0;
filename = imagefiles(i).name;
im = imread(filename);
img=rgb2gray(im);
adjimg=imadjust(img, [0.5 0.75], [0 1]);
imb=im2bw(adjimg);
%Crop image
imbc=imcrop(imb, [900 0 300 350]);
%Match a pattern
imshow(imbc)
[m,n]=size(imbc)
imbc=fft2(imbc);
w=conj(fft2(w,m,n));
x=real(ifft2(w.*imbc));
[r,c]=find(x==max(x(:)))
move(i)=r %y coordinate of each maximum value in each image
hold on
plot(c, r, 'b*')
hold off
end
Using imcrop(imb, [900 0 300 350]) yield: http://i45.tinypic.com/24o6q7a.png Using imcrop(imb, [1000 0 300 350]) yield: http://i46.tinypic.com/w70pjs.png
It's something wrong with my pattern matrix(mask)?

Categories

Find more on Visual Exploration 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!