def generateData(vis=False): chars = string.ascii_uppercase + string.digits # chars = string.digits font = np.arange(0, 6, 1) #randint(0, 5) # size = np.arange(2.5, 4, 0.5) #uniform(2.5, 3.5) line_size = np.arange(1, 4, 1) #randint(1, 3) blur = np.arange(0, 2, 1) #randint(0, 1) kw = np.arange(1, 9, 2) #randint(3, 7) kh = np.arange(1, 9, 2) #randint(3, 7) generatedImgs = [] rows = [] for c in chars: print("Generating - ", c) row = [] for f in font: for l in line_size: # for b in blur: for i in kw: for j in kh: img, _ = genSymbolImg(c, f, l, 1, i, j) # _, croped = segmentSymbolsByContours(img) resized = cv2.resize(img, (30, 40)) row.append(resized) # cv2.imshow("W", resized) # k = cv2.waitKey(0) # if k == 27: # exit() generatedImgs.append(row) rows.append(tl.concat_hor(row)) # cv2.imshow("ROW", tl.concat_hor(row)) # k = cv2.waitKey(0) # if k == 27: # break print("Generation done") generatedVisImg = tl.concat_ver(rows) return generatedImgs, generatedVisImg
import cv2 import numpy as np import tools as tl img = cv2.imread("sudoku.png", 0) ret, th = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) ret, th_inv = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV) th_adap11 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2) th_adap5 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2) ret_o, th_o = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU) print(ret_o) print(cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU) row1 = tl.concat_hor((img, th, th_o)) row2 = tl.concat_hor((img, th_adap11, th_adap5)) final_img = tl.concat_ver((row1, row2)) cv2.imshow("Sudoku", final_img) cv2.waitKey(0)
textRegions = [] for box in reversed(boxRegions): if box[3] > meanHeight: x, y, w, h = box textRegions.append((x, y, x + w, y + h)) cv2.rectangle(vis, (x, y), (x + w, y + h), (0, 255, 0), 1) textImg = [] for i in textRegions: x1, y1, x2, y2 = i textImg.append(img[y1:y2, x1:x2]) allTexts = tl.concat_ver(textImg) row1 = tl.concat_hor((img, img_inv, filtered)) row2 = tl.concat_hor((th_o, closing, vis, allTexts)) # final = tl.concat_ver((row2)) cv2.imshow('myimg', cv2.resize(row2, (0, 0), fx=1, fy=1)) cv2.waitKey(0) # for root, dirs, files in os.walk("../Data/"): # for file in files: # imagepath = os.path.join(root, file) # if file.endswith('.jpg'): # img = cv2.imread(imagepath) # ''' # ToDo: find text areas
basicHeuristicAnalysisRanges = temp[temp[:, 4].argsort()] basicHeuristicAnalysisRanges = basicHeuristicAnalysisRanges.astype(int) '''''' '''''' '''''' '''''' ''' Deep Heuristic Analysis ''' '''''' '''''' '''''' '''''' _verprojsBHA = np.zeros((1, img.shape[1]), dtype=np.uint8) for r in basicHeuristicAnalysisRanges: _band = gray[r[0]:r[1], r[2]:r[3]] if _band.shape[0] > 0 and _band.shape[1] > 0: bmin = np.min(_band) bmax = np.max(_band) bmid = int((bmax - bmin) / 2) hist, bins = np.histogram(_band.ravel(), 256, [0, 256]) b2 = np.sum(hist[bmid:bmax]) - np.sum(hist[bmin:bmid]) # Debug Heuristic analysis _img = img[r[0]:r[1], r[2]:r[3]] _color = (0, 255, 0) if b2 > 0 else (0, 0, 255) _img = cv2.copyMakeBorder(_img, 2, 2, 2, 2, cv2.BORDER_CONSTANT, value=_color) _verprojsBHA = tl.concat_ver2((_verprojsBHA, _img)) showImg = tl.concat_hor((img, _verprojsBHA)) cv2.imwrite("debug_imgs/HBA/" + str(framenum) + ".jpg", showImg) framenum += 1
verP = np.sum(gray, axis=1) / 255 verPVis = tl.getDrawProjectionVer(vis, verP) gradX = cv2.Sobel(gray, ddepth=cv2.CV_64F, dx=1, dy=0, ksize=3) gradX = np.abs(gradX) (minVal, maxVal) = (np.min(gradX), np.max(gradX)) if maxVal - minVal > 0: gradX = (255 * ( (gradX - minVal) / float(maxVal - minVal))).astype("uint8") else: gradX = np.zeros(gray.shape, dtype="uint8") verPX = np.sum(gradX, axis=1) / 255 verPXVis = tl.getDrawProjectionVer(vis, verPX) row1 = tl.concat_hor((gray, verPVis)) row2 = tl.concat_hor((gradX, verPXVis)) bigImg = tl.concat_ver((row1, row2)) scale = 0.8 cv2.imshow("Window2", cv2.resize(bigImg, (0, 0), fx=scale, fy=scale)) cv2.moveWindow("Window2", 0, 0) k = cv2.waitKey(0) if k == 27: exit() elif k == ord('q'): exit()
import cv2 import numpy as np import tools as tl img = cv2.imread("sudoku.png", 0) # to be black-white ret, th = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) ret, th_inv = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV) th_adap11 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2) th_adap5 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 5, 2) #adaptive treshold divivedv the picture and нахожит порог для каждого #поэтому он ничего не возвращает ret_0, th_0 = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU) print(ret_0) print(cv2.THRESH_BINARY + cv2.THRESH_OTSU) row1 = tl.concat_hor((img, th, th_0)) row2 = tl.concat_hor((img, th_adap5, th_adap11)) final_img = tl.concat_ver((row1, row2)) cv2.imshow("Sodoku", final_img) cv2.waitKey(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: yt, yb = band bandsImg[yt:yb] = [0, 255, 0] vis = cv2.addWeighted(vis, 0.6, bandsImg, 0.4, 0) ''' Find peaks Row Clipping Phase 2 ''' # TODO crop words using vertical projections ''' Final drawing ''' scale1 = 0.8 bigImg1 = tl.concat_hor( (vis, grad1, drawedverp, sliceLine, drawedverpConvolved)) cv2.imshow("Window1", cv2.resize(bigImg1, (0, 0), fx=scale1, fy=scale1)) k = cv2.waitKey(0) if k == 27: exit() elif k == ord('q'): exit()
if r[6]: _color = (0, 255, 0) if globalFlagDetected == False: if r[5] == maxDensity: _color = (0, 255, 0) elif r[5] == secondMaxDensity: _color = (212, 255, 127) elif r[5] == thirdMaxDensity: _color = (255, 255, 0) if r[4] < 260: _color = (0, 191, 255) if len(basicHeuristicAnalysisRanges) == 1: _color = (0, 255, 0) _img = tl.concat_hor((_img, r[7], r[8])) _img = cv2.copyMakeBorder(_img, 2, 2, 2, 2, cv2.BORDER_CONSTANT, value=_color) _verprojsBHA = tl.concat_ver2((_verprojsBHA, _img)) showImg = tl.concat_hor((img, _verprojsBHA)) cv2.imwrite("debug_imgs/HBA/" + str(framenum) + ".jpg", showImg) framenum += 1
if box[3] > meanHeight: x,y,w,h = box textRegions.append((x,y, x+w,y+h)) cv2.rectangle(vis,(x,y),(x+w,y+h),(0,0,255),1) ''' Extract all text regions ''' regionImgs = [] for textRegion in textRegions: x1,y1,x2,y2 = textRegion regionImgs.append(img[y1:y2,x1:x2]) scale = 0.8 row1 = tl.concat_hor((vis, gradX, blur)) row2 = tl.concat_hor((th, opening)) allTexts = tl.concat_ver(regionImgs) bigImg = tl.concat_ver((row1, row2)) cv2.imshow("Window1", cv2.resize(bigImg, (0,0), fx = scale, fy = scale)) cv2.imshow("Window2", cv2.resize(allTexts, (0,0), fx = 1.4, fy = 1.4)) k = cv2.waitKey(0) if k == 27: exit() elif k == ord('q'): exit()