gpt4 book ai didi

matlab - double类型的帧必须在0到1的范围内 : MATLAB

转载 作者:行者123 更新时间:2023-12-01 22:22:46 24 4
gpt4 key购买 nike

我有一个视频,我在 MATLAB 上为它制作了一个 Sobel 掩码。现在我必须通过 for 循环读取每一帧,在视频的每一帧上应用 Sobel 掩码。这个过程是这样的:

  • 第 1 步:阅读框架。
  • 第 2 步:使用 rgb2gray 将其转换为灰度。
  • 第 3 步:将其转换为 double。

在这里,当我尝试在生成的 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);

最佳答案

rim2 的末尾有 [-pi/2, pi/2] 范围,这与需要 [0,1] 范围的写入函数不兼容.使用 mat2gray 函数将其转换为 [0,1] 范围,即

writeVideo(write, mat2gray(rim2));

然后您的代码将按预期工作(在我的机器上确认)。

顺便说一句,这不会影响您的代码,但您可能打算执行 im2double(A) 而不是 double(A)。前者生成 [0,1] 范围内的“正确”灰度图像,而后者只是将 [0,255] 范围内的 uint8 图像转换为 double 格式(即 [0.0, 255.0]).

关于matlab - double类型的帧必须在0到1的范围内 : MATLAB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38944816/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com