python opencv对图像进行旋转且不裁剪图片的方法
2017-12-20 16:08
2526 查看
最近在做深度学习时需要用到图像处理相关的操作,在度娘上找到的图片旋转方法千篇一律,旋转完成的图片都不是原始大小,很苦恼,于是google到歪果仁的网站扒拉了一个方法,亲测好用,再次嫌弃天下文章一大抄的现象,虽然我也是抄歪果仁的。废话不多说了,直接贴代码了。
def rotate_bound(image, angle):
# grab the dimensions of the image and then determine the
# center
(h, w) = image.shape[:2]
(cX, cY) = (w // 2, h // 2)
# grab the rotation matrix (applying the negative of the
# angle to rotate clockwise), then grab the sine and cosine
# (i.e., the rotation components of the matrix)
M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0)
cos = np.abs(M[0, 0])
sin = np.abs(M[0, 1])
# compute the new bounding dimensions of the image
nW = int((h * sin) + (w * cos))
nH = int((h * cos) + (w * sin))
# adjust the rotation matrix to take into account translation
M[0, 2] += (nW / 2) - cX
M[1, 2] += (nH / 2) - cY
# perform the actual rotation and return the image
return cv2.warpAffine(image, M, (nW, nH))
其他的不用多说了吧,第一个参数穿opencv读取的图像,第二个参数传入需要旋转的角度,enjoy!
def rotate_bound(image, angle):
# grab the dimensions of the image and then determine the
# center
(h, w) = image.shape[:2]
(cX, cY) = (w // 2, h // 2)
# grab the rotation matrix (applying the negative of the
# angle to rotate clockwise), then grab the sine and cosine
# (i.e., the rotation components of the matrix)
M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0)
cos = np.abs(M[0, 0])
sin = np.abs(M[0, 1])
# compute the new bounding dimensions of the image
nW = int((h * sin) + (w * cos))
nH = int((h * cos) + (w * sin))
# adjust the rotation matrix to take into account translation
M[0, 2] += (nW / 2) - cX
M[1, 2] += (nH / 2) - cY
# perform the actual rotation and return the image
return cv2.warpAffine(image, M, (nW, nH))
其他的不用多说了吧,第一个参数穿opencv读取的图像,第二个参数传入需要旋转的角度,enjoy!
相关文章推荐
- 5 用python进行OpenCV实战之图像变换2(旋转)
- python3-opencv库(3)--图片颜色空间转换,利用HSV进行物体跟踪,图像通道分离与合并
- Python-OpenCv之图像基本处理-平移,缩放,旋转,翻转,裁剪及仿射变换
- 5 用python进行OpenCV实战之图像变换2(旋转)
- 使用python进行图像处理-调整图片大小
- Python通过PIL获取图片主要颜色并和颜色库进行对比的方法
- 在Python下利用OpenCV来旋转图像的教程
- ply python 图片压缩 图片裁剪 旋转
- [IOS OpenCV]摄像头采集图片用 CreateIplImageFromUIImage 转换后图像被旋转、变形解决方案
- python简单实现旋转图片的方法
- 【转】 Qt绘图,显示图片图像,平移,缩放,旋转和扭曲图片的方法 声明:本
- python加opencv进行图像处理
- [转]openCV: 线性插值方法进行图像放大
- 使用python进行图像处理的基本方法
- python使用PyGame绘制图像并保存为图片文件的方法
- 拍照/从相册读取图片后进行裁剪的方法
- python让图片按照exif信息里的创建时间进行排序的方法
- python从网络读取图片并直接进行处理的方法
- [python + opencv] 图像的旋转和分块
- python从网络读取图片并直接进行处理的方法