- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试从图像中提取轮廓,旋转这些轮廓并将它们插入到新图像中。代码如下。我的问题出在旋转轮廓方法中。执行代码时会看到以下错误“TypeError:‘cv.cvseq’对象不支持项目分配”。
关于如何解决这个问题的任何想法?我正在为 Opencv 2.2 使用 python 绑定(bind)。
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)
最佳答案
似乎您无法使用 Python 绑定(bind)更改 cvseq
对象的元素(注意 here 仅用于操作 cvseq
对象的 Python 方法列表具有删除序列元素和更改其顺序的方法。)
尽管如此,Python 绑定(bind)确实提供了实现旋转图像轮廓的既定目标的工具。
由于 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)
关于python - 在 python 中更改 CvSeq 中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5132874/
在使用 cv FindContours( g_gray, g storage, & contours ) 之后,我需要将整数值添加到序列 CvSeq * contours 中的所有点。怎么做? 最佳答
我正在创建一个 double 值数组,我想将创建的数组添加到 CvSeq。我为此目的使用了 cvSeqPush。但是当我检索这个数组时,它只显示数组的第一个元素,其余的是垃圾。代码如下.. int T
我想要一个函数,它返回类似于指针数组的东西,即指向 CvSeq 数组的第一个元素的指针。但是,我不知道是否可以创建一个 CvSeq 数组。 这样做的目的是得到不同图像轮廓的CvSeq值。 这是我的代码
我们可以随机访问 CvSeq 对象中的一个点吗?我们可以遍历它,所以我想它可以以一种简单的方式实现。这是如何实现的? 最佳答案 我找到了。有一个名为cvGetSeqElem 的方法,它接收序列和索引。
我有点迷失在 OpenCV 文档中,我想将 cvFindContours 返回的 CvSeq 保存到一个数组中,据我所知它将返回 CvContour 的序列,但我找不到它包含的内容?我应该保存它的哪些
我有一个快速的 OpenCV 问题。是否可以采用关键点 vector 并将其转换为 CvSeq? 提前致谢。 最佳答案 我不知道你为什么想要那个,但它应该是可能的,有了这些功能你可以做任何你想做的事:
嗨, 我如何在 opencv 中进行类型转换,我想将类型转换为 cvseq 到 cvpoint 最佳答案 据我所见: http://opencv.willowgarage.com/documentat
Bradski 说“当你想删除一个序列时,你可以使用 cvClearSeq(),一个清除序列所有元素的例程。” 但是,此函数不会将内存存储中分配的 block 返回给存储或系统。 他说“如果你想出于其
我使用: cvFindContours(gray, mem, contours, Loader.sizeof(CvContour.class) , CV_RETR_CCOMP, CV_CHAIN_AP
我想做一个 double 的 CvSeq。我想也许我可以通过将元素类型指定为 CV_32FC1 来解决这个问题: MySeq=cvCreateSeq(CV_32FC1,sizeof(CvSeq),si
因此,我在工作中有一个使用 OpenCV 的项目(这实际上是我使用 OpenCV 做的第一个项目),我必须使用 CvSeq 之类的东西来使用 SURF 算法。自从我开始把它放在我的代码中,这完全是一团
我正在尝试从图像中提取轮廓,旋转这些轮廓并将它们插入到新图像中。代码如下。我的问题出在旋转轮廓方法中。执行代码时会看到以下错误“TypeError:‘cv.cvseq’对象不支持项目分配”。 关于如何
我是 OpenCV 的新手,在使用它时遇到了一些问题。 目前我正在研究二叉分区树 (BPT) 算法。基本上我需要根据一些参数将图像分成许多区域。 2个区域将合并形成1个新区域,由这2个区域组成。 我设
我正在使用 javacv 开发项目,我能够识别多边形并将该详细信息存储在 CvSeq 中。我尝试访问该结构中的点,但它对我不起作用。所以请有人解释一下如何在 javacv 中访问 CvSeq 结构中的
问题已回答,请参阅问题末尾的解决方案。仍然欢迎更多评论/回答。 所以我在将所有元素放入一个内存存储时遇到了一些麻烦,我设法做的最好的事情就是每次都覆盖存储并且只取出覆盖它的最终元素,遗憾的是这还不够。
我是一名优秀的程序员,十分优秀!