2014-09-23 65 views
3

我有8个图像,我想以下面显示的缩放空间格式显示它们。原始图像的高度和宽度为256.然后在原始图像的右侧,每一层的大小减少2.像这里一样,图像的高度和宽度是256.在原始图像的右侧,高度和宽度是128 ,64,32,16,8,4,2.在MATLAB中显示图像缩放空间

我有所有的图像各自的尺寸。我只是想知道如何根据下面显示的模式排列图像。提前致谢。

enter image description here

回答

4

这就像你正在试图建立一个尺度空间和显示结果给用户。这不是一个问题。请记住,您必须使用for循环执行此操作,因为我没有看到您将如何执行此操作,除非您复制并粘贴多行代码。其实,我会用while循环,我会告诉你为什么很快。

在任何情况下,您都需要声明一个输出图像,其行数与原始图像一样多,但这些列将是原始图像的1.5倍以适应右侧的图像。

首先,编写代码将原始图像放置在左侧,将版本放在右侧一半。一旦你这样做了,你就编写一个for循环,使用索引将图像放在正确的位置,直到用完缩放,并且需要跟踪下一个图像开始和下一个图像结束的位置。请记住,第一个子采样后的下一个图像的起始位置将从原始图像的列位置开始,并且该行位于前一个图像结束的位置。作为一个例子,我们使用cameraman.tif图片,该图片恰好为256 x 256,但我会编写代码,以便它符合您想要的任何图像分辨率。当我对图像进行二次采样时,我将在MATLAB中使用imresize来帮助调整图像大小,并且我将指定一个采样因子0.5来表示子采样2。我之所以会使用while循环代替,是因为因为我们可以保持循环并调整大小,直到调整大小的图像的尺寸之一是1。在这种情况下,没有更多的规模需要处理,所以我们可以退出。

这样:

%// Read in image and get dimensions 
im = imread('cameraman.tif'); 
[rows,cols] = size(im); 

%// Declare output image 
out = zeros(rows, round(1.5*cols), 'uint8'); 
out(:,1:cols) = im; %// Place original image to the left 

%// Find first subsampled image, then place in output image 
im_resize = imresize(im, 0.5, 'bilinear'); 
[rows_resize, cols_resize] = size(im_resize); 
out(1:rows_resize,cols+1:cols+cols_resize) = im_resize; 

%// Keep track of the next row we need to write at 
rows_counter = rows_resize + 1; 

%// For the rest of the scales... 
while (true) 
    %// Resize the image 
    im_resize = imresize(im_resize, 0.5, 'bilinear'); 
    %// Get the dimensions 
    [rows_resize, cols_resize] = size(im_resize); 
    %// Write to the output 
    out(rows_counter:rows_counter+rows_resize-1, cols+1:cols+cols_resize) = ... 
     im_resize; 

    %// Move row counter over for writing the next image 
    rows_counter = rows_counter + rows_resize; 

    %// If either dimension gives us 1, there are no more scales 
    %// to process, so exit. 
    if rows_resize == 1 || cols_resize == 1 
     break; 
    end 
end 

%// Show the image 
figure; 
imshow(out); 

这是我得到的图像:

enter image description here

+0

我得到的黑色图像输出。而且,所有缩放的图像都存在于L {}数组中。我在上面的帖子中添加了你的代码。你能告诉一下吗? – nikhilk 2014-09-23 04:00:51

+2

@nikhilk - 您的代码有效。但是,你将它完全变黑的原因是因为你的图像是'double'并且在'[0,1]'之间进行了规范化。我认为你的图像是'uint8',所以预期的范围在'[0,255]'之间。如果你输出'uint8',并且你的强度介于'[0,1]'之间,那么你将不会看到任何东西!因此,将'out'声明改为这个:'out = zeros(rows,round(1.5 * cols));'。 – rayryeng 2014-09-23 04:23:57

+0

哦,明白了。谢谢:) – nikhilk 2014-09-23 04:29:37