Contour matchshapes in openCV

asked 2015-10-14 02:21:59 -0600

OlaruVlad gravatar image

updated 2015-10-14 07:07:00 -0600

I am new to openCV(with Python) and image processing and tried to start a project of my own to learn how things work. I'm trying to select the teeth from an image of an open mouth. The best solution that I could think of is to match it with a template of a tooth and try to matchshapes based on the contours that I get from the mouth image. This might not be the best solution, but it does seem rather easy.

So far, my code looks like this:

img = cv2.imread('resources/1.jpg', cv2.IMREAD_COLOR)
compare = cv2.imread('resources/template.jpg', cv2.IMREAD_GRAYSCALE)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.bilateralFilter(gray, 11, 17, 17)
edges = cv2.Canny(gray, 20, 10)
compare = cv2.Canny(compare, 200, 200)

contours, _ = cv2.findContours(gray.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
compare_contours, _2 = cv2.findContours(compare.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

for c in contours:
    for c2 in compare_contours:
        ret = cv2.matchShapes(c, c2, 1, 0.0)
        if ret < 0.5:
            peri = cv2.arcLength(c, True)
            approx = cv2.approxPolyDP(c, 0.02 * peri, True)
            cv2.drawContours(img, [approx], -1, (0, 255, 0), 7)

img is the image of the mouth and compare is a tooth from the same image that I selected with photoshop, so I do expect to match at least that one. My current problem is that the only contour that matches seems to be the frame of the image.

Can someone please point out what am I doing wrong or suggest me a better approach of the project? Does the image size have something to do with the image size and if so, what is the best way to resize images?

Thank you for your time!

Edit: Here are the images that I am currently using: imgur.com/2Ry0Yig, imgur.com/OPFQTPM.

edit retag flag offensive close merge delete

Comments

2

please, please replace the screenshot with a text version, so folks can c/p and test your code.

berak gravatar imageberak ( 2015-10-14 02:24:51 -0600 )edit
1

I'm sorry, didn't figure out at first how to add code to it and added the pic xD

OlaruVlad gravatar imageOlaruVlad ( 2015-10-14 03:25:37 -0600 )edit
1

no problem, well done ;)

berak gravatar imageberak ( 2015-10-14 03:28:15 -0600 )edit