Path: news.mathworks.com!not-for-mail
From: "Kenneth Galea" <k.galea@hotmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Matlab and Otsu Thresholding query
Date: Fri, 5 Feb 2010 14:57:04 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 49
Message-ID: <hkhbk0$e85$1@fred.mathworks.com>
References: <hkgur9$c47$1@fred.mathworks.com> <cce067d5-87e1-4435-9ec8-f8a114c7c816@21g2000yqj.googlegroups.com> <4e2e9862-9014-476e-95b7-db349024b01e@h2g2000yqj.googlegroups.com>
Reply-To: "Kenneth Galea" <k.galea@hotmail.com>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1265381824 14597 172.30.248.37 (5 Feb 2010 14:57:04 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 5 Feb 2010 14:57:04 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 2146944
Xref: news.mathworks.com comp.soft-sys.matlab:604745

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <4e2e9862-9014-476e-95b7-db349024b01e@h2g2000yqj.googlegroups.com>...
> Look at the color detection demo - it has right in there some code to
> threshold a range.  You'd do something like
> 
> tolerance = 2; % Gray levels.
> binaryImageRed = (subtractedImageRed < tolerance) &
> (subtractedImageRed > tolerance);
> same for green and red, then and them together
> binaryImage = binaryImage Red & binaryImageGreen & binaryImageBlue;
> 
> labeledImage = bwconncomp(binaryImage);
> measurements = regionprops(labeledImage);
> 
> Or, for divided images,
> tolerance = 0.2; % Gray levels.
> binaryImageRed = (dividedImageRed < (1.0 - tolerance)) &
> (dividedImageRed > (1+tolerance));
> etc.
> 
> You'll have to subtract or divide every color channel one at a time.
> redPlane = double(rgbImage(:, :, 1));
> greenPlane = double(rgbImage(:, :, 2));
> bluePlane = double(rgbImage(:, :, 3));
> do this for both your test image, and your background image, so that
> you have two red images, two green images, and two blue images.  Then
> subtract or divide them.

I understood the concept. You are great. I tried it out but I'm having a minor problem. Maybe a small error in my code. This is my code:
background = imread('C:\Users\Kenneth\Desktop\test3.jpg');
test = imread('C:\Users\Kenneth\Desktop\test3.jpg');
redPlane_test = double(test(:, :, 1));
greenPlane_test = double(test(:, :, 2));
bluePlane_test = double(test(:, :, 3)); 

redPlane_background = double(background(:, :, 1));
greenPlane_background = double(background(:, :, 2));
bluePlane_background = double(background(:, :, 3));

binaryImageRed =  redPlane_test - redPlane_background;
binaryImageGreen = greenPlane_test - greenPlane_background;
binaryImageBlue = bluePlane_test - bluePlane_background;
binaryImage = binaryImageRed & binaryImageGreen & binaryImageBlue

imshow(binaryImage)

In this case since my test image is background (to check if it becomes white) it's becoming totally black:/ It's surely a small bug in my code grrr!! Pls help

Thanks
Kenneth