我遇到了图像问题。我正在尝试拍摄图像并将其设为原始大小的 1/4,然后将其作为 2x2 矩阵重复。有点像这样:
Given an input image:
---------------------
| |
| |
| IMG |
| |
| |
---------------------
Firstly, shrink it to 1/4 of its original size:
-----------
| img |
| |
|---------|
Then concatenate it into a "2x2 array" of the shrunken image:
---------------------
| img | img |
| | |
|---------|---------|
| img | img |
| | |
---------------------
令我苦恼的是我不知道如何将它变成 2x2 数组。有什么建议么?这一次实际上不是 HW :) 这是为了学习目的。到目前为止,这是我尝试过的:
function[newImg] = immultiply(picture)
Image = imread(picture); %// Reads in the picture
[r, c, l] = size(Image); %// Finds the images rows, columns and layers
rows = round(r ./ 2); %// Divides up the rows
columns = round(c ./ 2); %// Divides up the columns
newImg = cat(3,rows,columns,3); %// Creates my image, but just gives me a blank thing
imshow(newImg)
end
我会在进一步处理时进行更新。谢谢!
最佳答案
前两个答案是正确的,我最初只是在玩这个,并不打算发布它,但我认为稍微修改一下,它解决了 Luis Mendo 对 Kamtal 的答案的部分评论。
我最初的想法是,为什么要丢弃数据?如果您将采样缩小到 1/4 大小但绘制了其中的 4 个,则您有足够的空间容纳所有数据:
img1 = imread('myimage.png');
subplot(1,2,1),imshow(img1)
img2 = img1([1:2:end, 2:2:end], [1:2:end, 2:2:end]);
subplot(1,2,2),imshow(img2);
生成的 img2
左上象限将正是 Kamtal 的答案产生的结果:具有奇值 x 和 y 坐标的像素的最近邻插值。其他 3 个将是(偶数/奇数)、(奇数/偶数)、(偶数/偶数)。 img1
中的每个像素都出现在 img2
中,并且每个子图像都可能略有不同。
相反,如果我们想将 4 个图像中的每一个的数据组合成一个图像而不丢弃所有 数据,我们可以稍微改变一下。我们只取 4 张图像的平均值。请注意,这里的 img2
与上面的相同,我只是分解了计算以使其显而易见。
img1 = imread('myimage.png');
subplot(1,3,1),imshow(img1)
img2a = img1(1:2:end, 1:2:end);
img2b = img1(1:2:end, 2:2:end);
img2c = img1(2:2:end, 1:2:end);
img2d = img1(2:2:end, 2:2:end);
img2 = [img2a img2b; img2c img2d];
subplot(1,3,2),imshow(img2);
img3a = (img2a + img2b + img2c + img2d)/4;
img3 = [img3a img3a; img3a img3a];
subplot(1,3,3),imshow(img3);
这里 img3
显示了 4 张相同的图像,所有图像都是使用均值滤波对原始图像进行下采样的结果。
要使用过滤器执行此操作,您将使用内核:
[0.25 0.25]
[0.25 0.25]
这只是取附近 4 个元素的平均值。内核的原点位于 (1,1),您的插值、下采样图像将像以前一样位于奇数行/列中:
img1 = imread('myimage.png');
subplot(1,2,1),imshow(img1)
h = [0.25, 0.25; 0.25, 0.25] //% define the mean filter kernel
img2a = imfilter(img1, h); //% filter before applying Kamtal's solution
img2b = img2a(1:2:end, 1:2:end);
img2 = [img2b img2b; img2b img2b];
subplot(1,2,2),imshow(img2);
生成的图像应该与上面的 img3
相同。
(顺带一提,用2x2的kernel进行均值滤波然后下采样到1/4大小本质上是双线性插值。imresize
默认使用双三次插值,所以其结果会略有不同。)
关于image - 调整和重复图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26935377/