Skip to content

Instantly share code, notes, and snippets.

@choowilson
Created February 24, 2020 14:57
Show Gist options
  • Save choowilson/65869acde6fa32f1064a082aba15d487 to your computer and use it in GitHub Desktop.
Save choowilson/65869acde6fa32f1064a082aba15d487 to your computer and use it in GitHub Desktop.
temp
def thresh_callback(val):
threshold = val
_,thres_out = cv.threshold(src_gray, 230, 255,
cv.THRESH_BINARY)
canny_output = cv.Canny(thres_out, threshold, threshold * 2)
contours, _ = cv.findContours(canny_output, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
# Find the convex hull object for each contour
hull_list = []
for i in range(len(contours)):
hull = cv.convexHull(contours[i])
hull_list.append(hull)
# Draw contours + hull results
drawing = np.zeros((canny_output.shape[0], canny_output.shape[1], 3), dtype=np.uint8)
c = max(hull_list, key = cv.contourArea)
x,y,w,h = cv.boundingRect(c)
# draw the biggest contour (c) in green
cv.rectangle(src,(x,y),(x+w,y+h),(0,255,0),2)
cv.imshow('Thres', thres_out)
cv.imshow('CannyOut', canny_output)
cv.imshow('Final', src)
for i in range(len(contours)):
color = (rng.randint(0,256), rng.randint(0,256), rng.randint(0,256))
cv.drawContours(drawing, contours, i, (255,0,0))
cv.drawContours(drawing, hull_list, i,(0,255,0))
# Show in a window
cv.imshow('Contours', drawing)
src = cv.imread("ic_front_test_720x1280.png")
if src is None:
print('Could not open or find the image:', args.input)
exit(0)
# Convert image to gray and blur it
src_gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
src_gray = cv.blur(src_gray, (3,3))
cv.imshow("gray",src_gray)
# Create Window
source_window = 'Source'
cv.namedWindow(source_window)
cv.imshow(source_window, src)
max_thresh = 255
# thresh = 100 # initial threshold
thresh = 255 # initial threshold
cv.createTrackbar('Canny thresh:', source_window, thresh, max_thresh, thresh_callback)
thresh_callback(thresh)
cv.waitKey()
cv.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment