我有一个视频,并已在MATLAB上为其制作了Sobel蒙版。现在,我必须通过for
循环读取每一帧,将Sobel蒙版应用于视频的每一帧。该过程类似于:
rgb2gray
。在这里,当我尝试在结果video.avi
文件上写入框架时应用了蒙版后,出现以下错误:
“类型的框架
double
必须在0到1的范围内”
我的代码有什么问题?我编写的代码如下所示:
vid = VideoReader('me.mp4');
frames = read(vid);
total = get(vid, 'NumberOfFrames');
write = VideoWriter('me.avi');
open(write);
mask1 = [-1 -2 -1; 0 0 0; 1 2 1]; % Horizontal mask
mask2 = [-1 0 1; -2 0 2; -1 0 1]; %Vertical Mask
for k = 1 : 125
image = frames(:,:,:,k);
obj = image;
obj1 = rgb2gray(obj);
obj2=double(obj1);
for row = 2 : size(obj2, 1) - 1
for col = 2 : size(obj2, 2) - 1
c1 = obj2(row - 1, col - 1) * mask1(1 ,1);
c2 = obj2(row - 1, col) * mask1(1 ,2);
c3 = obj2(row - 1, col + 1) * mask1(1 ,3);
c4 = obj2(row, col - 1)*mask1(2, 1);
c5 = obj2(row, col)*mask1(2, 2);
c6 = obj2(row, col + 1)*mask1(2, 3);
c7 = obj2(row + 1, col - 1)*mask1(3,1);
c8 = obj2(row + 1, col)*mask1(3,2);
c9 = obj2(row + 1, col + 1)*mask1(3,3);
c11 = obj2(row - 1, col - 1)*mask2(1 , 1);
c22 = obj2(row, col - 1)*mask2(2, 1);
c33 = obj2(row + 1, col - 1)*mask2(3, 1);
c44 = obj2(row -1, col)*mask2(1, 2);
c55 = obj2(row, col)*mask2(2 , 2);
c66 = obj2(row +1, col)*mask2(2 , 3);
c77 = obj2(row - 1, col + 1)*mask2(1 , 3);
c88 = obj2(row, col +1)*mask2(2 , 3);
c99 = obj2(row + 1, col + 1)*mask2(3 , 3);
result = c1 + c2 + c3 +c4 +c5+ c6+ c7+ c8 +c9;
result2 = c11 + c22 + c33 + c44 + c55 + c66 + c77 + c88 + c99;
%result = double(result);
%result2 = double(result2);
rim1(row, col) = ((result^2+result2^2) *1/2);
rim2(row, col) = atan(result/result2);
end
end
writeVideo(write, rim2); %This line has the problem with rim2 as rim2 is the frame i'm trying to write on the video file.
end
close(write);
rim2
[-pi/2, pi/2]
末尾具有范围,该范围与期望[0,1]范围的写入功能不兼容。[0,1]
使用mat2gray
函数将其转换为范围,即
writeVideo(write, mat2gray(rim2));
然后,您的代码将按预期工作(在我的计算机上确认)。
顺便说一句,这不会影响您的代码,但可能是您打算这样做im2double(A)
而不是double(A)
。前者会生成[0,1]范围内的“适当”灰度图像,而后者只是将uint8
[0,255]范围内的图像转换为double
格式(即[0.0,255.0])。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句