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.
0 件のコメント
採用された回答
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.
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!