1

我有一个脚本可以收集照片中的所有 MSER 区域:

import numpy as np
import cv2
import sys
import matplotlib.pyplot as plt

imp1 = sys.argv[1]
minA = int(sys.argv[2])
maxA = int(sys.argv[3])
minD = float(sys.argv[4])

try:
        als = str(sys.argv[5])
except IndexError:
        als = False

img1 = cv2.imread(imp1)


PARAMS = {'_delta':5, '_min_area': minA, '_max_area': maxA, '_min_diversity': minD}

mser = cv2.MSER(**PARAMS)
gray = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
vis = img1.copy()


regions = mser.detect(gray, None)
hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions]


print "Regions: " + str(type(regions))
print "Region Number: " + str(len(regions))
print "Hulls: " + str(type(hulls))
print "Hull Number: " + str(len(hulls))



if als == "True":
        rc = 1
        for i in hulls:
            tl = str(i.tolist())

            a = tl.replace("]], [[", "], [")
            b = a.replace("[[", "[")
            nl = b.replace("]]]", "]]")

            print "Hull  Count: " + str(rc)
            print "Hull Number: {} - {}".format(rc,  str(nl))
            print ""
            rc += 1

它运行良好,没有问题:

Hull  Count: 617
Hull Number: 617 - [[397, 1773], [360, 1775], [356, 1775], [352, 1774], [350, 1773], [349, 1772], [349, 1766], [361, 1683], [362, 1677], [369, 1677], [370, 1678], [397, 1771]]

Hull  Count: 618
Hull Number: 618 - [[2653, 1978], [2652, 1979], [2627, 1979], [2621, 1977], [2624, 1973], [2627, 1970], [2630, 1968], [2632, 1967], [2636, 1967], [2647, 1974]]

Hull  Count: 619
Hull Number: 619 - [[2660, 1977], [2658, 1979], [2655, 1980], [2654, 1980], [2627, 1979], [2615, 1976], [2618, 1974], [2630, 1967], [2632, 1966], [2636, 1966]]

但是,当我尝试将这些转换为 OpenCV 中的折线时,我只会得到无限的 numpy 错误。然后我尝试变得更简单,并简单地遍历各个船体:

regions = mser.detect(gray, None)
hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions]

nl = 1
for i in hulls:

    print "I Type: " + str(type(i))
    print "I Number: " + str(nl)
    print "I Length: " + str(len(i))
    print "I Raw: " + str(i)

    cv2.polylines(vis, i, 2, (0, 255, 0))
    h = cv2.cvtColor(vis, cv2.COLOR_BGR2RGB)

    plt.imshow(h)
    plt.title('MISER Regions')
    plt.show()

    nl += 1

但这只是给了我 66 个空白图像(即:只是普通图片),而我应该得到 66 个左右的图像,每个图像都有一个绿色 MSER 叠加。我不确定问题出在哪里。谁能帮我?

4

0 回答 0