python函数图像平移_[Python图像处理]六.图像缩放,图像旋转,图像翻转与图像平移...
圖像縮放
圖像縮放主要是調用resize()函數實現,result = cv2.resize(src, dsize[, result[.fx, fy[,interpolation]]])? 其中src表示原始圖像,dsize表示縮放大小, fx,fy也可以表示縮放大小倍數,他們兩個設置一個即可實現圖像縮放。
eg: result = cv2.resize(src, (160, 60))? | result = cv2.resize(src, None, fx=0.5, fy=0.5)
代碼如下:
importcv2deftest16():
src= cv2.imread("rose.jpg")#圖像縮放,設置的dsize是列數為200, 行數為100
result = cv2.resize(src, (200, 100))print(result.shape) #(100, 200, 3)
cv2.imshow("demo1", src)
cv2.imshow("demo2", result)if cv2.waitKey(0) == 27:
cv2.destroyWindow("demo1")
cv2.destroyWindow("demo2")
test16()
效果如下:
也可以獲取原始圖像像素再乘以縮放系數進行圖像轉換
代碼如下:
importcv2deftest17():
src= cv2.imread("rose.jpg")
rows, cols= src.shape[:2]#圖像縮放dsize(列,行)
result = cv2.resize(src, (int(cols*0.6), int(rows*0.4)))
cv2.imshow("src", src)
cv2.imshow("result", result)
cv2.waitKey(0)
cv2.destroyAllWindows()
test17()
效果如下:
(fx,fy)縮放倍數的方法對圖像進行放大或縮小
代碼如下:
importcv2deftest18():
src= cv2.imread("rose.jpg")#圖像縮放
result = cv2.resize(src, None, fx=0.3, fy=0.3)
cv2.imshow("src", src)
cv2.imshow("result", result)
cv2.waitKey(0)
cv2.destroyAllWindows()
test18()
效果如下:
圖像旋轉
圖像旋轉主要調用getRotationMatrix2D()函數和warpAffine()函數實現,繞圖像的中心旋轉,具體如下:
M = cv2.getRotationMatrix2D((cols/2, rows/2), 30, 1)
參數分別為:旋轉中心、旋轉度數、scale
rotated = cv2.warpAffine(src, M, (cols, rows))
參數分別為:原始圖像、旋轉參數、原始圖像寬高
代碼如下:
importcv2deftest19():
src= cv2.imread("rose.jpg")
rows, cols, channel=src.shape#繞中心旋轉
#參數:旋轉中心,旋轉度數,scale
M = cv2.getRotationMatrix2D((cols/2, rows/2), 30, 1)#參數: 原始圖像,旋轉參數, 元素圖像高度
rotated =cv2.warpAffine(src, M, (cols, rows))#顯示旋轉后的圖像
cv2.imshow("demo1", rotated)
cv2.waitKey(0)
cv2.destroyAllWindows()
test19()
效果如下:
備注: 參數中的旋轉度數設置為正數時表示逆時針旋轉,為負數時表示順時針旋轉
圖像翻轉
圖像翻轉在OpenCV中調用函數flip()實現,原型如下:
dst = cv2.flip(src, flipCode)
其中src表示原始圖像,flipCode表示翻轉方向,如果flipCode為0,則以X軸為對稱軸翻轉,如果fliipCode>0則以Y軸為對稱軸翻轉,如果flipCode<0則在X軸、Y軸方向同時翻轉。
代碼如下:
importcv2importmatplotlib.pyplot as pltdeftest20():#讀取圖片
img = cv2.imread('rose.jpg')
src=cv2.cvtColor(img, cv2.COLOR_BGR2RGB)#圖像翻轉
#0以X軸為對稱軸翻轉 >0以Y軸為對稱軸翻轉 <0 X軸Y軸翻轉
img1 =cv2.flip(src, 0)
img2= cv2.flip(src, 1)
img3= cv2.flip(src, -1)#顯示圖形
titles = ['Source', 'Image1', 'Image2', 'Image3']
images=[src, img1, img2, img3]for i in range(4):
plt.subplot(2, 2, i + 1), plt.imshow(images[i], 'gray')
plt.title(titles[i])
plt.xticks([]), plt.yticks([])
plt.show()
效果如下:
圖像平移
圖像平移首先定義平移矩陣M,再調用warpAffine()函數實現平移,核心函數如下:
M = np.float32([[1, 0, x], [0, 1, y]])? 其中(x,y)即為要偏移的x值,y值
shifted = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))
代碼如下:
importcv2importnumpy as npimportmatplotlib.pyplot as pltdeftest21():#讀取圖片
img = cv2.imread('rose.jpg')
image=cv2.cvtColor(img, cv2.COLOR_BGR2RGB)#圖像平移 下、上、右、左平移
M = np.float32([[1, 0, 0], [0, 1, 100]])
img1= cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))
M= np.float32([[1, 0, 0], [0, 1, -100]])
img2= cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))
M= np.float32([[1, 0, 100], [0, 1, 0]])
img3= cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))
M= np.float32([[1, 0, -100], [0, 1, 0]])
img4= cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))#顯示圖形
titles = ['Image1', 'Image2', 'Image3', 'Image4']
images=[img1, img2, img3, img4]for i in range(4):
plt.subplot(2, 2, i + 1), plt.imshow(images[i], 'gray')
plt.title(titles[i])
plt.xticks([]), plt.yticks([])
plt.show()
test21()
效果如下:
轉自:https://blog.csdn.net/Eastmount/article/details/82454335
總結
以上是生活随笔為你收集整理的python函数图像平移_[Python图像处理]六.图像缩放,图像旋转,图像翻转与图像平移...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python中索引是从什么开始_pyth
- 下一篇: github 修改项目为public_在