Пример #1
0
def byThreshold(img):
    grad = getGradient(img, useGradient=False)
    im2, contours, hierarchy = cv2.findContours(grad, cv2.RETR_EXTERNAL,
                                                cv2.CHAIN_APPROX_SIMPLE)
    for cnt in contours:
        x, y, w, h = cv2.boundingRect(cnt)
        cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 1)
    return tl.concat_ver((img, grad))
Пример #2
0
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
Пример #3
0
def segmentSymbolsByContours(img):
    vis = cv2.cvtColor((255 - img), cv2.COLOR_GRAY2BGR)
    gray = 255 - img
    '''
        Binarization
    '''
    ret, th = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

    im2, contours, hierarchy = cv2.findContours(th, cv2.RETR_EXTERNAL,
                                                cv2.CHAIN_APPROX_SIMPLE)

    textRegions = []
    for cnt in contours:
        x, y, w, h = cv2.boundingRect(cnt)

        if w * h > 10:
            textRegions.append((x, y, x + w, y + h))
            cv2.rectangle(vis, (x, y), (x + w, y + h), (0, 0, 255), 1)
            break

    bigImg = tl.concat_ver((vis, gray, th))
    return bigImg, img[y:y + h, x:x + w]
        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)

    indx = 1 if imagepath.find(categories[0]) != -1 else 0
    totalPlates[indx] += 1
    totalChars[indx] += total
    foundedChars[indx] += founded
    if total == founded:
        fullPlates[indx] += 1

    framenum += 1

    fimg = tl.concat_ver((imgBand, thres, _horpDr))
    #cv2.imshow("Result", cv2.resize(fimg,(0,0),fx=3,fy=3))
    #cv2.waitKey(0)

    cv2.imwrite("debug_imgs/Segmentation/"+str(framenum)+".jpg", fimg)

for i in range(len(categories)):
    print ""
    print "##########", categories[i - 1]
    print "########## From %d characters founded %d not founded %d" % (totalChars[i], foundedChars[i], totalChars[i]-foundedChars[i])
    print "########## From %d plates totally segmented %d not segmented %d" % (totalPlates[i], fullPlates[i], totalPlates[i]-fullPlates[i])
Пример #5
0
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)
Пример #6
0
meanHeight = hsum / float(len(contours))

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)
Пример #7
0
import numpy as np
import tools as tl
import genSmallTextImg as gsti

img, text = gsti.genSmallTextImg()
# gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# blur = cv2.blur(img,(21,1))
vis = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
img_inv = 255 - img
filtered = cv2.blur(img_inv, (5, 5))
ret_o, th_o = cv2.threshold(filtered, 0, 255, cv2.THRESH_OTSU)
im2, countours, hier = cv2.findContours(th_o, cv2.RETR_EXTERNAL,
                                        cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(vis, countours, -1, (255, 0, 0), 1)

hsum = 0
for cnt in countours:
    (x, y, w, h) = cv2.boundingRect(cnt)
    hsum += h

meanH = hsum / float(len(countours))

for cnt in countours:
    (x, y, w, h) = cv2.boundingRect(cnt)
    print(h)
    if (h > meanH - meanH * 0.1):
        cv2.rectangle(vis, (x, y), (x + w, y + h), (0, 0, 255), 1)

final = tl.concat_ver((img, vis))
cv2.imshow("Sudoku", final)
cv2.waitKey(0)
Пример #8
0
            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()
Пример #9
0
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

ret, th = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
print ret

kernel = np.ones((5, 5), dtype=np.uint8)

# erosion = cv2.erode(th, kernel, iterations = 1)
# dilation = cv2.dilate(erosion, kernel, iterations = 1)

closing = cv2.morphologyEx(th, cv2.MORPH_OPEN, kernel, iterations=2)

im2, countours, hier = cv2.findContours(closing, cv2.RETR_EXTERNAL,
                                        cv2.CHAIN_APPROX_SIMPLE)

print len(countours)

vis = cv2.cvtColor(closing, cv2.COLOR_GRAY2BGR)
cv2.drawContours(vis, countours, -1, (0, 255, 0), -1)

for cnt in countours:
    x, y, w, h = cv2.boundingRect(cnt)

    cv2.rectangle(vis, (x, y), (x + w, y + h), (0, 0, 255), 1)

final_img = tl.concat_ver((img, th, vis))
scale = 0.9
cv2.namedWindow("Final", cv2.WINDOW_AUTOSIZE)
cv2.imshow("Final", cv2.resize(final_img, (0, 0), fx=scale, fy=scale))
cv2.waitKey(0)
Пример #10
0
            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)

    indx = 1 if imagepath.find(categories[0]) != -1 else 0
    totalPlates[indx] += 1
    totalChars[indx] += total
    foundedChars[indx] += founded
    if total == founded:
        fullPlates[indx] += 1

    framenum += 1

    fimg = tl.concat_ver((imgBand, bright, equ, thres, _horpDr))

    cv2.imwrite("debug_imgs/Segmentation/" + str(framenum) + ".jpg", fimg)

for i in range(len(categories)):
    print ""
    print "##########", categories[i - 1]
    print "########## From %d characters founded %d not founded %d" % (
        totalChars[i], foundedChars[i], totalChars[i] - foundedChars[i])
    print "########## From %d plates totally segmented %d not segmented %d" % (
        totalPlates[i], fullPlates[i], totalPlates[i] - fullPlates[i])
Пример #11
0
                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()