我試圖從圖像中提取輪廓,旋轉這些輪廓並將它們插入到新圖像中。代碼如下。 我的問題是旋轉輪廓方法。執行代碼時會看到以下錯誤「TypeError:'cv.cvseq'對象不支持項目分配」。在python中更改CvSeq中的元素
如何解決這個問題的任何想法?我爲Opencv 2.2使用python綁定。
import cv
def rotateContour(contour, centerOfMass, angle):
for index in range(0, len(contour)):
contour[index] = rotatePoint(contour[index], centerOfMass, angle)
return contour
def rotatePoint(point, centerOfMass, angle):
px, py = point
x, y = centerOfMass
temppoint = (px-x, py-y)
temppointx = temppoint[0]*math.cos(angle) + temppoint[1] * math.sin(angle)
temppointy = temppoint[1]*math.cos(angle) - temppoint[0] * math.sin(angle)
temppoint = (temppointx + x, temppointy + y)
return temppoint
inputimage = cv.LoadImage('filename.png', cv.CV_LOAD_IMAGE_GRAYSCALE)
outputimage = cv.CreateImage((10000, 300), 8, 1)
storage = cv.CreateMemStorage (0)
contours = cv.FindContours(inputimage, storage, cv.CV_RETR_EXTERNAL, cv.CV_CHAIN_APPROX_SIMPLE)
for contour in contour_iterator(contours):
gray = cv.CV_RGB(200, 200, 200)
# Rotate contour somehow
contour = rotatecontour(contour)
cv.DrawContours(outputimage, contour, gray, gray, 0, -1, 8)
cv.SaveImage("outputfile.png", outputimage)