一组图像的每个像素的中位数 [英] Median of each pixel of a set of images

查看:68
本文介绍了一组图像的每个像素的中位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算一组图像或视频"中每个像素的中位数.但是,当MATLAB开始计算该值时,它会花费很长时间,并且随机结束并出现索引错误.为什么?

I would like to calculate the median of each pixel in a set of images or "video". However, when MATLAB starts calculating this, it takes a very long time and finishes randomly with an index error. Why?

这是代码:

V = VideoReader('hall_monitor.avi');

info = get(V);

M = info.Width;  
N = info.Height;  

nb_frames_bk = 5;

v_pixel = zeros([nb_frames_bk 3]);

IB=zeros([M N 3],'double');

for i=1:M
    for j=1:N          
        for k=1:nb_frames_bk
             frm=read(V,k);
             v_pixel(k,:)=frm(i,j,:);
        end
      IB(i,j,:)=median(v_pixel(:,:));
    end
end


IB=uint8(IB);

imshow(IB);

推荐答案

此代码可从大量重构中受益.一方面,您正在重新阅读框架,而只阅读一次,存储它们并在完成后使用它们.

This code can benefit from a lot of refactoring. For one thing, you are re-reading frames when you can just read them once, store them and use them after you're done.

第二,遍历所有像素以计算中位数将非常慢.根据代码中的样子,对于第一个 nb_frames_bk 帧上的每个空间位置,您将收集这些帧内的所有RGB值,并计算RGB中值.

Secondly, iterating over all pixels to compute your median is going to be very slow. From what it looks like in your code, for each spatial position over the first nb_frames_bk frames, you collect all of the RGB values within these frames and calculate the median RGB value.

作为次要说明,由于定义的输出矩阵错误,因此出现尺寸超出错误的情况.您将其定义为 M x N ,其中 M 宽度,而 N 高度.这需要交换.请记住,首先将矩阵定义为高度,然后将其定义为宽度.但是,对于我建议正确实施此操作的建议,这是不必要的.

Also as a minor note, you are getting a dimension exceeds error because you defined the output matrix wrong. You defined it as M x N with M being the width and N being the height. This needs to be swapped. Remember that matrices are defined as height first, width second. However, this is unnecessary with what I'm going to suggest for implementing this properly.

指定一次帧的范围,而不是一次读取一帧.这样,您将获得一个4D矩阵,其中前三个维度引用图像,第四个维度表示帧号.然后,您可以在第四维中取中值,以找到所有帧的RGB中值.

Instead of reading the frames one at a time, specify a range of frames. This way, you will get a 4D matrix where the first three dimensions references an image, with the fourth dimension representing the frame number. You can then take the median in the fourth dimension to find the median RGB value over all frames.

换句话说,只需执行以下操作:

In other words, simply do this:

V = VideoReader('hall_monitor.avi');

nb_frames_bk = 5;

frms = read(V, [1 nb_frames_bk]);
IB = median(frms, 4);

imshow(IB);

这要好得多,并且可以保证更快.您也不需要获取每个帧的宽度和高度,因为我们不再需要遍历每个像素,因此不再需要它.

This is much better, to the point and guaranteed to be faster. You also don't need to obtain the width and height of each frame as it is no longer needed as we are no longer looping over each pixel.

这篇关于一组图像的每个像素的中位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆