Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: locating colored pixels in an image

Subject: locating colored pixels in an image

From: Craig

Date: 17 Aug, 2007 19:36:53

Message: 1 of 4

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?

Subject: Re: locating colored pixels in an image

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 18 Aug, 2007 17:23:03

Message: 2 of 4

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

Subject: Re: locating colored pixels in an image

From: John D'Errico

Date: 18 Aug, 2007 19:51:07

Message: 3 of 4

"Craig " <hillx154@umn.edu> wrote in message
<fa4tcl$69q$1@fred.mathworks.com>...
> 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?

Except that R >= 200 is not a good criterion for a color
being red.

patch(rand(3,1),rand(3,1),[201 0 0]/255)
patch(rand(3,1),rand(3,1),[201 255 255]/255)
patch(rand(3,1),rand(3,1),[150 0 0]/255)
patch(rand(3,1),rand(3,1),[201 150 0]/255)
patch(rand(3,1),rand(3,1),[201 0 150]/255)

http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?
objectId=12326&objectType=FILE

John

Subject: Re: locating colored pixels in an image

From: us

Date: 18 Aug, 2007 20:53:58

Message: 4 of 4

Craig:
<SNIP sees red...

> I want to scan each column at a time and look for the red
pixels (R>200)...

one of the many solutions

% the data
% - assume rgb is a nxmx3 mat
% - extract the <red> plane
% m=rgb(:,:,1);
% - eg,
     m=[
0 0 203 0 0 0
201 0 0 0 207 210
202 0 0 204 0 211
0 0 0 205 208 212
0 0 0 206 209 0
     ];
% the engine
     [mf,mf]=histc(m,[200,inf]);
     mm=[0,mf(:).',0];
     ib=strfind(mm,[0,1]);
     ie=strfind(mm,[1,0]);
     il=ie-ib;
     rowx=mod(ib,size(m,1));
     colx=fix(ib/size(m,1))+1;
     r=[colx.',rowx.',il.'];
% the result
     m
     disp(sprintf('%% col/row %2d/%2d lenth %2d\n',r.'));

-but- carefully look at what <john d'errico> says...
us

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
strfind us 18 Aug, 2007 16:55:10
index us 18 Aug, 2007 16:55:10
indexing us 18 Aug, 2007 16:55:10
code us 18 Aug, 2007 16:55:10
histc us 18 Aug, 2007 16:55:10
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics