Ask Your Question
0

contours and hirerachy mismatch

asked 2018-11-20 03:57:37 -0600

venu gravatar image

updated 2018-11-20 04:01:31 -0600

berak gravatar image

C:\fakepath\original.jpg

Hi When I use the below code without bothering hirerachy , it is able to detect external contours correctly . Pl see the attached file “drawn-NO-hirerachy”. My original image has three external contours .

cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

when I use hirerachy code , it is not detecting external contours correctly . It shows a few points that I red circled in “drawa-with hirerachy”

im,cnts,hierarchy = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

Following is the code I am using

# import the necessary packages
import numpy as np
import cv2
import imutils
import pytesseract
from PIL import Image, ImageEnhance
from imutils.perspective import four_point_transform
from array import *


# load the example image
image = cv2.imread("h2.jpg")

# pre-process the image by resizing it, converting it to
# graycale, blurring it, and computing an edge map
image = imutils.resize(image, height=500)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(blurred, 50, 200, 255)

#cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

im,cnts,hierarchy = cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

cnts = cnts[0] if imutils.is_cv2() else cnts[1]

cnts = sorted(cnts, key=cv2.contourArea, reverse=True)


print 'No Of Contours =  ', len(cnts)

print 'No Of elements in hirerachy =  ', len(hierarchy[0])
print hierarchy

major = cv2.__version__.split('.')[0]

print major

for c in cnts:
    cv2.drawContours(image, [c], -1, (0,255,0), 2)

cv2.imshow('EDGED',edged)
cv2.imshow('DRAWN CONTOURS',image)
cv2.imshow('RETURNED IMAGE',im)
cv2.waitKey(0)
edit retag flag offensive close merge delete

Comments

@venu. Do you want to draw 6 borders(2 lines + 2 borders + 2 rectangles? Or just 4 borders(2 lines + 2 borders). I still have 6 borders.

supra56 gravatar imagesupra56 ( 2018-11-20 21:19:47 -0600 )edit

2 answers

Sort by » oldest newest most voted
1

answered 2018-11-22 06:37:07 -0600

supra56 gravatar image

I don't know which one do you want. Unfortunately, never answered question.. Here is code:

import cv2
import numpy as np

# load the example image
image = cv2.imread("h2.jpg")

# pre-process the image by resizing it, converting it to
# graycale, blurring it, and computing an edge map
image = cv2.resize(image, (500, 500))
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(blurred, 50, 200)
im,cnts,hierarchy = cv2.findContours(edged, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

cnts = cnts[:15] #cnts = cnts[1:][:7]
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)

for c in cnts:
    cv2.drawContours(image, [c], -1, (0,255,0), 2)

cv2.imshow('EDGED', edged)
cv2.imshow('DRAWN CONTOURS', image)
cv2.imshow('RETURNED IMAGE', im)
cv2.waitKey(0)

image description image description

edit flag offensive delete link more

Comments

for second image cnts = cnts[1:][:7]

supra56 gravatar imagesupra56 ( 2018-11-22 06:55:09 -0600 )edit
1

answered 2018-11-20 04:04:21 -0600

berak gravatar image

updated 2018-11-20 04:05:52 -0600

RETR_EXTERNAL does, what its name says: it only returns the most outer contour. (there is no hierarchy)

to traverse a hierachy of nested contours, use RETR_TREE or RETR_CCOMP instead.

have a look at the docs, again

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-11-20 03:57:37 -0600

Seen: 502 times

Last updated: Nov 22 '18