def detectEdgesInColorPlanes(self, imageToSplit, colorPlane="red"): morpher = ImageMorpher() channels = cv2.split(imageToSplit) colorEdges = [] for i in range(len(channels)): blurred = cv2.GaussianBlur(channels[i], (7, 7), 3) colorEdges.append(morpher.dilateWithSquare(cv2.Canny(blurred, 50, 100), 3)) if (colorPlane == "red"): print 'anding' result = cv2.bitwise_and(colorEdges[0], colorEdges[1]) result = morpher.closeWithSquare(result, 2) #self.showImage(result) result = cv2.bitwise_and(result, cv2.bitwise_not(colorEdges[2])) elif (colorPlane == "green"): result = cv2.bitwise_and(colorEdges[0], colorEdges[2]) result = morpher.closeWithSquare(result, 2) result = cv2.bitwise_and(result, cv2.bitwise_not(colorEdges[1])) elif (colorPlane == "blue"): result = cv2.bitwise_and(colorEdges[1], colorEdges[2]) result = morpher.closeWithSquare(result, 2) result = cv2.bitwise_and(result, cv2.bitwise_not(colorEdges[0])) #self.showImage(result) result = morpher.openWithSquare(result, 2) #self.showImage(result) return result
def cutOutNonRed(self, imageToCut): redHighlighted = self.highlightDarkRed(imageToCut, 160, -1, 180, 20, False) #self.showImage(redHighlighted) morpher = ImageMorpher() #self.showImage(redHighlighted) mask1 = morpher.dilateWithSquare(redHighlighted, 10) #self.showImage(mask1) mask1 = morpher.closeWithSquare(mask1, 50) #self.showImage(mask1) maskedImg = cv2.bitwise_and(imageToCut, imageToCut, mask=mask1) #self.showImage(maskedImg) gray = self.convertColorToGrayscale(imageToCut) blurred = cv2.GaussianBlur(gray, (5, 5), 3) edges = cv2.Canny(blurred, 50, 100) #self.showImage(edges) mask2 = morpher.dilateWithSquare(edges, 10) #self.showImage(mask2) mask2 = morpher.closeWithSquare(mask2, 50) #self.showImage(mask2) secondMaskedImg = cv2.bitwise_and(maskedImg, maskedImg, mask=mask2) #self.showImage(secondMaskedImg) totalMask = cv2.bitwise_and(mask1, mask2) #self.showImage(totalMask) totalMask = morpher.operateWithVerticalLine(totalMask, morpher.DILATION, 15) totalMask = morpher.operateWithHorizontalLine(totalMask, morpher.DILATION, 15) #self.showImage(totalMask) totalMask = morpher.openWithSquare(totalMask, 20) #self.showImage(totalMask) totalMask = morpher.closeWithSquare(totalMask, 40) #self.showImage(totalMask) result = cv2.bitwise_and(imageToCut, imageToCut, mask=totalMask) #self.showImage(result) return result
class LabelRecognizer: # constants for letters B_FROM_LABEL = 1 R_FROM_LABEL = 2 A_FROM_LABEL = 3 C_FROM_LABEL = 4 H_FROM_LABEL = 5 S_FROM_LABEL = 6 whRatioCutoff = 6 perimAreaCutoff = 3 correlationCutoff = 1 CONTOUR_MATCHING = 1 # may add other letter matching methods later def __init__(self): self.basicFunctions = BasicFunctions() self.morpher = ImageMorpher() def findLetterFromLabel(self, searchImage, letterEnum, matchMethod=CONTOUR_MATCHING): self.searchImage = searchImage print letterEnum self.initializeLetterTemplate(letterEnum) self.getContours(searchImage) return self.findMatchingContours() def initializeLetterTemplate(self, letterEnum): if letterEnum == LabelRecognizer.B_FROM_LABEL: fileName = "letterTemplates/Bcrop.JPG" elif letterEnum == LabelRecognizer.R_FROM_LABEL: # retake Rcrop (wrinkle in wrapper) fileName = "letterTemplates/Rcrop.JPG" elif letterEnum == LabelRecognizer.A_FROM_LABEL: fileName = "letterTemplates/Acrop.JPG" elif letterEnum == LabelRecognizer.C_FROM_LABEL: fileName = "letterTemplates/Ccrop.JPG" elif letterEnum == LabelRecognizer.H_FROM_LABEL: fileName = "letterTemplates/Hcrop.JPG" elif letterEnum == LabelRecognizer.S_FROM_LABEL: fileName = "letterTemplates/Scrop.JPG" img = self.basicFunctions.readColorImageFromFile(fileName) gray = self.basicFunctions.convertColorToGrayscale(img) if letterEnum == LabelRecognizer.R_FROM_LABEL: gray = cv2.equalizeHist(gray) blurred = cv2.GaussianBlur(gray, (101, 101), 3) else: blurred = cv2.GaussianBlur(gray, (7, 7), 3) edges = cv2.Canny(blurred, 50, 100) edges = self.morpher.dilateWithSquare(edges, 2) edges = self.morpher.closeWithSquare(edges, 3) self.basicFunctions.showImage(edges) contours = self.basicFunctions.findContours(edges) drawnOn = self.basicFunctions.drawContours(img, contours[1]) drawOn = self.basicFunctions.drawContours(img, contours[1], contourIdx=(len(contours[1]) - 1)) self.basicFunctions.showImage(drawOn) print type(contours[1]) if letterEnum == LabelRecognizer.R_FROM_LABEL: self.letterContour = contours[1][len(contours[1]) - 2] else: self.letterContour = contours[1][len(contours[1]) - 1] self.basicFunctions.showImage(self.basicFunctions.drawContours(img, [self.letterContour])) self.letterWHRatio = calcBoundingRectRatio(self.letterContour) self.letterPerimAreaRatio = calculatePerimAreaRatio(self.letterContour) def findMatchingContours(self): matchingContours = [] for i in range(len(self.imgContours)): result = cv2.matchShapes(self.letterContour, self.imgContours[i], 2, 0) if result <= self.correlationCutoff: if abs(self.letterPerimAreaRatio - calculatePerimAreaRatio(self.imgContours[i]) < self.perimAreaCutoff): if abs(self.letterWHRatio - calcBoundingRectRatio(self.imgContours[i]) < self.whRatioCutoff): matchingContours.append(self.imgContours[i]) print len(matchingContours) return tuple(matchingContours) def getContours(self, searchImage): # search image assumed to be color image """ gray2 = self.basicFunctions.convertColorToGrayscale(searchImage) blurred = cv2.GaussianBlur(gray2, (5, 5), 3) edges2 = cv2.Canny(blurred, 50, 100) edges = self.morpher.dilateWithSquare(edges2, 4) edges = self.morpher.closeWithSquare(edges2, 5) self.basicFunctions.showImage(edges2) """ gray = self.basicFunctions.convertColorToGrayscale(searchImage) blurred = cv2.GaussianBlur(gray, (5, 5), 3) edges = cv2.Canny(blurred, 50, 100) edges = self.morpher.dilateWithSquare(edges, 2) edges = self.morpher.closeWithSquare(edges, 3) contours = self.basicFunctions.findContours(edges) self.imgContours = contours[1] self.basicFunctions.showImage(self.basicFunctions.drawContours(searchImage, contours[1])) print len(self.imgContours)
def filter2( image, debug ): ##### #Filter out image wrapperBool = False basicFunctions = BasicFunctions() img = image original = deepcopy(image) maskLogo = deepcopy(img) maskWrapper = deepcopy(img) height, width, channels = img.shape counter = 0 pixcounter = 0 ########## #LOGO MASK for x in range(0,height): for y in range(0,width): pxR = img[x,y,2] pxG = img[x,y,1] pxB = img[x,y,0] if ~((pxR == 0) and (pxB == 0) and (pxG == 0)): pixcounter = pixcounter + 1 if (isPurple(pxR,pxG,pxB)): counter = counter + 1 if ( (isPink(pxR,pxG,pxB)) | (isPurple(pxR,pxG,pxB)) ): maskLogo[x,y] = [255,255,255] else: maskLogo[x,y] = [0,0,0] ##Get the logo score from the amount of purple in the image logoperc = float(counter)/(pixcounter) print "Logo Percentage = %s" % logoperc if (logoperc >= 0.01) and (logoperc <= 0.50): wrapperBool = True morpher = ImageMorpher() maskLogo = morpher.closeWithSquare(maskLogo,1) maskLogo = morpher.dilateWithSquare(maskLogo,1) for x in range(0,height): for y in range(0,width): pxR = img[x,y,2] pxG = img[x,y,1] pxB = img[x,y,0] pxRl = maskLogo[x,y,2] pxGl = maskLogo[x,y,1] pxBl = maskLogo[x,y,0] if ((pxRl != 0) and (pxGl != 0) and (pxBl != 0)): if ((pxR != 0) and (pxG != 0) and (pxB != 0)): img[x,y] = maskLogo[x,y] ############# #WRAPPER MASK for x in range(0,height): for y in range(0,width): pxR = img[x,y,2] pxG = img[x,y,1] pxB = img[x,y,0] if (isWrapper(pxR,pxG,pxB) or isSomethingElse(pxR,pxG,pxB)): maskWrapper[x,y] = [255,255,255] else: maskWrapper[x,y] = [0,0,0] maskWrapper = morpher.closeWithSquare(maskWrapper,2) maskWrapper = morpher.dilateWithSquare(maskWrapper,2) for x in range(0,height): for y in range(0,width): #print img pxR = img[x,y,2] pxG = img[x,y,1] pxB = img[x,y,0] pxRw = maskWrapper[x,y,2] pxGw = maskWrapper[x,y,1] pxBw = maskWrapper[x,y,0] if ((pxRw != 0) and (pxGw != 0) and (pxBw != 0)): if ((pxR != 0) and (pxG != 0) and (pxB != 0)): img[x,y] = [0,0,0] ######################## #Final Pass for contours temp = deepcopy(img) tempgray = deepcopy(img) tempgray = cv2.cvtColor(tempgray,cv2.COLOR_BGR2GRAY) thresh = deepcopy(img) for x in range(0,height): for y in range(0,width): pxR = img[x,y,2] pxG = img[x,y,1] pxB = img[x,y,0] if ((pxR != 0) and (pxG != 0) and (pxB != 0)): thresh[x,y] = [255,255,255] thresh = cv2.cvtColor(thresh,cv2.COLOR_BGR2GRAY) removing = True removalpass = 1 while(removing): contoured_img = deepcopy(thresh) contoured_img, contours, hierarchy = cv2.findContours(contoured_img,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE) removing = False for j in range(0,len(contours)): cnt = contours[j] area = cv2.contourArea(cnt) parent = hierarchy[0,j,3] if ((area <= 3000)): removing = True if (removing == False): break removalpass = removalpass + 1 for i in range(0, len(contours)): cnt = contours[i] area = cv2.contourArea(cnt) parent = hierarchy[0,i,3] if ((area <= 3000) and (parent == -1)): cv2.drawContours(thresh,cnt,-1,0,thickness=cv2.FILLED) cv2.drawContours(img,cnt,-1,(0,0,0),thickness=cv2.FILLED) if ((area <= 3000) and (parent != -1)): thresh = basicFunctions.fillContourGray(thresh,cnt,255) img = basicFunctions.fillContourColor(img,cnt,255,255,255) if (debug): plt.subplot(161), plt.imshow(original) plt.subplot(162), plt.imshow(maskLogo) plt.subplot(163), plt.imshow(maskWrapper) plt.subplot(164), plt.imshow(temp) plt.subplot(165), plt.imshow(thresh) plt.subplot(166), plt.imshow(img) plt.show() return img, logoperc