2017-04-01 18 views
1

私はthe documentation for cv2.transformを理解していません。誰かが同情して、(Pythonで)どうか説明してくれますか?opencv(Python)の点のリストに変換行列を適用する

私はそれが

# this is clumsy, I have to extend each vector, 
# apply the matrix to each extended vector, 
# and turn the list of transformed vectors into an numpy array 
extendedpolygon = np.hstack([polygon,np.ones((3,1))]) 
rotatedpolygon = np.int32([M.dot(x) for x in extendedpolygon]) 
cv2.fillConvexPoly(img,rotatedpolygon,(127)) 

私が好きな描く

import numpy as np 
import cv2 

dx,dy = 400,400 
centre = dx//2,dy//2 
img = np.zeros((dy,dx),np.uint8) 

# construct a long thin triangle with the apex at the centre of the image 
polygon = np.array([(0,0),(100,10),(100,-10)],np.int32) 
polygon += np.int32(centre) 

# draw the filled-in polygon and then rotate the image 
cv2.fillConvexPoly(img,polygon,(255)) 
M = cv2.getRotationMatrix2D(centre,20,1) # M.shape = (2, 3) 
rotatedimage = cv2.warpAffine(img,M,img.shape) 

は、私は、最初のポリゴンを回転させたい画像を、ポリゴンを描画でそれを埋めて、回転コードを持っています1つの関数呼び出しで行いますが、それを正しく取得できません。

# both of these calls produce the same error 
cv2.transform(polygon,M) 
cv2.transform(polygon.T,M) 
# OpenCV Error: Assertion failed (scn == m.cols || scn + 1 == m.cols) in transform, file /home/david/opencv/modules/core/src/matmul.cpp, line 1947 

私はそれがフレーズ "src - m.colsまたはm.cols-1と同じ数のチャネル(1〜4)が必要な入力配列です。私は多角形だと思った。別々のチャンネルとしてxとyの部分を持つ3つの座標が適格となるが、悲しいことではない。

私は、同じ質問がC++に求められていることを知っています。私はPythonで回答したいと思います。

+0

これはヒントでしたか? M = cv2.getRotationMatrix2D(center、20,1)#M.shape =(2,3) 私はポリゴンの各ベクトルにエントリを追加しなければならなかったのです計算することができました [拡張ポリゴンのxのM.dot(x)] M.shape =(2,3)はM.row = 2、M.col = 3を意味すると私は考えています。エラーメッセージです。 –

+0

は、ポイントの書式設定の例です。私はpython/numpyをよく知らないので、フォーマットを解釈するのを手伝ってはいけません... http://dsp.stackexchange.com/questions/38626/image-preprocessing-for-facial-detection-embedding-clustering-パイプラインは、あなたが探しているものでなければなりません。 – Micka

+1

ありがとうミカ、私は使用の例を探したが、見つけられなかった。 –

答えて

2

実例を指摘してくれたミカに感謝します。ここに私の質問に対する答えを提供するコードがあります。 cv2.fillConvexPoly()は最初の3つの答えを受け付けますが、非常に寛容であるように思わ、私はまだこれらの答えは、ドキュメントにマップする方法を理解していない理由を

import numpy as np 
import cv2 

dx,dy = 400,400 
centre = dx//2,dy//2 
img = np.zeros((dy,dx),np.uint8) 

# construct a long thin triangle with the apex at the centre of the image 
polygon = np.array([(0,0),(100,10),(100,-10)],np.int32) 
polygon += np.int32(centre) 

# draw the filled-in polygon and then rotate the image 
cv2.fillConvexPoly(img,polygon,(255)) 
M = cv2.getRotationMatrix2D(centre,20,1) # M.shape = (2, 3) 
rotatedimage = cv2.warpAffine(img,M,img.shape) 

# as an alternative, rotate the polygon first and then draw it 

# these are alternative ways of coding the working example 
# polygon.shape is 3,2 

# magic that makes sense if one understands numpy arrays 
poly1 = np.reshape(polygon,(3,1,2)) 
# slightly more accurate code that doesn't assumy the polygon is a triangle 
poly2 = np.reshape(polygon,(polygon.shape[0],1,2)) 
# turn each point into an array of points 
poly3 = np.array([[p] for p in polygon]) 
# use an array of array of points 
poly4 = np.array([polygon]) 
# more magic 
poly5 = np.reshape(polygon,(1,3,2)) 

for poly in (poly1,poly2,poly3,poly4,poly5): 
    newimg = np.zeros((dy,dx),np.uint8) 
    rotatedpolygon = cv2.transform(poly,M) 
    cv2.fillConvexPoly(newimg,rotatedpolygon,(127)) 

は、正直に言うと、私は理解していません。

関連する問題