Skip to content

Instantly share code, notes, and snippets.

@lYlantis
Created November 4, 2018 00:50
Show Gist options
  • Save lYlantis/f87c374e28af6fec129f2ebbefc66a4b to your computer and use it in GitHub Desktop.
Save lYlantis/f87c374e28af6fec129f2ebbefc66a4b to your computer and use it in GitHub Desktop.
Testing for erode and dilate parameters
import numpy as np
import cv2
import os
i=0
os.chdir('./Testing3')
while i<11:
j=0
while j<11:
image_color= cv2.imread("testme.jpg")
image_ori = cv2.cvtColor(image_color,cv2.COLOR_BGR2GRAY)
lower_bound = np.array([0,0,20])
upper_bound = np.array([255,255,195])
image = image_color
mask = cv2.inRange(image_color, lower_bound, upper_bound)
kernel = np.ones((3, 3), np.uint8)
#Use erosion and dilation combination to eliminate false positives.
mask = cv2.erode(mask, kernel, iterations=i)
mask = cv2.dilate(mask, kernel, iterations=j)
closing = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
contours = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)[1]
if i==1:
print(contours)
array = []
ii = 1
num= len(contours)
for c in contours:
(x,y),r = cv2.minEnclosingCircle(c)
center = (int(x),int(y))
r = int(r)
if r<=15:
cv2.circle(image,center,r,(0,255,0),2)
array.append(center)
cv2.imwrite("erode"+str(i)+"_dilate"+str(j)+"_circles"+str(num)+".jpg", image_color)
j += 1
i += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment