缩放和裁剪图像(矩阵) [英] Zoom and Crop Image (Matrix)

查看:389
本文介绍了缩放和裁剪图像(矩阵)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个与缩放和裁剪图像有关的问题。

I have 2 questions that pertains to zooming and cropping of an image.

1)放大图像,使图像的高度和高度都增加一倍宽度并且矩阵中的每个像素被复制为4个像素。想知道这是否可以在不使用任何循环或插值的情况下完成并且函数可以实现。

1) Zooming into an image that would make the image twice as large in both height and width and that each pixel in the matrix is replicated to become 4 pixels. Was wondering if this can be accomplished without using any loops or interpolations and the function imresize.

2)通过选择图像的某个区域然后裁剪矩阵来裁剪矩阵到一定的长度和高度。再次,我想知道如何在不使用任何循环或插值的情况下实现这一点,并且函数可以实现。也许从矩阵中删除某个列和行?

2) Cropping a matrix by selecting a certain area of the image and then cropping it to a certain length and height. Again, I'm wondering how this can be accomplished without using any loops or interpolations and the function imresize. Maybe by removing a certain column and row from the matrix?

任何有关如何为这些创建函数的帮助都将非常感激。 :)

Any help on how to create a function for these would be greatly appreciated. :)

推荐答案

让我们一次回答每个问题。

Let's answer each question one at a time.

您的问题陈述说您要放大,但您所描述的是一个简单的大小调整。我要做两件事。

Your problem statement says that you want to zoom in, yet what you're describing is a simple resizing. I'm going to do both.

第一点,你要求的是像素重复。最简单的方法是声明输出图像大小是输入图像大小的两倍,然后编写将每个输入像素复制到输出的代码,将其复制到右侧,底部和右下方。例如,使用MATLAB系统路径中的 onion.png ,您可以:

For the first point, what you are seeking is pixel duplication. The easiest thing to do would be to declare an output image that is twice the size of the input image, then write code that will duplicate each input pixel to the output where it gets copied to the right, bottom and bottom right. For example, using onion.png from the MATLAB system path, you would do:

im = imread('onion.png');
rows = size(im,1);
cols = size(im,2);
out = zeros(2*rows, 2*cols, size(im,3), class(im));
out(1:2:end,1:2:end,:) = im; %// Left
out(2:2:end,1:2:end,:) = im; %// Bottom
out(1:2:end,2:2:end,:) = im; %// Right
out(2:2:end,2:2:end,:) = im; %// Bottom-Right

注意我们索引数组的方式,我们跳过一个像素,起始位置会根据您要复制像素的位置而改变。

Note that the way we are indexing into the array, we are skipping over one pixel, and the starting position changes depending on where you want to copy the pixels.

这是原始图像:

以下是最终结果:

BTW,通常当你增加图像的大小时,你进行上采样,你通常会低通过滤结果来执行抗锯齿

BTW, usually when you increase the size of an image, you are upsampling, you usually low-pass filter the result to perform anti-aliasing.

现在,如果你想要放大到一个部分,您所要做的就是从上采样图像中选择您想要的部分并裁剪它。这将导致您的下一个问题。

Now if you want to zoom in to a portion, all you have to do is choose a portion that you want from the upsampled image and crop it. This leads to your next question.

这可以通过索引来完成。给定要提取的左上角的行和列位置,以及要裁剪的宽度和高度,您只需执行此操作即可。 r c 是左上角的行和列, w h 是裁剪结果的宽度和高度:

This can simply be done by indexing. Given a row and column location of the top-left corner of where you want to extract, as well as the width and height of what you want to crop, you simply do this. r and c are row and columns of the top-left corner and w and h are the width and height of the cropped result:

out = im(r:r+h-1, c:c+w-1,:);

假设(r,c)=(50,50)(w,h)=(50,50)。对于我们的 onion.png 图片,我们得到:

Let's say (r,c) = (50,50) and (w,h) = (50,50). For our onion.png image, we get:

r = 50; c = 50;
h = 50; w = 50;
out = im(r:r+h-1, c:c+w-1,:);

如果您想将裁剪后的图像放在原始图像的另一个位置,您只需重复上述步骤,但是输出将分配给原始图像中的位置。给定 r2 c2 作为原始图像中保存图像的左上角,做:

If you wanted to place the cropped image in another position in the original image, you would simply repeat the procedure above, but the output will be assigned to locations in the original image. Given r2 and c2 to be the top-left corner of where you want to save the image to in the original image, do:

im(r2:r2+h-1, c2:c2+w-1, :) = out;

这篇关于缩放和裁剪图像(矩阵)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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