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

1 次查看(过去 30 天)
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.

采纳的回答

Image Analyst
Image Analyst 2012-5-4
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 个评论
Stephen
Stephen 2012-5-4
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.

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by