MATLAB: How to find the co-ordinates in an image

imageimage processingImage Processing Toolboxocrpixel

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

Best 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));