Python 剪切图像而不剪切
我正在尝试使用python对图像进行剪切变换。我正在使用skimage(scikit图像),opencv或类似工具也可以完成我认为的工作。问题是,每当我尝试使用仿射变换和扭曲(略读)进行剪切时,图像就会出现“裁剪”或“剪裁”(被剪切图像的某些部分丢失),这对于剪切变换移动像素(涉及像素的平移)是有意义的。我需要“画布”支持图像缩放,以便“剪切”图像适合新图像,但保留输入图像的所有信息(图像附加)Python 剪切图像而不剪切,python,scikit-image,Python,Scikit Image,我正在尝试使用python对图像进行剪切变换。我正在使用skimage(scikit图像),opencv或类似工具也可以完成我认为的工作。问题是,每当我尝试使用仿射变换和扭曲(略读)进行剪切时,图像就会出现“裁剪”或“剪裁”(被剪切图像的某些部分丢失),这对于剪切变换移动像素(涉及像素的平移)是有意义的。我需要“画布”支持图像缩放,以便“剪切”图像适合新图像,但保留输入图像的所有信息(图像附加) 我也遇到了同样的问题,我终于找到了一个能很好地处理我测试的图像的解决方案。我想在图像上应用剪切、旋转
我也遇到了同样的问题,我终于找到了一个能很好地处理我测试的图像的解决方案。我想在图像上应用剪切、旋转和平移变换
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
奇怪的是,在你的图像中,我对负角度和剪切不起作用,你使用了哪个版本的cv?@Ilya.K:在2018年的那个时候,可能是OpenCV 3.4版或其他版本(不过那是很久以前的事了)。如何做到这一点的原则应该保持不变,但可能它们在功能上改变了一些东西。