Clear Filters
Clear Filters

how to get the value of a certain pixel-region in video and analyze them?

1 view (last 30 days)
I have a video of pills moving from left to right slowly. I want to draw a visual line in the original video and when the pills get through that line, the program automatically increase the pills-number by one, namely, n=n+1.
here is my code:
%read the video
avi = aviread('sample.avi');
video = {avi.cdata};
pixels = double(cat(4,video{1:end}))/255;
nFrames = size(pixels,4)
%convert the video to gray scale
for f = 1:nFrames
pixel(:,:,f) = (rgb2gray(pixels(:,:,:,f)));
end
background=(pixel(:,:,1)+pixel(:,:,2)+pixel(:,:,3))/3; %get a background picture
%transfer the video to binary video and counting
for l = 2:1:nFrames
dif(:,:,l)=(abs(pixel(:,:,l)-background));
bw(:,:,l) = im2bw(dif(:,:,l), 0.2);
**if (0==any(bw(:,314,l))) && (0~=any(bw(:,313,l)))
n=n+1;
disp(n)
end**
end
however I don't know why the condition "if (0==any(bw(:,314,l))) && (0~=any(bw(:,313,l)))" never get satisfied and the displayed "n" hence equals 0. I am badly in need of help. Thanks all of you.

Accepted Answer

Image Analyst
Image Analyst on 4 May 2012
Reread the help on any for how it handles 2D matrices. Then try something like this:
dif = abs(double(pixel(:,:,l))-background);
bw = im2bw(dif, 0.2);
if any(any(bw(:,314))) && ~any(any(bw(:,313)))
No need to have dif and bw be a 3D matrix depending on l (which is a bad choice for a variable name since "ell" looks like 1 ("one"). I'm still not sure about the "pixel" variable but I don't have time to look into it now.
  1 Comment
Stephen
Stephen on 4 May 2012
Thank you for your kind reply. In this program, I define "pixel" as the conversion result of the RGB video to gray video.Namely, the matrix of the gray video.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!