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

标签 matlab image-processing matlab-cvst

我有一个视频,我在 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/

相关文章:

c++ - 从 LabVIEW 调用的 DLL 写入 MATLAB 文件

matlab - 从数组中提取 a<x<=b 的元素 x

image-processing - 测量图像中弯管长度的特征检测技术

matlab - 如何从从 Matlab 获取的深度图像和彩色图像生成 3D 点云

c++ - 使用刷新的输入从 C++ 调用 Matlab

matlab - 激光雷达数据的 delaunay 三角测量输出

matlab - 使用 Matlab 中的方法设置对象属性

linux - 是否可以从命令行对图像应用效果?

opencv - 从图像OpenCvSharp中提取线条

matlab - 在 MATLAB 中生成代码期间替代外部函数,例如 imread 和其他函数