def AddSegment(xm, color): xl = tl.findb0(horp, xm, 0.7 * horp[xm]) xr = tl.findb1(horp, xm, 0.7 * horp[xm]) horp[xl:xr] = 0 if len(verticalSegments) > 0: if np.isclose(xm, verticalSegments, atol=15).any(): return verticalSegments.append(xm) cv2.line(imgBand, (xm, 0), (xm, imgBand.shape[0]), color, 1) cv2.line(bright, (xm, 0), (xm, bright.shape[0]), (0, 0, 255), 1) cv2.line(equ, (xm, 0), (xm, equ.shape[0]), (0, 0, 255), 1) cv2.line(thres, (xm, 0), (xm, thres.shape[0]), (0, 0, 255), 1) cv2.line(_horpDr, (xm, 0), (xm, _horpDr.shape[0]), (0, 0, 255), 1)
def byGradient(img): grad = getGradient(img, y=1, useGradient=True) proj = np.sum(grad, axis=0) / 255 drawProj = tl.getDrawProjectionHor(img, proj) y = img.shape[0] #medVal = median(img, proj, drawProj) #print medVal bandP1ranges = [] peaks = [] height = drawProj.shape[0] limit = int(np.max(proj) * 0.25) while np.max(proj) > limit: ybm = np.argmax(proj) c1 = 0.2 c2 = 0.2 yb0 = tl.findb0(proj, ybm, c1 * proj[ybm]) yb1 = tl.findb1(proj, ybm, c2 * proj[ybm]) if yb1 - yb0 > 4: bandP1ranges.append((yb0, yb1)) peaks.append((int(ybm), height - proj[ybm])) proj[yb0:yb1] = 0 # draw peaks #for peak in peaks: #cv2.circle(drawProj, peak, 1, (0,0,255)) images = [] for band in bandP1ranges: x1, x2 = band crop = img[0:y, x1:x2] #crop = cv2.resize(crop, (128, 128), 0, 0, cv2.INTER_LINEAR) images.append(crop) #print(crop) #cv2.line(img, (x1, 0), (x1, 100), (0, 0, 255), 1) #cv2.line(img, (x2, 0), (x2, 100), (0, 0, 255), 1) return images
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Plate segmentation ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' thres = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 3) horp = tl.projectionHor(thres) _horpDr = tl.getDrawProjectionHor(gray, horp) vm = np.max(horp) va = np.mean(horp) vb = 2*va-vm verticalSegments = [] while True: xm = np.argmax(horp) if horp[xm] > 0.86 * vm: xl = tl.findb0(horp, xm, 0.7 * horp[xm]) xr = tl.findb1(horp, xm, 0.7 * horp[xm]) horp[xl:xr] = 0 verticalSegments.append(xm) cv2.line(imgBand,(xm,0),(xm,imgBand.shape[0]),(0,0,255),1) else: break verticalSegments.append(0) verticalSegments.append(imgBand.shape[1]-1) verticalSegments = np.asarray(verticalSegments, dtype=np.int) verticalSegments.sort() # print verticalSegments total, founded = dtl.getSegmentation1Accuracy(verticalSegments, maskCharBand)
''' kernel = medianHeight verpConvolved = np.convolve(verp, np.ones((kernel, )) / kernel, mode='same') drawedverpConvolved = tl.getDrawProjectionVer(vis, verpConvolved) ''' Find peaks Band Clipping Phase 1 ''' bandP1ranges = [] peaks = [] c1 = 0.2 c2 = 0.4 while np.max(verpConvolved) > 10: ybm = np.argmax(verpConvolved) yb0 = tl.findb0(verpConvolved, ybm, c1 * verpConvolved[ybm]) yb1 = tl.findb1(verpConvolved, ybm, c2 * verpConvolved[ybm]) if yb1 - yb0 > medianHeight: bandP1ranges.append((yb0, yb1)) peaks.append((int(verpConvolved[ybm]), ybm)) verpConvolved[yb0:yb1] = 0 # draw peaks for peak in peaks: cv2.circle(drawedverpConvolved, peak, 2, (0, 0, 255), -1) # draw bands bandsImg = np.zeros(vis.shape, dtype=np.uint8) for band in bandP1ranges: