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?
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
"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.
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.