Ask Your Question
0

How to determine intersection points between contour and convex hull

asked 2020-01-15 09:49:16 -0500

titli gravatar image

updated 2020-01-15 10:15:08 -0500

berak gravatar image

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
src = cv2.imread('C:\Users\Shrouti\Pictures\8.jpg')

# read image

    # 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[0], False))

effective_list = []
for i in range(len(cnt)):
    for j in range(len(hull)):
        pc = cnt[i][0]
        Pc = np.array(pc)
        ph = hull[j][0]
        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[0], thresh.shape[1], 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[0],-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 flag offensive close merge delete

Comments

and the problem is ?

berak gravatar imageberak ( 2020-01-15 10:15:20 -0500 )edit

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.

titli gravatar imagetitli ( 2020-01-16 09:15:37 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-01-16 11:13:30 -0500

exbigboss gravatar image

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.

edit flag offensive delete link more

Comments

thank you !!

titli gravatar imagetitli ( 2020-01-27 09:45:43 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2020-01-15 09:49:16 -0500

Seen: 53 times

Last updated: Jan 16