コード例 #1
0
def lineDrawing(image):
    # load image and create slightly smaller image due to gradients being smaller
    image_mat = support.loadImage(image)
    r, c = support.getSize(image_mat)
    image_mat = image_mat[1:r - 1, 1:c - 1]
    #print(image_mat[0:5,0:5])
    #image_mat = image_mat / 255
    #print(image_mat[0:5,0:5])

    Kx, Ky = utils.makeKernel('sobel')
    Kx, Ky = normalizeMatrix(Kx), normalizeMatrix(Ky)
    Gx, Gy, G, D = utils.getEdges(image, Kx, Ky)

    Tx, Ty = gradientTangents(Gx, Gy)
    print(Tx[0:10, 0:10])
    print(Ty[0:10, 0:10])
    Tx_p, Ty_p = computeETF(Tx, Ty, G)
    print(Tx_p[0:10, 0:10])
    print(Ty_p[0:10, 0:10])
    print("Saving...")
    support.saveImage(Tx, "Tx_p_test")
    support.saveImage(Ty, "Ty__p_test")
    print("Saved")

    return computeFDoG(image_mat, Tx_p, Tx_p)
コード例 #2
0
def restoreRGB(image, mask):

    # split the image into spherical coordinates
    r, theta, phi = rgbToSphere(image)

    # Make a copy of the matrices
    rNew = r[:]
    tNew = theta[:]
    pNew = phi[:]

    # For a certain amount of time run inpaint algorithm
    for i in range(0, T + 1, 1):

        # inpaint for each sphere matrix
        inpaint(r, rNew, mask)
        inpaint(theta, tNew, mask)
        inpaint(phi, pNew, mask)

        # curImage = newImage, newImage = curImage
        r, rNew = rNew, r
        theta, tNew = tNew, theta
        phi, pNew = pNew, phi

        # Every so many iterations save the result
        if i % step == 0:
            rgb_image = sphereToRgb(rNew, tNew, pNew)
            support.saveImage(rgb_image, "rgb-%s-%04d.png" % (imgName, i))
            #support.showImage(rgb_image)

    return sphereToRgb(rNew, tNew, pNew)
コード例 #3
0
def testTiffany():  #500/2000
    image = support.loadImage('tiffany.pgm')
    resultPoints, resultCells = stipple.stipple(image)

    image = support.loadImageRGB('tiffany.png')
    image = image / 255
    image = colorutils.assignColors(image, resultCells)
    #support.showImage(image)
    support.saveImage(image, "tiffany_2000_0.65")
コード例 #4
0
def testTable():  #1500/5000
    image = support.loadImage('table.pgm')
    resultPoints, resultCells = stipple.stipple(image)

    image = support.loadImageRGB('table.png')
    image = image / 255
    image = colorutils.assignColors(image, resultCells)
    #support.showImage(image)
    support.saveImage(image, "table_1500_0.65")
コード例 #5
0
def testEagle():  # 500/2000
    image = support.loadImage('eagle.pgm')
    resultPoints, resultCells = stipple.stipple(image)

    image = support.loadImageRGB('eagle.png')
    image = image / 255
    image = colorutils.assignColors(image, resultCells)
    #support.showImage(image)
    support.saveImage(image, "eagle_2000_0.5")
コード例 #6
0
def restoreGray(image, mask):

    # Make a copy of the image
    newImage = image[:]
    support.showImage(newImage)

    # For a certain amount of time run inpaint algorithm
    for i in range(0, T + 1, 1):
        inpaint(image, newImage, mask)
        image, newImage = newImage, image

        # Every so many iterations save the result
        if i % step == 0:
            support.saveImage(newImage, "%s-%04d.png" % (imgName, i))
            support.showImage(newImage)

    return newImage
コード例 #7
0
def testRenderTiles():
    result = halftone.halftone("monalisa.pgm", 12, halftone.renderTiles)
    support.saveImage(result, "monalisa_tiles")

    result = halftone.halftone("einstein.pgm", 12, halftone.renderTiles)
    support.saveImage(result, "einstein_tiles")

    result = halftone.halftone("table.pgm", 12, halftone.renderTiles)
    support.saveImage(result, "table_tiles")

    result = halftone.halftone("butterfly.pgm", 14, halftone.renderTiles)
    support.saveImage(result, "butterfly_tiles")
コード例 #8
0
def testRenderDots():
    result = halftone.halftone("monalisa.pgm", 14, halftone.renderDots)
    support.saveImage(result, "monalisa_dots")

    result = halftone.halftone("einstein.pgm", 14, halftone.renderDots)
    support.saveImage(result, "einstein_dots")

    result = halftone.halftone("table.pgm", 14, halftone.renderDots)
    support.saveImage(result, "table_dots")

    result = halftone.halftone("butterfly.pgm", 14, halftone.renderDots)
    support.saveImage(result, "butterfly_dots")
コード例 #9
0
def testRenderOutline():
    result = halftone.halftone("monalisa.pgm", 12, halftone.renderOutline)
    support.saveImage(result, "monalisa_outl")

    result = halftone.halftone("einstein.pgm", 12, halftone.renderOutline)
    support.saveImage(result, "einstein_outl")

    result = halftone.halftone("table.pgm", 12, halftone.renderOutline)
    support.saveImage(result, "table_outl")

    result = halftone.halftone("butterfly.pgm", 12, halftone.renderOutline)
    support.saveImage(result, "butterfly_outl")
コード例 #10
0
def testSmoothAverageCandies():
    # size 3
    K = utils.makeKernel('average', 3)
    image = utils.smoothImage('candies.pgm', K)
    support.saveImage(image, 'candies_average_3_smooth')
    # size 5
    K = utils.makeKernel('average', 5)
    image = utils.smoothImage('candies.pgm', K)
    support.saveImage(image, 'candies_average_5_smooth')
    # size 7
    K = utils.makeKernel('average', 7)
    image = utils.smoothImage('candies.pgm', K)
    support.saveImage(image, 'candies_average_7_smooth')
    # size 9
    K = utils.makeKernel('average', 9)
    image = utils.smoothImage('candies.pgm', K)
    support.saveImage(image, 'candies_average_9_smooth')
コード例 #11
0
def testSmoothGaussPeppers():
    # sig = 0.5 size = 3
    K = utils.makeKernel('gauss', 3, 0.5)
    image = utils.smoothImage('peppers.pgm', K)
    support.saveImage(image, 'peppers_gauss_.5_smooth')
    # sig = 1 size = 7
    K = utils.makeKernel('gauss', 7, 1)
    image = utils.smoothImage('peppers.pgm', K)
    support.saveImage(image, 'peppers_gauss_1_smooth')
    # sig = 2 size = 13
    K = utils.makeKernel('gauss', 13, 2)
    image = utils.smoothImage('peppers.pgm', K)
    support.saveImage(image, 'peppers_gauss_2_smooth')
    # sig = 3 size = 19
    K = utils.makeKernel('gauss', 19, 3)
    image = utils.smoothImage('peppers.pgm', K)
    support.saveImage(image, 'peppers_gauss_3_smooth')
コード例 #12
0
def testLotus(name):
    image = tessellate.tessellate_main('lotus/lotus.png')
    support.saveImage(image, name)
コード例 #13
0
def testEagle(name):
    image = tessellate.tessellate_main('eagle/eagle.png')
    support.saveImage(image, name)
コード例 #14
0
def testLotusIn():
    image = support.loadImageRGB("lotus-in.png")
    drawing = support.loadMatrix("lotus-curves.npy")
    image = image / 255.0
    rgbImage = tessellate.assignColors(image, drawing)
    support.saveImage(rgbImage, "lotus-test.png")
コード例 #15
0
def testTiffany(name):
    image = tessellate.tessellate_main('tiffany/tiffany.png')
    support.saveImage(image, name)
コード例 #16
0
def testLion(name):
    image = tessellate.tessellate_main('lion/lion.png')
    support.saveImage(image, name)
コード例 #17
0
def testRect():
    image = support.loadImage("originals/rect.png")
    mask = support.loadMatrix("originals/rect-mask.npy")
    image = restoreGray(image, mask)
    support.saveImage(image, "rect-9999.png")
コード例 #18
0
def testfig5():
    image = support.loadImage("originals/fig5.png")
    mask = support.loadMatrix("originals/fig5-mask.npy")
    image = restoreGray(image, mask)
    support.saveImage(image, "fig5-9999.png")
コード例 #19
0
def testTiffany():
    image = support.loadImageRGB("originals/tiffany.png")
    image = impress.impress(image)
    support.showImage(image)
    support.saveImage(image, 'tiffany_color' + color_plate)
コード例 #20
0
def testRiver():
    image = support.loadImageRGB("originals/riverfront.png")
    image = impress.impress(image)
    support.showImage(image)
    support.saveImage(image, 'riverfront_color' + color_plate)
コード例 #21
0
def testDesktop():
    image = support.loadImageRGB("originals/desktop.png")
    image = impress.impress(image)
    support.showImage(image)
    support.saveImage(image, 'desktop_color' + color_plate)
コード例 #22
0
def testGetEdgesPeppers():
    # prewitt
    Kx, Ky = utils.makeKernel('prewitt')
    Gx, Gy, G, D = utils.getEdges('peppers.pgm', Kx, Ky)
    support.saveImage(Gx, 'peppers_prewitt_edges_gx')
    support.saveImage(Gy, 'peppers_prewitt_edges_gy')
    support.saveImage(G, 'peppers_prewitt_edges_g')

    # sobel
    Kx, Ky = utils.makeKernel('sobel')
    Gx, Gy, G, D = utils.getEdges('peppers.pgm', Kx, Ky)
    support.saveImage(Gx, 'peppers_sobel_edges_gx')
    support.saveImage(Gy, 'peppers_sobel_edges_gy')
    support.saveImage(G, 'peppers_sobel_edges_g')
コード例 #23
0
def testAnimateRGB():
    image = support.loadImageRGB("originals/animation-rgb.png")
    mask = support.loadMatrix("originals/animation-mask.npy")
    image = restoreRGB(image, mask)
    support.saveImage(image, "rgb-animation-9999.png")
コード例 #24
0
def testAnimate():
    image = support.loadImage("originals/animation.png")
    mask = support.loadMatrix("originals/animation-mask.npy")
    image = restoreGray(image, mask)
    support.saveImage(image, "animation-9999.png")
コード例 #25
0
def testFlowers():
    image = support.loadImageRGB("originals/flowers.png")
    image = impress.impress(image)
    support.showImage(image)
    support.saveImage(image, 'flowers_color' + color_plate)
コード例 #26
0
import support
#import linedrawing2 as linedrawing
import linedrawing

#images to png
#support.saveImage(support.loadImage('butterfly.pgm'), 'butterfly')
##support.saveImage(support.loadImage('circle-noisy.pgm'), 'circle-noisy')
##support.saveImage(support.loadImage('circle.pgm'), 'circle')
##support.saveImage(support.loadImage('disk.pgm'), 'disk')
##support.saveImage(support.loadImage('eagle-eye.pgm'), 'eagle-eye')
##support.saveImage(support.loadImage('eagle.pgm'), 'eagle')
##support.saveImage(support.loadImage('Einstein.pgm'), 'Einstein')
##support.saveImage(support.loadImage('fingerprint.pgm'), 'fingerprint')
##support.saveImage(support.loadImage('lighthouse.pgm'), 'lighthouse')
##support.saveImage(support.loadImage('mandrill.pgm'), 'mandrill')

support.saveImage(linedrawing.lineDrawing("butterfly.pgm"), "line_butterfly")
support.saveImage(linedrawing.lineDrawing("circle.pgm"), "line_circle")
support.saveImage(linedrawing.lineDrawing("circle-noisy.pgm"),
                  "line_circle-noisy")
support.saveImage(linedrawing.lineDrawing("fingerprint.pgm"),
                  "line_fingerprint")
support.saveImage(linedrawing.lineDrawing("disk.pgm"), "line_disk")
support.saveImage(linedrawing.lineDrawing("eagle-eye.pgm"), "line_eagle-eye")
support.saveImage(linedrawing.lineDrawing("eagle.pgm"), "line_eagle")
support.saveImage(linedrawing.lineDrawing("Einstein.pgm"), "line_Einstein")
support.saveImage(linedrawing.lineDrawing("lighthouse.pgm"), "line_lighthouse")
support.saveImage(linedrawing.lineDrawing("mandrill.pgm"), "line_mandrill")
コード例 #27
0
def testTable():
    image = support.loadImageRGB("originals/table.png")
    image = impress.impress(image)
    support.showImage(image)
    support.saveImage(image, 'table_color' + color_plate)
コード例 #28
0
#test.py

import support
from pixelart import *

image = support.loadImageRGB("obama80x54.png")
image = pixelart(image, 6, 4, 4)
support.showImage(image)
support.saveImage(image, "obama6x4")
コード例 #29
0
def main(image):
    pil_image = Image.open(BytesIO(image))
    image = np.array(pil_image) 
    original = image.copy()
    origHeight, origWidth, _ = image.shape
    ratio = image.shape[0] / scaling
    image = imutils.resize(image, height=scaling)
    newHeight, newWidth, _ = image.shape
    print('starting page extraction ...')
    grayscale = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    blurred = cv2.GaussianBlur(grayscale, (5, 5), 0)
    edged = cv2.Canny(blurred, 100, 200)
    backupEdged = edged.copy()
    target = None
    (_, contours, _) = cv2.findContours(edged, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    contours = sorted(contours, key=cv2.contourArea, reverse=True)
    #    print(contours)
    for c in contours:
        p = cv2.arcLength(c, True)
        approx = cv2.approxPolyDP(c, 0.02 * p, True)
        if len(approx) == 4:
            target = approx
            break
    try:
        imageArea = np.array([[[newWidth,0]], [[0, 0]], [[0, newHeight]], [[newWidth, newHeight]]], np.int32)
        if target is None:
            target = imageArea
        else:
            area1 = support.polyArea(target)
            area2 = support.polyArea(imageArea)
            if area1 / area2 < 1/3:
                target = imageArea
        print("AREA ",support.polyArea(target))
        print('page found in image')
        print('page found in image')
        pageExtracted = True
        target = support.scaleTarget(target, 0.015)
        newTarget = target * ratio
        #approx = support.cornerPoints(target)
        approx = support.cornerPoints(newTarget)
        print(approx)
        print(approx[0][1] + approx[1][1])
        warpedHeight = int(((approx[3][1]-approx[1][1]) + (approx[2][1] - approx[0][1])) / 2)
        warpedWidth = int(((approx[2][0]-approx[3][0]) + (approx[1][0] - approx[0][0])) / 2)
        print(warpedWidth, warpedHeight)
        pts2 = np.float32([[0,0],[warpedWidth,0],[warpedWidth,warpedHeight],[0,warpedHeight]])

        M = cv2.getPerspectiveTransform(approx,pts2)
        dest = cv2.warpPerspective(original,M,(warpedWidth,warpedHeight))
        destHeight, destWidth, _ = dest.shape
        cv2.drawContours(image, [target], -1, (0, 255, 0), 2)
        dest = cv2.cvtColor(dest, cv2.COLOR_BGR2GRAY)
        
        warped = support.perspectiveTransform(original, target.reshape(4, 2) * ratio)
        warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY)
        T = threshold_local(warped, 11, offset = 10, method = "gaussian")
        warped = (warped > T).astype("uint8") * 255
        
        ret, threshold1 = cv2.threshold(dest, 127, 255, cv2.THRESH_BINARY)
        threshold2 = cv2.adaptiveThreshold(dest, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)
        threshold3 = cv2.adaptiveThreshold(dest, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
        ret2, threshold4 = cv2.threshold(dest, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
        
        top, bottom, left, right = [10] * 4
        print('outputting dest ... done')
        dest = imutils.resize(dest, origHeight)
        cropHeight, cropWidth = dest.shape
        dest = Image.fromarray(dest)
        tImage = dest
    except NameError:
        print('page not found in image')
        print(traceback.print_exc(file=sys.stdout))
    except:
        print(traceback.print_exc(file=sys.stdout))
    print('page extraction ... done')
    #================================================================================================================
    print('\nstarting text extraction ...')
    print("SIZE: ",tImage.size)
    scale, image = support.downscale_image(tImage)
    edges = cv2.Canny(np.asarray(image), 100, 200)
    _, contours, _ = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    borders = support.findBorder(contours, edges)
    borders.sort(key=lambda i_x1_y1_x2_y2: (i_x1_y1_x2_y2[3] - i_x1_y1_x2_y2[1]) * (i_x1_y1_x2_y2[4] - i_x1_y1_x2_y2[2]))
    borderContour = None
    if len(borders):
        borderContour = contours[borders[0][0]]
        edges = support.removeBorder(borderContour, edges)
    edges = 255 * (edges > 0).astype(np.uint8)
    maxed_rows = rank_filter(edges, -2, size=(1, 20))
    maxed_cols = rank_filter(edges, -2, size=(20, 1))
    debordered = np.minimum(np.minimum(edges, maxed_rows), maxed_cols)
    edges = debordered
    contours = support.findComponents(edges)
    model = classify.load_model('bin')
    mapping = pickle.load(open('%s/mapping.p' % 'bin', 'rb'))
    if len(contours) == 0:
        print('no text detected')
    else:
        crop = support.findOptimalComponents(contours, edges)
        crop = support.padCrop(crop, contours, edges, borderContour)
        crop = [int(x / scale) for x in crop]
        print('outputting detection boxes ... done')
        #support.textDetection(textExtractPath) <-- doesn't work
        #support.textDetection2(textExtractPath) <-- doesn't work
        
        print('outputting text ... done')
        textImage = tImage.crop(crop)
        retImages, image2 = support.textDetection3(textImage, cropWidth, cropHeight)
        prefix = rand_num = random.randint(1, 999999999)
        support.saveImage(image2, prefix)
        svg_paths = []
        document = []
        for ind, line in enumerate(retImages):
            line_pil = Image.fromarray(line['roi'])
            svg_path = coords.makeSVG(line_pil, prefix, ind)
            svg_paths.append({'svg_path': svg_path, 'width': line['width'], 'img': line_pil})
        for p in svg_paths:
            # get a line of text
            if p['width'] < cropWidth / 2:
                document.append("$" + coords.main(p['img']) + "$\\\\")
            else:    
                document.append(extractletter.main(p['svg_path'], model, mapping) + "\\\\")
        document = ("\n").join(document)
        tmp_path = "tmp" + str(prefix) + os.sep
        f = open(tmp_path + "doc.txt", 'w')
        f.write(document)
        f.close()
        tex, tex_pdf = coords.make_document(document, tmp_path)
        return tex, tex_pdf
コード例 #30
0
def lineDrawing(image):
    # load image and create slightly smaller image due to gradients being smaller
    image_mat = support.loadImage(image)
    r, c = support.getSize(image_mat)
    image_mat = image_mat / 255.0

    Kx, Ky = utils.makeKernel('sobel')
    Kx, Ky = Kx / 8.0, Ky / 8.0
    Gx, Gy, G, D = utils.getEdges(image, Kx, Ky)
    image_mat = image_mat[1:r - 1, 1:c - 1]
    G = normalizeMatrix(G)

    Tx, Ty = gradientTangents(Gx, Gy)
    support.saveImage(Tx, "Tx_test")
    support.saveImage(Ty, "Ty_test")
    support.saveImage(Gx, "Gx_test")
    support.saveImage(Gy, "Gy_test")
    Tx_p, Ty_p = computeETF(Tx, Ty, G)
    support.saveImage(Tx, "Tx_p_test")
    support.saveImage(Ty, "Ty_p_test")

    return computeFDoG(image_mat, Tx_p, Tx_p)