def run():
    extension = 'bmp'
    pic = sys.argv[1] if len(sys.argv) > 1 else 'smiley'
    name = 'samples/' + pic
    bw_filename = name + '.' + extension
    marked_filename = name + '_marked.' + extension
    out_filename = name + '_out.' + extension

    bw_rgb = misc.imread(bw_filename)
    marked_rgb = misc.imread(marked_filename)

    bw = color_conv.rgb2yiq(bw_rgb)
    marked = color_conv.rgb2yiq(marked_rgb)

    Y = np.array(bw[:, :, 0], dtype='float64')

    (n, m) = np.shape(bw)[0:2]  # extract image dimensions
    size = n * m
    Wn = 0
    if (os.path.isfile(pic + '.mtx')):
        Wn = scipy.io.mmread(pic).tocsr()
    else:
        Wn = build_weights_matrix(Y)
        scipy.io.mmwrite(pic, Wn)

    ## once markes are found
    colored = find_marked_locations(bw_rgb, marked_rgb)

    ## set rows in colored indices
    Wn = Wn.tolil()
    xy2idx = np.arange(size).reshape(n, m)  # [x,y] -> index
    for idx in [xy2idx[i, j] for [i, j] in colored]:
        Wn[idx] = sparse.csr_matrix(([1.0], ([0], [idx])), shape=(1, size))

    LU = scipy.sparse.linalg.splu(Wn.tocsc())

    b1 = (marked[:, :, 1]).flatten()
    b2 = (marked[:, :, 2]).flatten()

    x1 = LU.solve(b1)
    x2 = LU.solve(b2)

    sol = np.zeros(np.shape(bw_rgb))
    sol[:, :, 0] = Y
    sol[:, :, 1] = x1.reshape((n, m))
    sol[:, :, 2] = x2.reshape((n, m))
    sol_rgb = color_conv.yiq2rgb(sol)

    misc.imsave(out_filename, sol_rgb)
Exemple #2
0
def run():
    extension = 'bmp'
    pic = sys.argv[1] if len(sys.argv) > 1 else 'smiley'
    name = 'samples/' + pic
    bw_filename = name + '.' + extension
    marked_filename = name + '_marked.' + extension
    out_filename = name + '_out.' + extension

    bw_rgb = misc.imread(bw_filename)
    marked_rgb = misc.imread(marked_filename)

    bw = color_conv.rgb2yiq(bw_rgb)
    marked = color_conv.rgb2yiq(marked_rgb)

    Y = np.array(bw[:, :, 0], dtype='float64')

    (n, m) = np.shape(bw)[0:2]  # extract image dimensions
    size = n * m
    Wn = 0
    if (os.path.isfile(pic + '.mtx')):
        Wn = scipy.io.mmread(pic).tocsr()
    else:
        Wn = build_weights_matrix(Y)
        scipy.io.mmwrite(pic, Wn)

    ## once markes are found
    colored = find_marked_locations(bw_rgb, marked_rgb)

    ## set rows in colored indices
    Wn = Wn.tolil()
    xy2idx = np.arange(size).reshape(n, m)  # [x,y] -> index
    for idx in [xy2idx[i, j] for [i, j] in colored]:
        Wn[idx] = sparse.csr_matrix(([1.0], ([0], [idx])), shape=(1, size))

    LU = scipy.sparse.linalg.splu(Wn.tocsc())

    b1 = (marked[:, :, 1]).flatten()
    b2 = (marked[:, :, 2]).flatten()

    x1 = LU.solve(b1)
    x2 = LU.solve(b2)

    sol = np.zeros(np.shape(bw_rgb))
    sol[:, :, 0] = Y
    sol[:, :, 1] = x1.reshape((n, m))
    sol[:, :, 2] = x2.reshape((n, m))
    sol_rgb = color_conv.yiq2rgb(sol)

    misc.imsave(out_filename, sol_rgb)
def read_images():
    original_image = cv.imread("samples/hair_res.bmp", 0)  # open colour image
    im = np.zeros((original_image.shape[0], original_image.shape[1], 3))
    im[:, :, 0] = original_image
    im[:, :, 1] = original_image
    im[:, :, 2] = original_image
    cv.imwrite("samples/hair_res1.bmp", im)
    np.set_printoptions(threshold=np.nan)

    segments = slic(im, n_segments=300, sigma=2)
    fig = plt.figure("Superpixels -- %d segments" % 300)
    ax = fig.add_subplot(1, 1, 1)
    print(segments)
    ax.imshow(mark_boundaries(im, segments))
    plt.axis("off")
    plt.show()
    feature_vec = np.zeros(
        (len(np.unique(segments)), 3))  # creating feature vector
    yiq = color_conv.rgb2yiq(im)
    for (j, segValue) in enumerate(np.unique(segments)):
        y, i, q = cv.split(yiq)
        feature_vec[j][0] = np.mean(y[segments == segValue])  # adding r values
        feature_vec[j][1] = np.mean(i[segments == segValue])  # adding g values
        feature_vec[j][2] = np.mean(q[segments == segValue])  # adding b values

    plt.scatter(feature_vec[:, 0], feature_vec[:, 1], label='True Position')
    plt.show()

    clusters = KMeans(n_clusters=12, random_state=0).fit(feature_vec)
    print(len(clusters.labels_))
    for i in range(len(clusters.labels_)):
        for ii in range(segments.shape[0]):
            for ij in range(segments.shape[1]):
                if segments[ii, ij] == i:
                    segments[ii, ij] = clusters.labels_[i]

    plt.scatter(feature_vec[:, 0],
                feature_vec[:, 1],
                c=clusters.labels_,
                cmap='rainbow')
    print(segments)
    fig = plt.figure("Superpixels -- %d segments" % 300)
    ax = fig.add_subplot(1, 1, 1)
    ax.imshow(mark_boundaries(im, segments))
    plt.axis("off")
    plt.show()
Exemple #4
0
def run(convert, email, template, alt_template):
    bw_filename = convert.source_file.name
    marked_filename = convert.marked_file.name
    out_filename = bw_filename.replace('source', 'result')
    matrix_name = 'matrix/' + bw_filename.replace('.bmp',
                                                  '')[bw_filename.rfind('/'):]

    bw_rgb = misc.imread(bw_filename)
    marked_rgb = misc.imread(marked_filename)

    bw = color_conv.rgb2yiq(bw_rgb)
    marked = color_conv.rgb2yiq(marked_rgb)

    y = np.array(bw[:, :, 0], dtype='float64')

    (n, m) = np.shape(bw)[0:2]  # extract image dimensions
    size = n * m
    wn = 0
    if os.path.isfile(matrix_name + '.mtx'):
        wn = scipy.io.mmread(matrix_name).tocsr()
    else:
        wn = build_weights_matrix(y)
        scipy.io.mmwrite(matrix_name, wn)

    ## once markes are found
    colored = find_marked_locations(bw_rgb, marked_rgb)

    ## set rows in colored indices
    wn = wn.tolil()
    xy2idx = np.arange(size).reshape(n, m)  # [x,y] -> index
    for idx in [xy2idx[i, j] for [i, j] in colored]:
        wn[idx] = sparse.csr_matrix(([1.0], ([0], [idx])), shape=(1, size))

    lu = scipy.sparse.linalg.splu(wn.tocsc())

    b1 = (marked[:, :, 1]).flatten()
    b2 = (marked[:, :, 2]).flatten()

    x1 = lu.solve(b1)
    x2 = lu.solve(b2)

    sol = np.zeros(np.shape(bw_rgb))
    sol[:, :, 0] = y
    sol[:, :, 1] = x1.reshape((n, m))
    sol[:, :, 2] = x2.reshape((n, m))
    sol_rgb = color_conv.yiq2rgb(sol)
    path_name = out_filename[:out_filename.rfind('/')]
    if not os.path.exists(path_name):
        os.makedirs(path_name)
    misc.imsave(out_filename, sol_rgb)

    data = dict()
    html = render_to_string(template, data)
    text = render_to_string(alt_template, data)
    msg = EmailMultiAlternatives(subject=u'Преобразование изображения',
                                 body=text,
                                 from_email=settings.DEFAULT_FROM_EMAIL,
                                 to=[email],
                                 alternatives=[(html, 'text/html')])
    msg.send()
    f = open(out_filename)
    convert.destination_file = out_filename  #File(f)
    convert.sended = True
    convert.save()
    f.close()