Find coordinates of a pixel

11 views (last 30 days)
Filipa
Filipa on 28 Aug 2014
Commented: Image Analyst on 28 Aug 2014
I have this image and need to find the coordinates of a pixel nonzero of the points marked in red in image.
I thied this but but the result wasn't the expected.
[rows1 columns1]=size(lefthalf)
topleft=lefthalf(1:rows1/2,:);
[Ptrx Ptry]=find(topleft==1,1,'first')
To point 1 - By scanning each vertical line from the left image border to the half of image width in horizontal direction, find the coordinates of the farthest-left non-zero pixel.
To point 2 - From the image top to bottom, by searching each horizontal line from 1(x) toward right, find the coordinate of the first met non-zero pixel.
Thank's
  1 Comment
Filipa
Filipa on 28 Aug 2014
Sorry, I mixed up row, columns with x,y so the result is correct.
How can I do to find the point 2?

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 28 Aug 2014
You got your x,y wrong. You mixed up row,column with x,y - a very common mistake. You plot (x,y) but the find does not return x,y like you assumed. It returns row,column, which is [y,x] NOT [x,y] like you assumed.
  2 Comments
Filipa
Filipa on 28 Aug 2014
Thank's
But how can I do to find the point 2?
Image Analyst
Image Analyst on 28 Aug 2014
Filipa, just transpose the image to find the top most pixel. If you need a full blown, fancy demo, see below:
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 = 25;
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
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 standard MATLAB gray scale demo image.
folder = fileparts(which('cameraman.tif')); % Determine where demo folder is (works with all versions).
button = menu('Use which demo image?', 'CameraMan', 'Moon', 'Eight', 'Coins', 'Pout');
if button == 1
baseFileName = 'cameraman.tif';
elseif button == 2
baseFileName = 'moon.tif';
elseif button == 3
baseFileName = 'eight.tif';
elseif button == 4
baseFileName = 'coins.png';
else
baseFileName = 'pout.tif';
end
%===============================================================================
% Read in a standard MATLAB gray scale demo image.
folder = fileparts(which('cameraman.tif')); % Determine where demo folder is (works with all versions).
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the original gray scale image.
subplot(1, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Binarize the image
thresholdLevel = graythresh(grayImage);
binaryImage = im2bw(grayImage, thresholdLevel);
% Display the original gray scale image.
subplot(1, 2, 2);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize);
% Find the left-most binary pixel
[rows, columns] = find(binaryImage); % Coordinates of all the white pixels.
% Extract just the left most one.
xLeft = columns(1)
yLeft = rows(1)
% Plot it
hold on;
plot(xLeft, yLeft, 'r*', 'MarkerSize', 20, 'LineWidth', 3);
% Find the top-most binary pixel - transpose the image to do so.
[rows, columns] = find(binaryImage'); % Coordinates of all the white pixels.
% Extract just the left most one.
% I'ts different than before because the image was transposed.
xTop = rows(1)
yTop = columns(1)
% Plot it
hold on;
plot(xTop, yTop, 'r+', 'MarkerSize', 20, 'LineWidth', 3);

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!