Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 通过计算变换计算元素位置_Python_Opencv_Transformation - Fatal编程技术网

Python 通过计算变换计算元素位置

Python 通过计算变换计算元素位置,python,opencv,transformation,Python,Opencv,Transformation,这个问题涉及到。Hovewer更详细地说明了这一点,并添加了一些假设 我有元素图像和一些模型 我在两个方向上都检测到了轮廓 contoursModel0, hierarchyModel = cv2.findContours(model.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE); contoursModel = [cv2.approxPol

这个问题涉及到。Hovewer更详细地说明了这一点,并添加了一些假设

我有元素图像和一些模型

我在两个方向上都检测到了轮廓

contoursModel0, hierarchyModel = cv2.findContours(model.copy(), cv2.RETR_LIST,   
                                                  cv2.CHAIN_APPROX_SIMPLE);
contoursModel = [cv2.approxPolyDP(cnt, 2, True) for cnt in contoursModel0];    
contours0, hierarchy = cv2.findContours(canny.copy(), cv2.RETR_LIST,  
                                        cv2.CHAIN_APPROX_SIMPLE);
contours = [cv2.approxPolyDP(cnt, 2, True) for cnt in contours0];
然后我将每个轮廓相互匹配

modelMassCenters = [];
imageMassCenters = [];
for cnt in contours:
for cntModel in contoursModel:
    result = cv2.matchShapes(cnt, cntModel, cv2.cv.CV_CONTOURS_MATCH_I1, 0);
    if(result != 0):
        if(result < 0.05):
           #Here are matched contours
           momentsModel = cv2.moments(cntModel);
           momentsImage = cv2.moments(cnt);
           massCenterModel = (momentsModel['m10']/momentsModel['m00'],  
                              momentsModel['m01']/momentsModel['m00']); 
           massCenterImage = (momentsImage['m10']/momentsImage['m00'], 
                              momentsImage['m01']/momentsImage['m00']); 
           modelMassCenters.append(massCenterModel);
           imageMassCenters.append(massCenterImage); 
在计算了每对对应直线给出的比例和旋转后,我将找到旋转的中值和平均值,它们与中值的差值不超过某个增量。规模也是如此。然后,计算这些值的点将用于计算位移。

如果特征具有相似的形状,则第二步(通过成对形状比较将轮廓相互匹配)听起来非常容易出错,例如,您有几个大小相似的圆形轮廓。然而,如果你只有一个象限中有5个圆形特征的刚体,那么如果你把身体和它的特征看成一个整体,你就可以得到一个非常可靠的仿射变换估计。因此,在匹配特征时,不要丢弃特征的范围和方向等信息。这些在关联特征时至少和单个轮廓的大小和形状一样重要

我会尝试(未经测试的伪代码):


如果您有非常具有挑战性的图像,例如由6个等距相似尺寸的特征组成的圆环,其中5个具有相同的形状,另一个不同(例如5个圆和一个星),您可以在特征参数列表中添加额外的参数,例如偏心率和锐度,在搜索旋转角度时,将它们包含在相关性中。

只是为了解决这个问题,会有人指出您的代码有点C/Java风格。虽然不是语法错误,但在
if
周围不需要括号,行不需要以
结尾我知道,我根本不懂Python。仅在使用OpenCVA测试某些视觉算法时使用它当您使用findHomography时,是否使用了ransac选项?它到底有什么问题?FindHomography假设在3D中进行收缩和变换。我只添加了一个作为解决错误匹配问题的示例。它不适合解决这个问题。谢谢你提供了非常好的解决方案。有时问题是模型体轮廓和图像体轮廓没有被检测到或有一些遮挡。我将尝试将模型中心上检测到的所有特征视为质心。图像也是如此。您的方法中的一些正确性度量是什么?我看到在那里可以做一些相关的事情,但是如何做呢?我想找到更系统、更可靠的输出匹配正确性的方法。1.0的互相关(图像和模型之间)将是完美的匹配;小于1.0表示匹配质量较低。确定。谢谢你完整的回答。我将等待一些其他的解决方案,因为这个问题可以通过多种方式解决,然后接受最佳答案
for i in range(0, len(modelMassCenters) - 1):
for j in range(i + 1, len(modelMassCenters) - 1  ):
    x1, y1 = modelMassCenters[i];
    x2, y2 = modelMassCenters [j];
    modelVec = (x2 - x1, y2 - y1);
    x1, y1 = imageMassCenters[i];
    x2, y2 = imageMassCenters[j];
    imageVec = (x2 - x1, y2 - y1);
    rotation = angle(modelVec,imageVec);
    rotations.append((i, j, rotation)); 
    scale = length(modelVec)/length(imageVec);
    scales.append((i, j,  scale)); 
"""
Convert from rectangular (x,y) to polar (r,w)
    r = sqrt(x^2 + y^2)
    w = arctan(y/x) = [-\pi,\pi]
"""
def polar(x, y):        # w in radians
    from math import hypot, atan2, pi
    return hypot(x, y), atan2(y, x)

model_features = []
model = params(model_body_contour)    # return tuple (center_x, center_y, area)
for contour in model_feature_contours:
    f = params(countour)
    range, angle = polar(f[0]-model[0], f[1]-model[1])
    model_features.append((angle, range, f[2]))

image_features = []
image = params(image_body_contour)
for contour in image_feature_contours:
    f = params(countour)
    range, angle = polar(f[0]-image[0], f[1]-image[1])
    image_features.append((angle, range, f[2]))

# sort image_features and model_features by angle, range
#
# correlate image_features against model_features across angle offsets
#    rotation = angle offset of max correlation
#    scale = average(model areas and ranges) / average(image areas and ranges)