Ejemplo n.º 1
0
def draw_convex_hull(a, original):

    ret, b = cv2.threshold(a, 255, 255, cv2.THRESH_BINARY)

    contornos, jerarquia = cv2.findContours(a,
            cv2.RETR_EXTERNAL,
                cv2.CHAIN_APPROX_SIMPLE)

    mayores = obtener_mayores_contornos(contornos, 4)
    puntos = [];
    for n, center, radius in mayores:

        cnt = contornos[n]
        centro = calcular_centroide(cnt)
        dibujar_marcador(original, centro, (0, 0, 255))
        if radius < 30:
            continue

        #hull = cv2.convexHull(cnt)
        #foo = cv2.convexHull(cnt, returnPoints=False)
        #cv2.drawContours(original, contornos, n, (0, 35, 245))
        #if len(cnt) > 3 and len(foo) > 2:
        #    defectos = cv2.convexityDefects(cnt, foo)
        #    if defectos is not None:
        #        defectos = defectos.reshape(-1, 4)
        #        puntos = cnt.reshape(-1, 2)
        #        for d in defectos:
        #            if d[3] > 20:
        #                cv2.circle(original, tuple(puntos[d[0]]), 5, (255, 255, 0), 2)
        #                cv2.circle(original, tuple(puntos[d[1]]), 5, (255, 255, 0), 2)
        #                cv2.circle(original, tuple(puntos[d[2]]), 5, (0, 0, 255), 2)

        #lista = numpy.reshape(hull, (1, -1, 2))
        #cv2.polylines(original, lista, True, (0, 255, 0), 3)


        box = cv2.fitEllipse(cnt)
        cv2.ellipse(original, box, (255,255,0), 4)

        center = tuple(map(int, center))
        dibujar_marcador(original, center, (0, 255, 0))
        radius = int(radius)
        cv2.circle(original, center, radius, (255, 0, 0), 3)
        # Valores a devolver:
        # cantidad de defectos de convexion
        # Coordenadas del centro del circulo
        # inclinacion de la elipse
        # radio del centro del circulo
        puntos.append(Event(center, radius, box[2]))

    eventos = etiquetar_eventos(original, puntos)
    return eventos
Ejemplo n.º 2
0
print gray.shape
print gray
ret, b = cv2.threshold(gray, 20, 255, cv2.THRESH_BINARY)

cv2.imshow('Test', b)

contornos, jerarquia = cv2.findContours(b,
        cv2.RETR_EXTERNAL,
        cv2.CHAIN_APPROX_SIMPLE)

for cnt in contornos:
    hull = cv2.convexHull(cnt)
    lista = numpy.reshape(hull, (1, -1, 2))

    centro = calcular_centroide(hull)

    dibujar_marcador(a, centro, (255, 255, 0))

    centro = calcular_centroide(cnt)
    dibujar_marcador(a, centro, (20, 40, 255))

    cv2.polylines(a, lista, True, (0, 255, 0), 3)
    center, radius = cv2.minEnclosingCircle(cnt)
    center = tuple(map(int, center))
    radius = int(radius)
    cv2.circle(a, center, radius, (255, 0, 0), 3)
    cv2.circle(a, center, 5, (255, 0, 0), 3)

cv2.imshow('Original', a)