Need find Convex Hull in Python for already found Convex Hull point , draw outer most Convex Hull and get it's X and Y co-ordinates

asked 2020-03-20 00:54:06 -0500

sreevathsabr gravatar image

updated 2020-10-18 13:54:27 -0500

Below is my program, I am trying to find the Convex Hull of the image and send that convex Hull points again to OpenCV Convex Hull to find the outermost Convex Hull of it .

Input Image image description Output Image image description

import cv2
import numpy as np
# Load the image
img1 = cv2.imread(r'test.tif')
# Convert it to greyscale
img = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
# Threshold the image
ret, thresh = cv2.threshold(img,50,255,0)
# Find the contours
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img1, contours, -1, (255, 0, 0), 2)
hull=np.array([])
for i in range(len(contours)):
      hullv=cv2.convexHull(contours[i])
      hull=np.append(hull,hullv)
      cv2.drawContours(img1, [hullv], 0, (255, 0, 0), 2)
conv2=cv2.convexHull(hull)
cv2.drawContours(img1, conv2, -1, (255, 0, 0), 2)
cv2.imwrite(r"contours2.png",img1)

I am seeing below error when I do this.

Traceback (most recent call last):
  File "C:/TestCode/DocumentLayoutDetection/ConvexHull.py", line 17, in <module>
    conv2=cv2.convexHull(hull)
cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgproc\src\convhull.cpp:137: error: (-215:Assertion failed) total >= 0 && (depth == CV_32F || depth == CV_32S) in function 'cv::convexHull'
edit retag flag offensive close merge delete

Comments

Never seem before hull=np.array([]). Also what is this conv2=cv2.convexHull(hull)?

supra56 gravatar imagesupra56 ( 2020-03-20 03:45:12 -0500 )edit

Ex:

points = np.array(points)
points = cv2.convexHull(points)

Take a look example:

areas = []
contours,hierarchy = cv2.findContours(thresh, 1, 2)
for c in contours:
     areas.append(cv2.contourArea(c))
     people = np.array(contours)
     ages = np.array(areas)
     inds = ages.argsort()
     sortedcontours = people[inds]
     cnt = sortedcontours[-1]
     hull = cv2.convexHull(cnt)
supra56 gravatar imagesupra56 ( 2020-03-20 03:51:41 -0500 )edit
1

hull=np.array([]) , this is like i am initializing the NumPy array and hull=np.append(hull,hullv) and in this line, I am appending the convecexHull points to that NumPy array. conv2=cv2.convexHull(hull) , here i am trying to find the convexHull of the ConvexHull points to remove all the inner ConvexHull and to have only the outer one

sreevathsabr gravatar imagesreevathsabr ( 2020-03-20 03:56:25 -0500 )edit

Can you post original image?

supra56 gravatar imagesupra56 ( 2020-03-20 04:09:07 -0500 )edit

What are u achieving in your example? Sorry i didn't understand why people and age come in

sreevathsabr gravatar imagesreevathsabr ( 2020-03-20 04:10:34 -0500 )edit
1

Uploaded the image to question with what is input and what is output , which i am expecting

sreevathsabr gravatar imagesreevathsabr ( 2020-03-20 04:29:05 -0500 )edit

Why 4 images?

supra56 gravatar imagesupra56 ( 2020-03-20 06:24:37 -0500 )edit

There are only two images .. one is input and other is expected output

sreevathsabr gravatar imagesreevathsabr ( 2020-03-20 06:29:33 -0500 )edit

I see 4 images on right side.

supra56 gravatar imagesupra56 ( 2020-03-20 06:40:52 -0500 )edit