Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

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()

click to hide/show revision 2
retagged

updated 2020-01-15 10:14:39 -0600

berak gravatar image

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

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()

click to hide/show revision 3
None

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

berak gravatar image

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

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

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))

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

np.linalg.norm(vector) # load the image, convert it to grayscale, and blur it slightly

slightly src = cv2.imread('C:\Users\Shrouti\Pictures\8.jpg')

cv2.imread('C:\Users\Shrouti\Pictures\8.jpg') # read image

image
# show source image

cv2.imshow("Source", src)

src)
# convert image to gray scale

gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)

cv2.COLOR_BGR2GRAY)
# blur the image

blur = cv2.blur(gray, (3, 3))

3))
# binary thresholding of the image

ret, thresh = cv2.threshold(blur, 200, 255, cv2.THRESH_BINARY)

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 #for i in range(len(contours)):

range(len(contours)): hull.append(cv2.convexHull(contours[0], False))

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)

effective_list.append(pc)
# create an empty black image

drawing = np.zeros((thresh.shape[0], thresh.shape[1], 3), np.uint8)

np.uint8)
# draw contours and hull points

for #for i in range(len(contours)):

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 #cv2.drawContours(drawing, contours, i, color_contours, 2, 8, hierarchy)

hierarchy) cv2.drawContours(drawing, cnt[0],-1, color_contours, 3) # draw convex hull #cv2.drawContours(drawing, hull, i, color, 2, 8)

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

cv2.waitKey(0)

cv2.destroyAllWindows()

drawing) cv2.waitKey(0) cv2.destroyAllWindows()