如何使用matlab中的blockproc使用8x8矩阵量化图像?

时间:2016-06-25 13:30:32

标签: matlab image-processing

这个想法来自jpeg压缩,你可以将量化矩阵(8x8)块式地应用于(余弦变换的)图像。

funQY = @(block_struct) (block_struct.data)./quantY;
QY = blockproc(dctY, [8 8], funQY);

dctY是表示图片亮度的二维矩阵(2856x4290)。 quantY是相应的量化矩阵(8x8)。

错误消息表明矩阵尺寸不一致。 block_struct.data不代表dctY的8x8块吗?如果是这样,为什么它不能被8x8矩阵划分?这里我的错误是什么?

1 个答案:

答案 0 :(得分:2)

作为@beaker noted in a comment,原因是您的图片大小不能与块的大小整除。这是一个简单的例子,我们在blockproc中检查传递给函数的每个块的大小:

dctY = rand(24,34); % first dim is divisible, second is not
funtmp = @(block_struct) size(block_struct.data);
blockproc(dctY,[8 8],funtmp)

返回

ans =

     8     8     8     8     8     8     8     8     8     2
     8     8     8     8     8     8     8     8     8     2
     8     8     8     8     8     8     8     8     8     2

正如您在最右侧所看到的,每个块行中的最后一个块是一个大小为[8, 2]的部分块。

您可以使用the PadPartialBlocks option of blockproc

'PadPartialBlocks'  A logical scalar.  When set to true, blockproc will
                    pad partial blocks to make them full-sized (M-by-N)
                    blocks.  Partial blocks arise when the image size
                    is not exactly divisible by the block size.  If
                    they exist, partial blocks will lie along the right
                    and bottom edge of the image.  The default is
                    false, meaning the partial blocks are not padded,
                    but processed as-is.

                    blockproc uses zeros to pad partial blocks when
                    necessary.

这导致

>> blockproc(dctY,[8 8],funtmp,'padpartialblocks',true)

ans =

     8     8     8     8     8     8     8     8     8     8
     8     8     8     8     8     8     8     8     8     8
     8     8     8     8     8     8     8     8     8     8

根据您的应用,您可能最好修剪掉最后一个部分块,而不是使用给定值填充。您可以选择使用TrimBorderBorderSize选项执行此操作,这些选项会对称地修剪像素。

相关问题