How to find the co-ordinates in an image?

In the attached image,how do i find the co-ordinates of first letter(i.e. starting balck pixel of 'a') and co-ordinates of last letter?(i.e.last pixel of letter 'e') Please help me Thank you

 Accepted Answer

Try this:
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 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 = pwd;
baseFileName = 'oneline.png';
% 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(2, 2, 1);
imshow(grayImage, []);
axis on;
title('Original Grayscale 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')
% Let's compute and display the histogram.
[pixelCount, grayLevels] = imhist(grayImage);
subplot(2, 2, 2);
bar(grayLevels, pixelCount); % Plot it as a bar chart.
grid on;
title('Histogram of original image', 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('Gray Level', 'FontSize', fontSize);
ylabel('Pixel Count', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Get a horizontal profile
horizontalProfile = sum(grayImage, 1) / rows;
% Threshold the image and create a binary image
subplot(2, 2, 3);
x = 1:length(horizontalProfile);
plot(x, horizontalProfile, 'b-', 'LineWidth', 2); % Plot it as a line.
grid on;
title('Profile of original image', 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('Column Number', 'FontSize', fontSize);
ylabel('Gray Level', 'FontSize', fontSize);
% Threshold at 240
theThreshold = 240;
inAWord = find(horizontalProfile < theThreshold)
% Draw a red line over the plot
hold on;
plot([x(1), x(end)], [theThreshold, theThreshold], 'r-', 'LineWidth', 2); % Plot it as a line.
% Determine the left and right-most columns:
leftColumn = inAWord(1);
rightColumn = inAWord(end);
% Display the original gray scale image.
subplot(2, 2, 4);
imshow(grayImage, []);
axis on;
title('Image with detected words', 'FontSize', fontSize, 'Interpreter', 'None');
% Put up shaded areas over words
hold on;
for k = 1 : length(inAWord)
col = inAWord(k);
fill([col, col], [1, rows], 'y', 'FaceAlpha', 0.1, 'EdgeColor', 'y');
end
message = sprintf('The left column = %d\nThe right column = %d', leftColumn, rightColumn);
uiwait(helpdlg(message));

9 Comments

It worked! Thanks a lot
so is it (1,230) and (1690,250) are the co-ordinates of first and last letter resp?
hello sir. I tried the above code given by you to get the co-ordinates. But where are the co-ordinates?means I want it in the form of (x,y)
y doesn't really apply since the input was already one line that had been extracted. So y1 = 1, and y2 = number of rows in the image.
For x, that's the column number and those are in the array called "inAWord."
sir,i tried yo code.do u mean that y1=1 and y2 = 1690 .. and the array contains column 1 through 1334..how can i get the value of x1 and x2??
No, from what I can see on the left hand side axes, the y1 was 1 and the y2 was about 110 or so.
And there are lots of x1 and x2, right? There is a pair of starting and stopping x for every "word". The inAWord array has an index for every x location that is in a word. If you want the starting and stopping x, you'd have to use something like diff() to get just those points. Something like (untested):
logicalIndexes = horizontalProfile < theThreshold
startingX = 1 + find(diff(logicalIndexes) > 0);
endingX = 1 + find(diff(logicalIndexes) < 0);
sir,when i inserted those lines which u gave its displaying logicalIndexes values columns from through through 1703.but not getting anything when tried to display endingX
What is endingX? Maybe later today I can download the image and code and try it for you. No guarantees though.
endingX = 1 + find(diff(logicalIndexes) < 0); This one which you gave,...And i would be grateful for that.Please do try when you are free

Sign in to comment.

More Answers (0)

Categories

Find more on Convert Image Type 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!