Path: news.mathworks.com!newsfeed-00.mathworks.com!NNTP.WPI.EDU!elk.ncren.net!newsflash.concordia.ca!canopus.cc.umanitoba.ca!not-for-mail
From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)
Newsgroups: comp.soft-sys.matlab
Subject: Re: locating colored pixels in an image
Date: Sat, 18 Aug 2007 17:23:03 +0000 (UTC)
Organization: National Research Council Canada - Conseil national de rechereches Canada
Lines: 45
Message-ID: <fa79tn$756$1@canopus.cc.umanitoba.ca>
References: <fa4tcl$69q$1@fred.mathworks.com>
NNTP-Posting-Host: origin.ibd.nrc.ca
X-Trace: canopus.cc.umanitoba.ca 1187457783 7334 192.70.172.160 (18 Aug 2007 17:23:03 GMT)
X-Complaints-To: abuse@cc.umanitoba.ca
NNTP-Posting-Date: Sat, 18 Aug 2007 17:23:03 +0000 (UTC)
Originator: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)
Xref: news.mathworks.com comp.soft-sys.matlab:424410


In article <fa4tcl$69q$1@fred.mathworks.com>, Craig  <hillx154@umn.edu> wrote:
>I have an image (3000x2000 pixels).  I want to scan each 
>column at a time and look for the red pixels (R>200).  Most 
>pixels are just black.  When it finds a red pixel, I want 
>it to search forward and find how many consecutive red 
>pixels (R>200) there are.  Any ideas?  

There are many ways of doing this. This is just a small variant
on finding the boundaries of "runs", which in one form or another
ends up getting asked about every 10 days.

*One* possible method:

If the original image is A, then construct

B = [zeros(1,size(A,2)), A(:,:,1)>200, zeros(1,size(A,2))];

This would be 3002 x 2000 (x1)

Then,

C = B(2:end,:) xor B(1:end-1,:)

C would be 3001 x 2000 .

For any particular column C(:,K) the first 1 in the column marks
the beginning of the first run of red at that same offset into
A(:,K) -- e.g., if the first 1 in C(:,K) was in row 5, then A(5,K)
is the first red pixel. The second 1 in the column C(:,K) marks
the beginning of the first run of *non*-red, so if the second 1
in the column C(:,K) were at (say) position 17, then the last
red element in the run was at A(16,K). The pattern continues
with the first 1 of each pair marking the beginning of a red run,
and the second 1 of each pair marking one past the last red in
the run. If C(3001,K) is set then A(3000,K) was a red pixel.
If you encounter a pair C(N,K) and C(N+1,K) that are both 1s,
then A(N,K) was a single red pixel -- you can see then why it is
important that the end-marker be one past the end of the run,
so you can find the "runs" of length 1.

To find the boundaries, you can iterate over each column K and
use find(C(:,K)). Finding the boundaries in a vectorized
way is left as an exercise for the reader ;-)
-- 
  All is vanity.                                       -- Ecclesiastes