opencv学习笔记---No2图片的几何变换

图片的几何变换

图片的简单变换不外乎有这么几种 :缩放,平移,镜像,剪切,旋转等。
昨天小编有提到图像都是由一个个像素点组成的,对图片进行操作等同于对每个像素点组成的矩阵进行操作。

缩放

opencv中的resize函数可以完成此功能
常见的缩放的方法有:最近邻域插值、双线性插值、像素关系重采样、立方插值
双线性插值计时通过横纵坐标确立新的像素点的位置。其实我们只要获得图片的宽和高就可以进行缩放处理,以缩小为例,原来的图像中的某一点坐标为(100,200),缩小至二分之一后就是(50,100)【注:我们默认等比例缩小】具体的代码实现如下:

import cv2
import numpy as np
image = cv2.imread("test.jpg")
imgInfo = image.shape
height = imgInfo[0]
width = imgInfo[1]
mode = imgInfo[2]
##图pain的缩放
reheight = int(height*0.5)
rewidth = int(width*0.5)
newimg = cv2.resize(image,(reheight,rewidth))  
#1:原始图片 2:缩放矩阵 
cv2.imshow("image",newimg)

效果图如下:
8645826-050713e13e08b936.PNG
缩放.PNG

平移

平移就是对像素点做坐标轴上的平移,比如说原来某一像素点为(20,150),我们可以对其进行任意方向的移动,具体实现到就是将像素矩阵做加减运算,可以用warpAffine函数 具体的代码实现如下:

matShift = np.float32([[1,0,100],[0,1,100]])# 2*3
dst = cv2.warpAffine(image,matShift,(height,width))
cv2.imshow("dst",dst)

剪切

剪切就是在整个大的矩阵中拿出一部分(必须是连续的哟)

'''
newing_2 = image[100:200,200:300]
cv2.imshow("image1",newing_2)
'''
8645826-567150f51e300cbe.PNG
剪切.PNG

镜像

镜像就是做镜面处理。


#图pain的景象
newImgInfo = (height*2,width,mode)
dst = np.zeros(newImgInfo,np.uint8)#uint8 
for i in range(0,height):
    for j in range(0,width):
        dst[i,j] = image[i,j]
        #x y = 2*h - y -1
        dst[height*2-i-1,j] = image[i,j]
for i in range(0,width):
    dst[height,i] = (0,0,255)#BGR
cv2.imshow('dst',dst)
cv2.waitKey(0)

明天将继续和大家分享。

猜你喜欢

转载自blog.csdn.net/weixin_34293911/article/details/86882968
今日推荐