How Does Contour Extraction Work?

asked 2015-07-06 19:48:46 -0600

Shiki gravatar image

updated 2015-07-07 11:34:26 -0600

Eduardo gravatar image

I've been modifying squares.py to be able to detect contours of 3D objects. My goal is for it to extract the shapes of 3D objects (i.e. get the squares of a cube). However, whenever I run my code on the image below, drawing the contours gives me a hexagon and a green Y. Is there any way to modify this so that for each contour, I get the polygon of a square/ shape? Thanks!

image description

image description

#!/usr/bin/env python                                 

'''                                                          
Usage:                                                
  ObjectDescriptors.py [<image source>]

  Converts visual objects into basic shapes.
'''

import cv2                                            
import numpy as np                                    

# relative module
import video                                          

# built-in module                                     
import sys

#from bulbs.neo4jserver import Graph
def findShapes(img):
    img = cv2.GaussianBlur(img, (1, 1), 0)
    cv2.imshow("test", img)
    shapeList = []

    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    gray = cv2.bilateralFilter(gray, 11, 17, 17)
    edged = cv2.Canny(gray, 0, 200)
    cv2.imshow("test", edged)
    contours, hierarchy = cv2.findContours(edged, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    for cnt in contours:
        cnt_len = cv2.arcLength(cnt, True)
        cnt = cv2.approxPolyDP(cnt, 0.01*cnt_len, True)
        shapeList.append(cnt)
    keyVertices = []
    for i in shapeList:
        print("Shape Coordinates:",  [pts for pts in i])
    return shapeList def removeDuplicates(contours):
    print(2)    

if __name__ == '__main__':
    from glob import glob
    for fn in glob('../data/perfectcube/png'):
        img = cv2.imread(fn)
        squares = findShapes(img)
        cv2.drawContours( img, squares, 0, (0, 255, 0), 3 )
        cv2.drawContours( img, squares, 1, (255, 0, 0), 3 )
        cv2.drawContours( img, squares, 2, (0, 0, 255), 3 )
        cv2.imshow('squares', img)
        ch = 0xFF & cv2.waitKey()
        if ch == 27:
            break
    cv2.destroyAllWindows()
edit retag flag offensive close merge delete

Comments

Add your code please, there is no way at all for us to guess what is happening here...

StevenPuttemans gravatar imageStevenPuttemans ( 2015-07-07 07:46:47 -0600 )edit

Sorry, added code for reference.

Shiki gravatar imageShiki ( 2015-07-07 11:32:46 -0600 )edit
1

Afaik cv2.findContours works on binary images only, thus it would be very difficult (not impossible) to use this method

Guanta gravatar imageGuanta ( 2015-07-07 14:12:21 -0600 )edit

Do you have any suggestions for approximating 3D shapes? I've looked into PCL as well, but it didn't seem like it could create a 3D representation.

Shiki gravatar imageShiki ( 2015-07-07 15:43:39 -0600 )edit