def patternDetection(cnts): #Initialize classes sd = ShapeDetector() cl = ColorLabeler() contours = [] for c in cnts: shape = sd.detect(c, 3) if shape != "": color = cl.label(lab, c) x,y,w,h = cv2.boundingRect(c) cX = x+w/2 cY = y+h/2 contours.append([cX, cY, shape, color]) row = [] pattern = [] rowMin = contours[0][1]-10 rowMax = contours[0][1]+10 for c in contours: cX, cY, shape, color = c if rowMin <= cY and cY <= rowMax: row.append([cX, cY, shape+' '+color]) else: row.sort() pattern.append(row) row = [[cX, cY, shape+' '+color]] rowMin = cY-10 rowMax = cY+10 return pattern
def alltheCV(image): #resized = imutils.resize(image, width=300) #ratio = image.shape[0] / float(resized.shape[0]) #height, width = image.shape[:2] print("runningCV") #Convert white background to black gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) (thresh, baw) = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU) wab = cv2.bitwise_not(baw) wabrgb = cv2.cvtColor(wab, cv2.COLOR_GRAY2RGB) noBackground = cv2.bitwise_and(image, wabrgb) #Blur and threshold the image for curve detection blurred = cv2.GaussianBlur(noBackground, (5, 5), 0) lab = cv2.cvtColor(blurred, cv2.COLOR_BGR2LAB) gray = cv2.cvtColor(noBackground, cv2.COLOR_BGR2GRAY) thresh = cv2.threshold(gray, 30, 255, cv2.THRESH_BINARY)[1] #Detect contours cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] #Initialize classes sd = ShapeDetector() cl = ColorLabeler() #Finding centers using bounding rectangle centers = [] for c in cnts: shape, perimeter = sd.detect(c, 1) color = cl.label(lab, c) x, y, w, h = cv2.boundingRect(c) cX = x + w / 2 cY = y + h / 2 centers.append([cX, cY, perimeter, shape, color]) return centers
lab = cv2.cvtColor(noBackground, cv2.COLOR_BGR2LAB) blurred_3 = cv2.GaussianBlur(noBackground, (1, 1), 0) gray_3 = cv2.cvtColor(blurred_3, cv2.COLOR_BGR2GRAY) thresh_3 = cv2.threshold(gray_3, 30, 255, cv2.THRESH_BINARY)[1] cnts = cv2.findContours(thresh_3.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if imutils.is_cv2() else cnts[1] #Initialize classes sd = ShapeDetector() cl = ColorLabeler() contours = [] for c in cnts: shape = sd.detect(c, 3) if shape != "": color = cl.label(lab, c) x, y, w, h = cv2.boundingRect(c) cX = x + w / 2 cY = y + h / 2 contours.append([cX, cY, shape, color]) # cv2.imshow("Image", img) # cv2.circle(img, (cX, cY), 3, (255, 255, 255), -1) # cv2.waitKey(0) # cv2.imshow("Image", img) # cv2.circle(img, (cX, cY), 3, (255, 255, 255), -1) # cv2.waitKey(0) # print(contours)
# Finding centers using moment of shape # centers = [] # for c in cnts: # if cv2.contourArea(c) > 10: # M = cv2.moments(c) # cX = int(M["m10"] / M["m00"]) # cY = int(M["m01"] / M["m00"]) # shape = sd.detect(c) # color = cl.label(lab, c) # centers.append([cX,cY, shape, color]) #Finding centers using bounding rectangle centers = [] for c in cnts: shape, perimeter = sd.detect(c, ratio) color = cl.label(lab, c) x, y, w, h = cv2.boundingRect(c) cX = x + w / 2 cY = y + h / 2 centers.append([cX, cY, perimeter, shape, color]) centersSorted = sorted(centers, key=lambda k: [k[0], k[1]]) curr = 0 col = [] for c in centersSorted: if curr > cY: #new column output.append(col) #add old column col = []
# Finding centers using moment of shape # centers = [] # for c in cnts: # if cv2.contourArea(c) > 10: # M = cv2.moments(c) # cX = int(M["m10"] / M["m00"]) # cY = int(M["m01"] / M["m00"]) # shape = sd.detect(c) # color = cl.label(lab, c) # centers.append([cX,cY, shape, color]) #Finding centers using bounding rectangle centers = [] for c in cnts: shape = sd.detect(c, ratio) color = cl.label(lab, c) x, y, w, h = cv2.boundingRect(c) cX = x + w / 2 cY = y + h / 2 centers.append([cX, cY, shape, color]) centersSorted = sorted(centers, key=lambda k: [k[0], k[1]]) for c in centersSorted: cX, cY, shape, color = c cv2.circle(image, (cX, cY), 3, (255, 255, 255), -1) cv2.imshow('image', image) print(shape, color) # print(cX, cY) cv2.waitKey(0)