2014-06-12 94 views
3

我需要使用python剪切和傾斜一些圖像。 我遇到過this skimage module,但我似乎無法完全理解我應該如何使用它。歪斜或剪切python中的圖像

我試過幾件事情,顯然給了我錯誤,因爲後來我突然意識到,我沒有將我的圖像傳遞給函數。然後我注意到該函數並不把我的圖像作爲輸入參數。那麼如何應用這種轉變呢?或者,這甚至是正確的功能,以便歪斜或剪切圖像?

+1

[PIL](https://pillow.readthedocs.org/en/1.7.8/pythondoc-PIL.ImageTransform.html)和[仿射變換](http://en.wikipedia.org/wiki)/Affine_transformation)? –

+3

看看http://scikit-image.org/docs/dev/auto_examples/applications/plot_geometric.html –

回答

8

如果你想使用skimage模塊操作順序是:

  • 加載圖像或定義數據與
  • 工作創建轉換你想
  • 應用transformatioin

工作流程可能如下所示:

from skimage import io 
from skimage import transform as tf 

# Load the image as a matrix 
image = io.imread("/path/to/your/image.jpg") 

# Create Afine transform 
afine_tf = tf.AffineTransform(shear=0.2) 

# Apply transform to image data 
modified = tf.warp(image, inverse_map=afine_tf) 

# Display the result 
io.imshow(modified) 
io.show() 

來自skimage模塊的AffineTransform類接受transformation matrix作爲其第一個參數(如果您使用其他參數,則該類將構造該類)。

+0

這個例子實際上應該使用'from skimage import io; image = io.imread('path/to/image.jpg')'或類似'image = data.lena()'的東西。 'data.imread'只存在,因爲它是從'io'模塊導入以讀取圖像。不過,很好的例子! –

+0

謝謝,正是我需要的。只是一件事 - 在我的代碼中,我使用'img = color.rgb2gray(io.imread(「path_to_my_image」))''和'ImageViewer'來查看圖像。否則,使用'matplotlib.pyplot'生成一個圖像,顯示所有基色的陰影。 – user961627

+0

感謝您的反饋! – MrAlias