在Python中更改CvSeq中的元素

2 投票
1 回答
2200 浏览
提问于 2025-04-16 12:37

我正在尝试从一张图片中提取轮廓,然后把这些轮廓旋转后放到一张新图片里。下面是我的代码。

我遇到的问题是在旋转轮廓的方法上。当我运行代码时,出现了一个错误:“TypeError: 'cv.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)

1 个回答

1

看起来你不能通过Python的接口来直接改变cvseq对象里的元素(注意,这里提到的Python方法列表只提供了删除序列元素和改变它们顺序的方法)。

不过,Python的接口还是提供了一些工具,可以帮助你实现旋转图像中轮廓的目标。

因为cv.DrawContours()方法需要一个cvseq作为输入,所以我们得找其他方法来绘制轮廓,先在Python中存储和处理它们。一种方法是使用cv.FillPoly()cv.DrawPoly()方法(这两者都需要一个包含元组列表的列表作为输入),具体使用哪个取决于你传给cv.DrawContours()的厚度参数是-1还是大于0。

所以,找到轮廓并绘制它们旋转后的样子的一种方法如下(这里通过重新绘制填充形式来找到每个轮廓的质心,并使用OpenCV的矩函数):

import cv
import numpy as np

# Draw contour from list of tuples.
def draw_contour( im , contour , color , thickness = 1 , linetype = 8 ,
                  shift = 0 ) :
  if thickness == -1 :
    cv.FillPoly( im , [contour] , color , linetype , shift )
  else :
    cv.PolyLine( im , [contour] , True , color , thickness , linetype , shift )

# Rotate contour around centre point using numpy.
def rotate_contour( contour , centre_point , theta ) :
  rotation = np.array( [ [ np.cos( theta ) , -np.sin( theta ) ] , 
                         [ np.sin( theta ) ,  np.cos( theta ) ] ] )
  centre = np.vstack( [ centre_point ] * len( contour ) )
  contour = np.vstack( contour ) - centre
  contour = np.dot( contour , rotation ) + centre
  return [ tuple ( each_row ) for each_row in contour ]

# Find centre of mass by drawing contour in closed form and using moments.
def find_centre_of_mass( contour ) :
  bottom_right = np.max( contour , axis = 0 )
  blank = cv.CreateImage( tuple ( bottom_right ) , 8 , 1 )
  cv.Set( blank , 0 )
  draw_contour( blank , contour , 1, -1 )
  moments = cv.Moments( blank , 1 )  
  sM00 = float ( cv.GetSpatialMoment( moments , 0 , 0 ) )
  sM01 = float ( cv.GetSpatialMoment( moments , 0 , 1 ) )
  sM10 = float ( cv.GetSpatialMoment( moments , 1 , 0 ) )
  return ( sM10 / sM00 , sM01 / sM00 )

THETA = np.pi / 3.0
COLOR = cv.CV_RGB( 200 , 200 , 200 )
input_image = cv.LoadImage( ‘filename.png’ , cv.CV_LOAD_IMAGE_GRAYSCALE )
output_image = cv.CreateImage( ( input_image.width , input_image.height ) , 
                                 input_image.depth , input_image.nChannels )
cv.Set( output_image , 0 )

storage = cv.CreateMemStorage( 0 )
contour_pointer = cv.FindContours( input_image , storage , 
                                   cv.CV_RETR_EXTERNAL , 
                                   cv.CV_CHAIN_APPROX_SIMPLE )

while contour_pointer is not None :
  contour = contour_pointer [ : ]
  centre_of_mass = find_centre_of_mass( contour )
  rotated_contour = rotate_contour( contour , centre_of_mass , THETA )
  draw_contour( output_image , rotated_contour , COLOR , -1 )
  contour_pointer = contour_pointer.h_next()

cv.SaveImage( ‘outputfile.png’ , output_image)

撰写回答