
封面
圖像幾何變換
圖片變換的本質(zhì)就是對圖片的矩陣進行操作
圖片位移
矩陣仿射變換,是一系列變換的基礎(chǔ)
圖片縮放
圖片縮放就是將圖片寬高信息進行改變
- 加載圖片
- 修改大小
- 放大 縮小 等比例縮放
def resize(img,param):
height,width,_ = get_img_info(img)
dstHeight = int(height * param)
dstWidth = int(height * param)
dst = cv2.resize(img,(dstWidth,dstHeight))
return dst
def get_img_info(img):
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
mode = imgInfo[2]
return height, width, mode

圖像縮放
img = cv2.imread('empire.jpeg',1)
dst = utils.resize(img,0.5)
cv2.imshow('resize img',dst)
cv2.waitKey(0)
在 opencv 中提供的有關(guān)縮放的方法
- 最近臨域插值
對于縮放后像素位置 x y 如果是小數(shù),可以按臨近進行取整 - 雙線性插值
當(dāng)我們進行縮放后得到點的 x y 都是小數(shù),這次在雙線插值法不是取最近點,而是按比例進行從周圍 4 個點進行取值。根據(jù)該點距離其周圍 4 點的比例來將周圍 4 點像素值作為目標(biāo)點。 - 像素關(guān)系重采樣
- 立方插值
def resize_nearby(img,scale):
height,width,_ = get_img_info(img)
dstHeight = int(height/scale)
dstWidth = int(height/scale)
# create empty
dstImage = np.zeros((dstHeight,dstWidth,3),np.uint8)
for i in range(0,dstHeight):# row
for j in range(0,dstWidth):#column
iNew = int(i*(height*1.0/dstHeight))
jNew = int(j*(width*1.0)/dstWidth)
dstImage[i,j] = img[iNew,jNew]
return dstImage
通過變換矩陣實現(xiàn)圖片縮放
def resize_with_mat(img,scale):
height,width,_ = get_img_info(img)
# tranform array
matTransform = np.float32([[scale,0,0],[0,scale,0]]) # 2 * 3
dst = cv2.warpAffine(img,matTransform,(int(width*scale),int(height*scale)) )
return dst
圖片剪切
其本質(zhì)是對數(shù)據(jù)進行操作,x 表示列信息 y 表示行信息。
def img_crop(img,startX,startY,h,w):
height,width,_ = get_img_info(img)
print(startX,(startX+h),startY,startY + w)
dst = img[startX:(startX+w),startY:(startY + h)]
# dst = img[100:200,100:300]
return dst

圖片裁剪
圖片鏡像
def img_mirror_v(img):
height,width,deep = get_img_info(img)
dstInfo = (height*2,width,deep)
dst = np.zeros(dstInfo,np.uint8)
for i in range(0,height):
for j in range(0,width):
dst[i,j] = img[i,j]
# x y
dst[height*2-i-1,j] = img[i,j]
for i in range(0,width):
dst[height,i] = (0,0,255) #BGR
return dst

圖片鏡像
圖片位移
了解 opencv 提供有關(guān)位移的 API,
這里 matTransform 就是偏移矩陣,由該矩陣來控制圖片的位置
[1,0,100],[0,1,200] 對該矩陣進行拆分為[1,0],[0,1]和[100],[200]
xy 為 C 矩陣
AC + B = [[1x + 0y],[0x + 1*y]] + [[100],[200]] = [[x+100],[y+200]]
def img_transform(img):
height,width,_ = get_img_info(img)
# tranform array
matTransform = np.float32([[1,0,100],[0,1,200]]) # 2 * 3
dst = cv2.warpAffine(img,matTransform,(width,height))
return dst

圖片平移
仿射變換
所謂仿射變換就是線性變換將平移,通過矩陣來控制 2D 圖像,
直線變換
- 變換前是直線的,變換后依然是直線
- 直線比例保持不變
- 變換前是原點的,變換后依然是原點
這里通過 左上角、左下角和右上角
def img_transform(img):
height,width,deep = get_img_info(img)
matSrc = np.float32([[0,0],[0,height-1],[width-1,0]])
matDst = np.float32([[50,50],[300,height-200],[width-300,100]])
# combination
matAffine = cv2.getAffineTransform(matSrc,matDst) # mat
dst = cv2.warpAffine(img,matAffine,(width,height))
return dst

圖片變換
圖片旋轉(zhuǎn)
有關(guān)圖像旋轉(zhuǎn)這里我們使用 opencv 提供 getRotationMatrix2D 來獲得一個矩陣來控制旋轉(zhuǎn),getRotationMatrix2D 接受三個參數(shù)分別為,
- 旋轉(zhuǎn)的中心點
- 旋轉(zhuǎn)的角度
- 縮放的比例
def img_rotate(img):
height,width,deep = get_img_info(img)
# mat rotate 1 center 2 angle 3 src
matRotate = cv2.getRotationMatrix2D((height*0.5,width*0.5),45,0.5)
dst = cv2.warpAffine(img,matRotate,(width,height))
return dst

圖片旋轉(zhuǎn)