剪切图像而不裁剪 [英] Shear an image without cropping

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

问题描述

我正在尝试使用 python 对图像进行剪切变换.我正在使用 skimage (scikit-image),opencv 或类似的也可以完成我认为的工作.问题是每当我尝试使用仿射变换和扭曲(skimage)进行剪切时,图像就会出现裁剪"或剪切"(剪切"图像的某些部分丢失),只要剪切变换移动像素(涉及像素的翻译).我需要支持图像比例的画布",以便剪切"图像适合新图像但保留输入图像的所有信息(附加图像)

I am trying to do a shear transformation on images using python. I am using skimage (scikit-image), opencv or similar can also do the job i think. The problem is whenever I try to shear using affine transform and warp (skimage) the image appears "cropped" or "clipped" (some parts of the "sheared" image are lost) which has sense as far as shear transform moves pixels (involves a translation of pixels). I need that the "canvas" supporting the image scale so the "sheared" image fit in a new image but keeping all the information of the input image (image attached)

推荐答案

我遇到了同样的问题,我终于设法找到了一个可以很好地处理我测试的图像的解决方案.我使用的事实是我想对图像应用剪切、旋转和平移变换.

I got the same trouble and I finally manage to found a solution that work well with the images I tested. I was using the fact that I want to apply shear, rotation and translation transformation on the image.

  1. 首先,我们计算旋转所需的空间(不需要不使用时计算).
  2. 其次,我们为图像的最后一个像素计算必要的空间,该像素将通过剪切在图像中产生最大的平移.
  3. 第三,我们需要计算必要的平移,以补偿剪切将移动图像中心这一事实.

如果您希望所有图像的大小相同,请使用 45 度角,因为这个角度需要更多空间.

If you want all your images to be the same size, then use an angle of 45 degrees because it's the angle that need the more space.

import numpy as np
import cv2

#Parameters of the affine transform:
angle = 45; #Angle in degrees.
shear = 1;
translation = 5;

type_border = cv2.BORDER_CONSTANT;
color_border = (255,255,255);

original_image = cv2.imread(name_image_file);
rows,cols,ch = original_image.shape;

#First: Necessary space for the rotation
M = cv2.getRotationMatrix2D((cols/2,rows/2), angle, 1);
cos_part = np.abs(M[0, 0]); sin_part = np.abs(M[0, 1]);
new_cols = int((rows * sin_part) + (cols * cos_part)); 
new_rows = int((rows * cos_part) + (cols * sin_part));

#Second: Necessary space for the shear
new_cols += (shear*new_cols);
new_rows += (shear*new_rows);

#Calculate the space to add with border
up_down = int((new_rows-rows)/2); left_right = int((new_cols-cols)/2);

final_image = cv2.copyMakeBorder(original_image, up_down, up_down,left_right,left_right,type_border, value = color_border);
rows,cols,ch = final_image.shape;

#Application of the affine transform.
M_rot = cv2.getRotationMatrix2D((cols/2,rows/2),angle,1);
translat_center_x = -(shear*cols)/2;
translat_center_y = -(shear*rows)/2;

M = M_rot + np.float64([[0,shear,translation + translat_center_x], [shear,0,translation + translat_center_y]]);
final_image  = cv2.warpAffine(final_image , M, (cols,rows),borderMode = type_border, borderValue = color_border);

示例(我将cv2.copyMakeBorder"和cv2.getRotationMatrix2D"的边框设置为白色):角度 = -45 度.剪切 = -0.5

Example (where I put the border of "cv2.copyMakeBorder" and "cv2.getRotationMatrix2D" to white): Angle = -45 degrees. Shear = -0.5

这篇关于剪切图像而不裁剪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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