From: ImageAnalyst on
Nehal:
All right, here is a demo for you using your image. Note that this
demo is many, many times longer than the handful of lines it takes to
actually do the work because I had to fix up the image you posted to
binarize it and get rid of the white border. There are also many
lines in there strictly for illustrative demo purposes to display
results and help you understand it better. The actual code would
actually be only about 4 or 5 lines. (And I give credit to Walter
Roberson for the tip about using the any() function.)
-ImageAnalyst

clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;

% Read in a user's hand.
folder = 'C:\Documents and Settings\userName\My Documents\Temporary
stuff';
baseFileName = 'samplekl.png';
fullFileName = fullfile(folder, baseFileName);
grayImage = imread(fullFileName);
% Display the original gray scale image.
subplot(2, 3, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
set(gcf, 'Position', get(0,'Screensize')); % Enlarge figure to full
screen.

% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
% The user posted a color image, so convert it to monochrome;
grayImage = rgb2gray(grayImage);
end
% The user has a white border around the image, let's crop it
grayImage2 = imcrop(grayImage, [83 31 639 479]);
% Create a binary image.
binaryImage = grayImage2 > 0;
% Display the original gray scale image.
subplot(2, 3, 2);
imshow(binaryImage, []);
title('Cropped Binary Image', 'FontSize', fontSize);

% Finally, now we have the image that the user should have posted to
begin with.


horizontal = any(grayImage2, 1);
subplot(2, 3, 3);
bar(horizontal);
title('Pixels in Columns', 'FontSize', fontSize);

vertical = any(grayImage2, 2);
subplot(2, 3, 4);
bar(vertical);
title('Pixels in Rows', 'FontSize', fontSize);

% Find the first and last columns and rows.
column1 = find(horizontal, 1, 'first');
column2 = find(horizontal, 1, 'last');
row1 = find(vertical, 1, 'first');
row2 = find(vertical, 1, 'last');

% Display the rectangle over the original image.
subplot(2, 3, 5);
imshow(binaryImage, []);
hold on;
xBox = [column1 column2 column2 column1 column1];
yBox = [row1 row1 row2 row2 row1];
plot(xBox, yBox);
title('Bounding rectangle', 'FontSize', fontSize);

% Crop the image
croppedImage = binaryImage(row1:row2, column1:column2);
subplot(2, 3, 6);
imshow(croppedImage, []);
title('cropped image', 'FontSize', fontSize);

From: Nehal on
ImageAnalyst <imageanalyst(a)mailinator.com> wrote in message <281f274d-f45d-479a-9b8d-9759651a37c1(a)z10g2000yqb.googlegroups.com>...
> Nehal:
> All right, here is a demo for you using your image. Note that this
> demo is many, many times longer than the handful of lines it takes to
> actually do the work because I had to fix up the image you posted to
> binarize it and get rid of the white border. There are also many
> lines in there strictly for illustrative demo purposes to display
> results and help you understand it better. The actual code would
> actually be only about 4 or 5 lines. (And I give credit to Walter
> Roberson for the tip about using the any() function.)
> -ImageAnalyst

thanks a lot... the code is working... but i need to make clear something...

1. the image was taken by a faulty webcam... i'll try to collect some good samples... and i'll again check and post here if it's working.

2. the image was already converted to binary... i've converted it and then posted it... so why i am converting it grb2gray again in the code...?
I mean the image is already converted rgb2gray and then gray2binary... so why i am assuming that the image is in rgb and then again going though the conversion precess..?

3. after sub-plotting the converted binary image... i am getting the same image except the white border... but isn't it me who is creating the white border in the 1st place...? can't i do it without doing that...?
I mean i am creating the white border then cropping out the image with the black background only... but the image was already there without the white border... so why can't i use that...?

i'll take some samples with a good web-cam hopefully within 24hrs... and then i'll again post here...

and i am really really grateful for your help... thanks you very much.. :)
From: ImageAnalyst on
I don't know how it came to be a 24 bit RGB image with a white
border. Possibly you saved out the figure, not the image array, and
the figure is a color figure by default so whatever function you used
to save it saved it as a 24 bit BMP format. That may also explain the
white border. If you have access to the original binary image, then
you don't have to worry about all that code and can just start using
the original binary image immediately. I HAD to do all that because
you didn't provide me with the original binary image.

You shouldn't get the same image except for the white border. It
should be cropped in close to the hand. Try downloading your image
from the web site (like I did) and trying it with that and you'll
see. Otherwise look at the horizontal and vertical arrays and look at
the first few elements, then try to figure out why they are not zero.
If they're not zero, then there must be some non-zero pixels near the
top or left edge of your image.
From: Nehal on
ImageAnalyst <imageanalyst(a)mailinator.com> wrote in message <65818ceb-c0eb-4dd6-9fa2-99e4fde5f43d(a)x27g2000yqb.googlegroups.com>...
> I don't know how it came to be a 24 bit RGB image with a white
> border. Possibly you saved out the figure, not the image array, and
> the figure is a color figure by default so whatever function you used
> to save it saved it as a 24 bit BMP format. That may also explain the
> white border. If you have access to the original binary image, then
> you don't have to worry about all that code and can just start using
> the original binary image immediately. I HAD to do all that because
> you didn't provide me with the original binary image.
>
> You shouldn't get the same image except for the white border. It
> should be cropped in close to the hand. Try downloading your image
> from the web site (like I did) and trying it with that and you'll
> see. Otherwise look at the horizontal and vertical arrays and look at
> the first few elements, then try to figure out why they are not zero.
> If they're not zero, then there must be some non-zero pixels near the
> top or left edge of your image.

i think the image was converted automatically when i was uploading it in imageshack web site...
anywaz... i have taken some samples today with a new web cam... but your code is not working... i tried the code line by line from the top... but didn't work... then i tried to do from the line...
"% Finally, now we have the image that the user should have posted to begin with."
with declaring the grayImage & binaryImage variable properly... but it still doesn't work...

after putting the following command... it shows a blank sub-plot space...

% Create a binary image.
binaryImage = grayImage2 > 0;
% Display the original gray scale image.
subplot(2, 3, 2);
imshow(binaryImage, []);

but no binary image... can you plz help me...?

and i am sharing the new sample here: http://www.mediafire.com/?tolmjzyiuzn
i have uploaded it to a file sharing site so that the image does not change and i have posted the original grb image.

the image i posted above has zero in the first few horizontal and vertical arrays...

so plz can u help me now...? how to crop this image...?
From: ImageAnalyst on
Because you're starting with a totally different type of image it of
course required some slight modifications:

clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;

% Read in a user's hand.
folder = 'C:\Documents and Settings\tk2013\My Documents\Temporary
stuff';
baseFileName = 'sample.tif';
fullFileName = fullfile(folder, baseFileName);
grayImage = imread(fullFileName);
% Display the original gray scale image.
subplot(2, 3, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
set(gcf, 'Position', get(0,'Screensize')); % Enlarge figure to full
screen.

% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
% The user posted a color image, so convert it to monochrome;
grayImage = rgb2gray(grayImage);
end
% The user has a white border around the image, let's crop it
% grayImage = imcrop(grayImage, [83 31 639 479]);
% Create a binary image.
binaryImage = grayImage == 255;
% Display the original gray scale image.
subplot(2, 3, 2);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize);

horizontal = any(binaryImage, 1);
subplot(2, 3, 3);
plot(horizontal, 'ro-');
title('Pixels in Columns', 'FontSize', fontSize);

vertical = any(binaryImage, 2);
subplot(2, 3, 4);
plot(vertical, 'ro-');
title('Pixels in Rows', 'FontSize', fontSize);

% Find the first and last columns and rows.
column1 = find(horizontal, 1, 'first');
column2 = find(horizontal, 1, 'last');
row1 = find(vertical, 1, 'first');
row2 = find(vertical, 1, 'last');

% Display the rectangle over the original image.
subplot(2, 3, 5);
imshow(binaryImage, []);
hold on;
xBox = [column1 column2 column2 column1 column1];
yBox = [row1 row1 row2 row2 row1];
plot(xBox, yBox);
title('Bounding rectangle', 'FontSize', fontSize);

% Crop the image
croppedImage = binaryImage(row1:row2, column1:column2);
subplot(2, 3, 6);
imshow(croppedImage, []);
title('cropped image', 'FontSize', fontSize);