簡體   English   中英

使用Python進行圖像變形

[英]Image warping with Python

我需要在Python中扭曲較大尺寸(1679x1475)的圖像。 我有變換的坐標。 如何有效地將圖像變形為變換后的坐標系。 我嘗試了scipy.interpolate.griddata,但很快我的計算機內存不足。

為此,OpenCV帶有一個函數cv2.resize() 圖像的大小可以手動指定,也可以指定縮放比例。 使用了不同的插值方法。 cv2.INTER_AREA插值方法是cv2.INTER_AREA用於縮小, cv2.INTER_CUBIC (slow)cv2.INTER_LINEAR用於縮放。 默認情況下,出於所有調整大小的目的,使用的插值方法為cv2.INTER_LINEAR 您可以使用以下方法之一調整輸入圖像的大小:

import cv2
import numpy as np

img = cv2.imread('messi5.jpg')

res = cv2.resize(img,None,fx=2, fy=2, interpolation = cv2.INTER_CUBIC)

#OR

height, width = img.shape[:2]
res = cv2.resize(img,(2*width, 2*height), interpolation = cv2.INTER_CUBIC)

您需要scipy.ndimage.map_coordinates 您可以配置插值方法及其處理原始圖像之外的點的方式。 一個例子:

import numpy as np
from scipy import misc
#create a 2D array that has a grayscale image of a raccoon
face = misc.face(gray=True)

import matplotlib.pyplot as plt
plt.imshow(face,cmap=plt.cm.gray)

未變形的圖像看起來像這樣

#set up our new coordinate system
rows,cols = np.mgrid[0:768, 0:1024]
rows = rows**(1/2) * 767**(1/2)
cols = cols**(2) / 1023
rows = np.roll(rows,150,0)

from scipy import ndimage
#warp the image using a 3rd order (cubic) spline interpolation
new_img = ndimage.map_coordinates(face,[rows,cols], order=3)
plt.figure()
plt.imshow(new_img,cmap=plt.cm.gray)

變形后的圖像

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM