Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
RGB to Binary Image Conversion (Only the Large White Region)

Subject: RGB to Binary Image Conversion (Only the Large White Region)

From: Nehal

Date: 21 Oct, 2010 16:19:04

Message: 1 of 10

I am trying to convert a RGB image into Binary. The object is a white-gloved hand and the background is linear black. So it is easier to convert the image into binary and get the proper shape of the hand.

The object: http://img209.imageshack.us/i/17889045.png/

I have converted the above image into binary using both the codes:
binaryImage = im2bw (imageArray);
and
binaryImage = im2bw(imageArray, graythresh(imageArray));

both the codes gives me a binary image something like this: http://img844.imageshack.us/i/20897654.png/

There is a small region which is also converted into binary which was not the part of the glove: http://img294.imageshack.us/i/88380532.jpg/
The region is red circled.
It was my hand, without the glove in the original image. While converting into binary, that small region was also converted into binary.

Now I do not want this to be happen. I want only the gloved part of my hand to be white and the rest of the part including the small hand part (skin color) and background to be black.

Could anyone help me..?

Subject: RGB to Binary Image Conversion (Only the Large White Region)

From: ImageAnalyst

Date: 21 Oct, 2010 16:58:29

Message: 2 of 10

Just try thresholding the blue channel:

clc;
clear;
close all;
workspace;
fontSize = 20;

% Change the current folder to the folder of this m-file.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end
% Read in a standard MATLAB gray scale demo image.
folder = 'C:\Documents and Settings\user\My Documents\downloads';
baseFileName = '17889045.png';
fullFileName = fullfile(folder, baseFileName);
rgbImage = imread(fullFileName);
% Display the original color image.
subplot(2, 2, 1);
imshow(rgbImage);
title('Original Grayscale Image', 'FontSize', fontSize);
set(gcf, 'Position', get(0,'Screensize')); % Enlarge figure to full
screen.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')

blueChannel = rgbImage(:,:,3);
% Display the blue channel image.
subplot(2, 2, 2);
imshow(blueChannel, []);
title('Blue Channel of the Image', 'FontSize', fontSize);

binaryImage = blueChannel > 200;
% Display the binary image.
subplot(2, 2, 3);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize);

Subject: RGB to Binary Image Conversion (Only the Large White Region)

From: Nehal

Date: 21 Oct, 2010 17:08:03

Message: 3 of 10

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <5267c53a-56c8-4103-86ed-83669d0da37c@h7g2000yqn.googlegroups.com>...
> Just try thresholding the blue channel:
>
> binaryImage = blueChannel > 200;

how did you get this "200" value..? have you calculated it for this image only..? or it is something like a standard value..?
if you have calculated the value for this image only, i need to know how you did it coz i have almost 1400 images.

Subject: RGB to Binary Image Conversion (Only the Large White Region)

From: ImageAnalyst

Date: 21 Oct, 2010 18:41:59

Message: 4 of 10

I thresholded it in Photoshop until I found a threshold that looked
reasonable. If you need it to be automatic, then that's where the art
comes in. You have to select a thresholding method that will work
with all of your other images. I find graythresh (Otsu's method)
rarely good. I find the triangle method often good, especially for
skewed Gaussian looking histograms, which are quite common. Maybe you
can give this a try:
http://www.mathworks.com/matlabcentral/fileexchange/28047-gray-image-thresholding-using-the-triangle-method

Or maybe you can convert to HSV color space and look for objects that
have both a low S value and a high V value. Your skin will have a
high S value so it should not be selected. See my demos for more
examples of HSV thresholding:
http://www.mathworks.com/matlabcentral/fileexchange/28512-simple-color-detection-by-hue
http://www.mathworks.com/matlabcentral/fileexchange/28972-custom-shaped-histogram

Subject: RGB to Binary Image Conversion (Only the Large White Region)

From: Nehal

Date: 22 Oct, 2010 06:10:03

Message: 5 of 10

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <270de36d-be42-4403-ad6d-84ff048e3212@e14g2000yqe.googlegroups.com>...
> I thresholded it in Photoshop until I found a threshold that looked
> reasonable. If you need it to be automatic, then that's where the art
> comes in. You have to select a thresholding method that will work
> with all of your other images. I find graythresh (Otsu's method)
> rarely good. I find the triangle method often good, especially for
> skewed Gaussian looking histograms, which are quite common. Maybe you
> can give this a try:
> http://www.mathworks.com/matlabcentral/fileexchange/28047-gray-image-thresholding-using-the-triangle-method
>

using the first method, I have managed to convert the image into binary something like this: http://img155.imageshack.us/img155/6080/binary2.jpg
but still it is not good enough because the gloved part of the hand is not visible.

any suggestion on this..?

Subject: RGB to Binary Image Conversion (Only the Large White Region)

From: Nehal

Date: 28 Oct, 2010 17:02:04

Message: 6 of 10

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <5267c53a-56c8-4103-86ed-83669d0da37c@h7g2000yqn.googlegroups.com>...
> blueChannel = rgbImage(:,:,3);
> binaryImage = blueChannel > 200;

could you please tell me what it actually does by this 2 line..? this is working fine.. but i just need to know how it's working...

Subject: RGB to Binary Image Conversion (Only the Large White Region)

From: Sean

Date: 28 Oct, 2010 17:16:04

Message: 7 of 10

"Nehal " <arnab620@yahoo.com> wrote in message <iacaac$fgi$1@fred.mathworks.com>...
> ImageAnalyst <imageanalyst@mailinator.com> wrote in message <5267c53a-56c8-4103-86ed-83669d0da37c@h7g2000yqn.googlegroups.com>...
> > blueChannel = rgbImage(:,:,3);
> > binaryImage = blueChannel > 200;
>
> could you please tell me what it actually does by this 2 line..? this is working fine.. but i just need to know how it's working...

IA's variable names are very good.
The first line extracts the 3rd slice of the 3-dimensional matrix. I.e. all of the rows, all of the columns and only the third slice. In an RGB image the third slice corresponds to the B, blue.

So blueChannel is the intensities for all pixels. The bluer the area the higher the intensity. This leads to the second step where he keeps the blue areas that are above a certain level (200). I.e. the really blue (or purple brown etc) parts. The results is a binary image telling you where it's blue (==1) and where it's not blue (==0).

Subject: RGB to Binary Image Conversion (Only the Large White Region)

From: Steven_Lord

Date: 28 Oct, 2010 17:20:30

Message: 8 of 10



"Nehal " <arnab620@yahoo.com> wrote in message
news:iacaac$fgi$1@fred.mathworks.com...
> ImageAnalyst <imageanalyst@mailinator.com> wrote in message
> <5267c53a-56c8-4103-86ed-83669d0da37c@h7g2000yqn.googlegroups.com>...
>> blueChannel = rgbImage(:,:,3);
>> binaryImage = blueChannel > 200;
>
> could you please tell me what it actually does by this 2 line..? this is
> working fine.. but i just need to know how it's working...

The first is using subscripted indexing to extract a piece of the variable
rgbImage. You might want to read through the Matrices and Arrays section of
the Getting Started guide for more information about indexing:

http://www.mathworks.com/help/techdoc/learn_matlab/f2-8955.html

This section from the regular documentation goes into more details about
indexing, once you've read the Getting Started section above.

http://www.mathworks.com/help/techdoc/math/f1-85462.html

The second line just uses the regular relational operator greater-than.

http://www.mathworks.com/help/techdoc/ref/relationaloperators.html

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: RGB to Binary Image Conversion (Only the Large White Region)

From: Nehal

Date: 29 Oct, 2010 12:39:03

Message: 9 of 10

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <iacb4k$a5t$1@fred.mathworks.com>...
> "Nehal " <arnab620@yahoo.com> wrote in message <iacaac$fgi$1@fred.mathworks.com>...
> > ImageAnalyst <imageanalyst@mailinator.com> wrote in message <5267c53a-56c8-4103-86ed-83669d0da37c@h7g2000yqn.googlegroups.com>...
> > > blueChannel = rgbImage(:,:,3);
> > > binaryImage = blueChannel > 200;
> >
> > could you please tell me what it actually does by this 2 line..? this is working fine.. but i just need to know how it's working...
>
> IA's variable names are very good.
> The first line extracts the 3rd slice of the 3-dimensional matrix. I.e. all of the rows, all of the columns and only the third slice. In an RGB image the third slice corresponds to the B, blue.
>
> So blueChannel is the intensities for all pixels. The bluer the area the higher the intensity. This leads to the second step where he keeps the blue areas that are above a certain level (200). I.e. the really blue (or purple brown etc) parts. The results is a binary image telling you where it's blue (==1) and where it's not blue (==0).

Thank You.

Subject: RGB to Binary Image Conversion (Only the Large White Region)

From: Nehal

Date: 29 Oct, 2010 12:49:04

Message: 10 of 10

"Steven_Lord" <slord@mathworks.com> wrote in message <iacbcu$qr5$1@fred.mathworks.com>...
>
>
> "Nehal " <arnab620@yahoo.com> wrote in message
> news:iacaac$fgi$1@fred.mathworks.com...
> > ImageAnalyst <imageanalyst@mailinator.com> wrote in message
> > <5267c53a-56c8-4103-86ed-83669d0da37c@h7g2000yqn.googlegroups.com>...
> >> blueChannel = rgbImage(:,:,3);
> >> binaryImage = blueChannel > 200;
> >
> > could you please tell me what it actually does by this 2 line..? this is
> > working fine.. but i just need to know how it's working...
>
> The first is using subscripted indexing to extract a piece of the variable
> rgbImage. You might want to read through the Matrices and Arrays section of
> the Getting Started guide for more information about indexing:
>
> http://www.mathworks.com/help/techdoc/learn_matlab/f2-8955.html
>
> This section from the regular documentation goes into more details about
> indexing, once you've read the Getting Started section above.
>
> http://www.mathworks.com/help/techdoc/math/f1-85462.html
>
> The second line just uses the regular relational operator greater-than.
>
> http://www.mathworks.com/help/techdoc/ref/relationaloperators.html
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com
Thank You.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us