我有一个视频,我在MATLAB上为它制作了Sobel蒙板。现在我必须通过for
循环读取每一帧来在视频的每个帧上应用Sobel掩码。该过程是这样的:double类型的框架必须在0到1的范围内:MATLAB
- 第1步:阅读框架。
- 第2步:使用
rgb2gray
将其转换为灰度。 - 第3步:将其转换为双精度。
这里,将面膜当我尝试写在框架上产生的video.avi
文件后,我收到以下错误:
"Frames of type
double
must be in the range of 0 to 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);
感谢您的帮助,您对'mat2gray'的建议奏效。但我想理解所有这些事情的概念,所以如果你能给我一点时间,我想和你聊聊。 –
当然,我很乐意。感谢您接受我的回答;我怀疑你比@ rayryeng更喜欢它,因为它很短,并直接指向了这个问题(即“解决了问题”),这是我的贡献意图;然而rayryeng已经给你提供了有关你的任务中涉及的问题和概念的令人惊讶的帮助分解;你至少可以通过加强他的贡献并传播一些爱(即奖励声誉:))来承认他的努力。)理解所涉及的概念所需的所有背景信息都很好地展现出来。 (雷:对不起,并不意味着要偷:p) –
不,不,我并不是想让@rayryeng失望或伤害。毫无疑问,他的建议是伟大的,但我对我的感觉是,虽然我对他的代码有一些想法(但无法理解所有),但我很难理解,这是我的问题,因为我不擅长图像处理,但可能是这是我无法理解他的代码的原因。你的建议很短,所以我只是把它放在我的代码中,这就是为什么我要求清除我的概念。 –