# How to determine intersection points between contour and convex hull

This is my code, where I have computed both contour and convex hull. Now I want to determine the intersection points between the two:

# -*- coding: utf-8 -*-
"""
Created on Thu Dec 26 20:50:00 2019

@author: Shrouti
"""
from PIL import Image
import cv2
import numpy as np
from matplotlib import pyplot as plt
import os

def angle_between(v1, v2):
v1_u = unit_vector(v1)
v2_u = unit_vector(v2)
return np.arccos(np.clip(np.dot(v1_u, v2_u), -1.0, 1.0))

def unit_vector(vector):
np.seterr(divide='ignore', invalid='ignore')
return vector / np.linalg.norm(vector)

# load the image, convert it to grayscale, and blur it slightly

# show source image
cv2.imshow("Source", src)

# convert image to gray scale
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)

# blur the image
blur = cv2.blur(gray, (3, 3))

# binary thresholding of the image
ret, thresh = cv2.threshold(blur, 200, 255, cv2.THRESH_BINARY)

# find contours
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
# cc
cnt = sorted(contours, key=cv2.contourArea, reverse=True)
Cnt = np.array(cnt)
# create hull array for convexHull points
hull = []

# calculate points for each contour
#for i in range(len(contours)):
hull.append(cv2.convexHull(contours, False))

effective_list = []
for i in range(len(cnt)):
for j in range(len(hull)):
pc = cnt[i]
Pc = np.array(pc)
ph = hull[j]
Ph = np.array(ph)
if(np.array_equal(Pc,Ph)):
print(ph)
effective_list.append(pc)

# create an empty black image
drawing = np.zeros((thresh.shape, thresh.shape, 3), np.uint8)

# draw contours and hull points
#for i in range(len(contours)):
color_contours = (0, 255, 0) # color for contours
color = (255, 255, 255) # color for convex hull
# draw contours
#cv2.drawContours(drawing, contours, i, color_contours, 2, 8, hierarchy)
cv2.drawContours(drawing, cnt,-1, color_contours, 3)
# draw convex hull
#cv2.drawContours(drawing, hull, i, color, 2, 8)

angles=[]
indices = []
cv2.drawContours(drawing, hull, 0, color, 2, 8)
cv2.imshow("Output", drawing)

cv2.waitKey(0)

cv2.destroyAllWindows()

edit retag close merge delete

the problem is whenever I am running the program I can not find anything inside "effective_list". How this is possible? Because convex hull and contour must have intersection points. It is really important to get these for my further work. Please help.

Sort by » oldest newest most voted

Lookup the convexity defects functions.

You can find it here: https://docs.opencv.org/4.1.1/d3/dc0/...

In theory, this function would give you a bunch of segments that have 0 distance from the convex hull.

more

Official site

GitHub

Wiki

Documentation