我是Matlab的新手。
我的老板给了我一个任务,我坚持到最后一步。
任务是: 用户应该在实时视频的快照中指定一个矩形,然后我应该检测每个视频帧中该矩形的平均颜色。
第一个用户使用以下代码在视频快照中指定矩形的边界:
test = getsnapshot(vid);
imwrite(test,'mytest.png','png');
testsnap = imread('mytest.png');
Rectpos=getrect;
然后我计算每个R , G , B
的平均度:
bbx=boundingboxPixels(testsnap,Rectpos(1),Rectpos(2),Rectpos(3),Rectpos(4));
rRect=mean(bbx(:,:,1));
gRect=mean(bbx(:,:,2));
bRect=mean(bbx(:,:,3));
其中boundingboxPixels
方法是这样的:
function pixel_vals = boundingboxPixels(img, x_init, y_init, x_width, y_width)
if x_init > size(img,2)
error('x_init lies outside the bounds of the image.'); end
if y_init > size(img,1)
error('y_init lies outside the bounds of the image.'); end
if y_init+y_width > size(img,1) || x_init+x_width > size(img,2) || ...
x_init < 1 || y_init < 1
warning([...
'Given rectangle partially falls outside image. ',...
'Resizing rectangle...']);
end
x_min = max(1, uint16(x_init));
y_min = max(1, uint16(y_init));
x_max = min(size(img,2), x_min+uint16(x_width));
y_max = min(size(img,1), y_min+uint16(y_width));
x_range = x_min : x_max;
y_range = y_min : y_max;
Upper = img( x_range, y_min , :);
Left = img( x_min, y_range, :);
Right = img( x_max, y_range, :);
Lower = img( x_range, y_max , :);
pixel_vals = [...
Upper
permute(Left, [2 1 3])
permute(Right, [2 1 3])
Lower];
end
然后从每个视频帧获得计算出的RGB
颜色的平均值和阈值:
tv=getdata(vid,1);//vid is real-time video
r=tv(:,:,1,1);
g=tv(:,:,2,1);
b=tv(:,:,3,1);
redVal = (r >= rRect-thr) & (r <= rRect+thr);
greenVal = (g >= gRect-thr) & (g <= gRect+thr);
blueVal = (b >= bRect-thr) & (b <= bRect+thr);
现在我该如何使用redVal , greenVal , blueVal
来检测这种颜色?
答案 0 :(得分:0)
&
来解决问题