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
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 Output Image
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'
Never seem before
hull=np.array([])
. Also what is thisconv2=cv2.convexHull(hull)
?Ex:
Take a look example:
hull=np.array([])
, this is like i am initializing the NumPy array andhull=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 oneCan you post original image?
What are u achieving in your example? Sorry i didn't understand why people and age come in
Uploaded the image to question with what is input and what is output , which i am expecting
Why 4 images?
There are only two images .. one is input and other is expected output
I see 4 images on right side.