def process(img, blocks): cv2.imshow("img", img) cv2.imshow("blocks", blocks) C, M, Y, K = util.cvtColorBGR2CMYK_(blocks) #convert to CMYK #imgNoBgnd = getUniformBackGround(img, K) #get uniform background #cv2.imshow("imgnobgnd", imgNoBgnd) invWhiteLines = getWhiteLines(C, M, Y, K) #get only white lines #cv2.imshow("White Lines", invWhiteLines) #remove white lines from image invWhite3 = cv2.merge([invWhiteLines, invWhiteLines, invWhiteLines]) #imgBlocks = cv2.bitwise_and(imgNoBgnd, invWhite3) imgBlocks = cv2.bitwise_and(img, invWhite3) #imgBlocks = blocks #find the blocks in imgBlocks imgray = cv2.cvtColor(imgBlocks,cv.CV_RGB2GRAY); cv2.imshow("grayscale for blocks", imgray) _,imbw = cv2.threshold(imgray, 0, 255, cv2.THRESH_BINARY) cv2.imshow("bw for blocks", imbw) #cv2.imshow("img Blocks", imgBlocks) ctrs = cv2.findContours(imbw, cv2.RETR_LIST , cv2.CHAIN_APPROX_SIMPLE); #imgCopy = img rectList = getRects(img, ctrs) #img #print "B:\tG:\tR:" for i in range(len(rectList)): #print rectList[i] #(x,y),(w,h),t = rectList[i][1] #print x,y,w,h,t #x,y,w,h = rectList[i][0] rec = np.asarray(cv.GetSubRect(cv.fromarray(imgBlocks),rectList[i][0])) cv2.imshow(str(i), rec)
def process(img): C, M, Y, K = util.cvtColorBGR2CMYK_(img) #convert to CMYK imgNoBgnd = getUniformBackGround(img, K) #get uniform background invWhiteLines = getWhiteLines(C, M, Y, K) #get only white lines #remove white lines from image invWhite3 = cv2.merge([invWhiteLines, invWhiteLines, invWhiteLines]) imgBlocks = cv2.bitwise_and(imgNoBgnd, invWhite3) cv2.imshow("White Lines", invWhiteLines) #find the blocks in imgBlocks imgray = cv2.cvtColor(imgBlocks,cv.CV_RGB2GRAY); _,imbw = cv2.threshold(imgray, 20, 255, cv2.THRESH_BINARY_INV) ctrs = cv2.findContours(imbw, cv2.RETR_LIST , cv2.CHAIN_APPROX_SIMPLE); rectList = drawRects(img, ctrs) #img for i in range(len(rectList)): #print rectList[i][1] #(x,y),(w,h),t = rectList[i][1] #print x,y,w,h,t #x,y,w,h = rectList[i][0] rec = np.asarray(cv.GetSubRect(cv.fromarray(imgBlocks),rectList[i][0])) cv2.imshow(str(i), rec) #print rec.shape rh,rw,rc = rec.shape print rec[rh/2][rw/2][0],rec[rh/2][rw/2][1],rec[rh/2][rw/2][2] Cfinal, Mfinal, Yfinal, Kfinal = util.cvtColorBGR2CMYK_(rec) print Cfinal[rh/2][rw/2],Mfinal[rh/2][rw/2],Yfinal[rh/2][rw/2],Kfinal[rh/2][rw/2] hsv = cv2.cvtColor(rec, cv.CV_BGR2HSV) print hsv[rh/2][rw/2][0],hsv[rh/2][rw/2][1],hsv[rh/2][rw/2][2] #cv2.imshow('hsv'+str(i),hsv) print "***********************" #brown, orange, maroon destr = cv2.inRange(rec, np.array((50, 85, 150)), np.array((255, 255, 255))); cv2.imshow("dR"+str(i), destr) #orange destg = cv2.inRange(rec, np.array((10, 150, 200)), np.array((255, 255, 255))); cv2.imshow("dG"+str(i), destg) #brown, destb = cv2.inRange(rec, np.array((100, 100, 150)), np.array((255, 255, 255))); cv2.imshow("dB"+str(i), destb) #blue destl = cv2.inRange(rec, np.array((140, 10, 10)), np.array((255, 255, 255))); cv2.imshow("dl"+str(i), destl) #green, brown, blue deste = cv2.inRange(rec, np.array((0, 10, 10)), np.array((255, 255, 255))); cv2.imshow("de"+str(i), deste) #cv2.imshow("CF"+str(i),Cfinal) #cv2.imshow("MF"+str(i),Mfinal) #cv2.imshow("YF"+str(i),Yfinal) #cv2.imshow("KF"+str(i),Kfinal) #Process the CMYK of imgBlocks #The processing is necessary to find the color of the blocks. Cfinal, Mfinal, Yfinal, Kfinal = util.cvtColorBGR2CMYK_(imgBlocks) #convert cfinal = np.uint8(Cfinal*255) _,cfinalbw = cv2.threshold(cfinal, 20, 255, cv2.THRESH_BINARY_INV) cv2.imshow("asdsd",cfinalbw) key = cv2.waitKey()
def process(self, imageIn, timeNow): self.image = imageIn if self.gui: self.imageOut = self.image.copy() self.blobs = [] imageColors, imageWhites = hsv(self.image) if self.gui: cv2.imshow("imageColors", imageColors) cv2.imshow("imageWhites", imageWhites) C, M, Y, K = util.cvtColorBGR2CMYK_(imageWhites) #convert to CMYK #imgNoBgnd = getUniformBackGround(imageColors, K) #get uniform background #cv2.imshow("imgnobgnd", imgNoBgnd) invWhiteLines = getWhiteLines(C, M, Y, K) #get only white lines #cv2.imshow("White Lines", invWhiteLines) #remove white lines from imageColors invWhite3 = cv2.merge([invWhiteLines, invWhiteLines, invWhiteLines]) #imgBlocks = cv2.bitwise_and(imgNoBgnd, invWhite3) imgBlocks = cv2.bitwise_and(imageColors, invWhite3) #imgBlocks = imageWhites #find the imageWhites in imgBlocks imgray = cv2.cvtColor(imgBlocks,cv.CV_RGB2GRAY); if self.gui: cv2.imshow("grayscale for imageWhites", imgray) _,imbw = cv2.threshold(imgray, 0, 255, cv2.THRESH_BINARY) if self.gui: cv2.imshow("bw for imageWhites", imbw) #cv2.imshow("imgBlocks", imgBlocks) # Contour processing: Old way ''' ctrs = cv2.findContours(imbw, cv2.RETR_LIST , cv2.CHAIN_APPROX_SIMPLE); #imgCopy = imageColors rectList = getRects(ctrs, imageOut) self.logd("processBlobs", "{0} rects".format(len(rectList))) ''' # Contour processing: New way # * Find contours contours, _ = cv2.findContours(imbw, cv2.RETR_LIST , cv2.CHAIN_APPROX_SIMPLE) # * Walk through list of contours for contour in contours: contour = contour.astype(np.int32) # convert contours to 32-bit int for each individual contour # ** Compute blob properties to be used for filtering area = cv2.contourArea(contour) bbox = cv2.boundingRect(contour) x, y, w, h = bbox # ** Filter out ones that are too small or too big if area < BlobTracker.minBlobArea or area > BlobTracker.maxBlobArea: continue # ** Process only vertical rectangles (ie, w<h) with w and h > some threshold if w >= h or w <= self.minBlobWidth or h <= self.minBlobHeight: continue # ** Compute additional blob properties to be stored rect = cv2.minAreaRect(contour) length = "unknown" if h > 173: length = "large" elif h > 140: length = "medium" elif h > 100: length = "small" # ** Identify color (name) imageBlob = imgBlocks[y:y+h, x:x+w] #imageBlob = np.asarray(cv.GetSubRect(cv.fromarray(imgBlocks),rectList[i][0])) color_bgr = cv2.mean(imageBlob) color_obj = findMatchColorBGR(colors, color_bgr) #self.logd("process", "color_obj: {0}".format(color_obj)) color = color_obj.name if color_obj is not None else "none" # ** Create blob object blob = SimpleBlob(area=area, bbox=bbox, rect=rect, length=length, color_bgr=color_bgr, color=color) # ** Apply additional filter(s) on computed blob properties # NOTE density is not being calculated in SimpleBlob #if blob.density < BlobTracker.minBlobDensity: continue # ** Add to blobs list; TODO resolve with existing blob objects and update properties if a match is found? self.blobs.append(blob) # Blob drawing: Old way ''' if self.gui: #print "B:\tG:\tR:" for i in range(len(rectList)): #print rectList[i] #(x,y),(w,h),t = rectList[i][1] #print x,y,w,h,t #x,y,w,h = rectList[i][0] cv2.imshow(str(i), imageBlob) #showHist(imageBlob, i) #print imageBlob.shape #rh,rw,rc = imageBlob.shape #print imageBlob[rh/2][rw/2][0],imageBlob[rh/2][rw/2][1],imageBlob[rh/2][rw/2][2] #Cfinal, Mfinal, Yfinal, Kfinal = util.cvtColorBGR2CMYK_(imageBlob) #print Cfinal[rh/2][rw/2],Mfinal[rh/2][rw/2],Yfinal[rh/2][rw/2],Kfinal[rh/2][rw/2] #hsv = cv2.cvtColor(imageBlob, cv.CV_BGR2HSV) #print hsv[rh/2][rw/2][0],hsv[rh/2][rw/2][1],hsv[rh/2][rw/2][2] #cv2.imshow('hsv'+str(i),hsv) #print "***********************" ''' self.blobs.sort(key=lambda blob: blob.bbox) # sort blob list by bbox (left-to-right, top-to-bottom, ...) # * Report and draw blobs self.logd("process", "{0} blobs".format(len(self.blobs))) for blob in self.blobs: self.logd("process", str(blob)) #blob.detail() if self.gui: blob.draw(self.imageOut) if self.gui: cv2.rectangle(self.imageOut, (318,0), (322,640), (255,255,255), -1) # draw center line return True, self.imageOut