Exemplo n.º 1
0
    def postResize(self):
        print "  Post resize"
        data_name = self._keyword
        data_files = dataFiles(data_name)

        for data_file in data_files:
            data_filename = os.path.basename(data_file)
            C_8U = loadRGB(data_file)

            if C_8U is None:
                os.remove(data_file)
                print "  - Delete: %s" % data_filename
                continue
            h, w = C_8U.shape[0:2]

            opt_scale = 800.0 / float(h)
            opt_scale = max(opt_scale, 800.0 / float(w))
            opt_scale = min(opt_scale, 1.0)

            h_opt = int(opt_scale * h)
            w_opt = int(opt_scale * w)

            C_8U_small = cv2.resize(C_8U, (w_opt, h_opt))
            saveRGB(data_file, C_8U_small)
            print "  - Resized: %s" % data_filename
Exemplo n.º 2
0
def loadData(data_name, data_id):
    data_file = dataFile(data_name, data_id)

    if data_file is None:
        return None

    return loadRGB(data_file)
Exemplo n.º 3
0
def loadData(data_name, data_id):
    data_file = dataFile(data_name, data_id)

    if data_file is None:
        return None

    return loadRGB(data_file)
Exemplo n.º 4
0
def animationResultImp(image_file):
    image_name = os.path.basename(image_file)
    image_name = os.path.splitext(image_name)[0]

    image = loadRGB(image_file)

    som1D, som2D = setupSOM(image)

    fig = plt.figure()
    fig.subplots_adjust(left=0.05, bottom=0.05, right=0.95, top=0.9, wspace=0.1, hspace=0.2)

    font_size = 15
    fig.suptitle("SOM-Color Manifolds (Animation)", fontsize=font_size)

    plt.subplot(131)
    plt.title("%s" % (image_name))
    plt.imshow(image)
    plt.axis('off')

    plt.subplot(132)
    plt.title("SOM 1D")
    som1D_plot = SOMPlot(som1D)
    ani1D = animation.FuncAnimation(fig, som1D_plot.trainAnimation, interval=0, blit=True)
    plt.axis('off')

    plt.subplot(133)
    plt.title("SOM 2D")
    som2D_plot = SOMPlot(som2D)
    ani2D = animation.FuncAnimation(fig, som2D_plot.trainAnimation, interval=0, blit=True)
    plt.axis('off')

    showMaximize()
Exemplo n.º 5
0
    def postResize(self):
        print "  Post resize"
        data_name = self._keyword
        data_files = dataFiles(data_name)

        for data_file in data_files:
            data_filename = os.path.basename(data_file)
            C_8U = loadRGB(data_file)

            if C_8U is None:
                os.remove(data_file)
                print "  - Delete: %s" % data_filename
                continue
            h, w = C_8U.shape[0:2]

            opt_scale = 800.0 / float(h)
            opt_scale = max(opt_scale, 800.0 / float(w))
            opt_scale = min(opt_scale, 1.0)

            h_opt = int(opt_scale * h)
            w_opt = int(opt_scale * w)

            C_8U_small = cv2.resize(C_8U, (w_opt, h_opt))
            saveRGB(data_file, C_8U_small)
            print "  - Resized: %s" % data_filename
Exemplo n.º 6
0
 def get_gray_scal(self, image_file):
     image = loadRGB(image_file)
     som1D, som2D = self.setupSOM(image)
     print("  - Train 2D")
     som2D.trainAll()
     som2D_plot = SOMPlot(som2D)
     gray, gray_reverse = som2D_plot.showGrayImage2(image)
     return gray, gray_reverse
Exemplo n.º 7
0
    def get_2d_manifold(self, image_file):
        image = loadRGB(image_file)
        som1D, som2D = self.setupSOM(image)

        print("  - Train 2D")
        som2D.trainAll()

        return som2D.nodeImage()
def singleImageResult(image_file):
    image_name = os.path.basename(image_file)
    image_name = os.path.splitext(image_name)[0]

    image = loadRGB(image_file)

    som1D, som2D = setupSOM(image)

    fig = plt.figure(figsize=(10, 7))
    fig.subplots_adjust(left=0.05,
                        bottom=0.05,
                        right=0.95,
                        top=0.9,
                        wspace=0.1,
                        hspace=0.2)

    font_size = 15
    fig.suptitle("SOM-Color Manifolds for Single Image", fontsize=font_size)

    plt.subplot(231)
    h, w = image.shape[:2]
    plt.title("Original Image: %s x %s" % (w, h), fontsize=font_size)
    plt.imshow(image)
    plt.axis('off')

    print "  - Train 1D"
    som1D.trainAll()

    print "  - Train 2D"
    som2D.trainAll()

    som1D_plot = SOMPlot(som1D)
    som2D_plot = SOMPlot(som2D)
    plt.subplot(232)
    plt.title("SOM 1D", fontsize=font_size)
    som1D_plot.updateImage()
    plt.axis('off')

    plt.subplot(233)
    plt.title("SOM 2D", fontsize=font_size)
    som2D_plot.updateImage()
    plt.axis('off')

    ax1D = fig.add_subplot(235, projection='3d')
    plt.title("1D in 3D", fontsize=font_size)
    som1D_plot.plot3D(ax1D)

    ax2D = fig.add_subplot(236, projection='3d')
    plt.title("2D in 3D", fontsize=font_size)
    som2D_plot.plot3D(ax2D)

    result_file = resultFile("%s_single" % image_name)
    plt.savefig(result_file)
Exemplo n.º 9
0
def singleImageResult(image_file):
    image_name = os.path.basename(image_file)
    image_name = os.path.splitext(image_name)[0]

    image = loadRGB(image_file)

    som1D, som2D = setupSOM(image)

    fig = plt.figure(figsize=(10, 7))
    fig.subplots_adjust(left=0.05, bottom=0.05, right=0.95, top=0.9, wspace=0.1, hspace=0.2)

    font_size = 15
    fig.suptitle("SOM-Color Manifolds for Single Image", fontsize=font_size)

    plt.subplot(231)
    h, w = image.shape[:2]
    plt.title("Original Image: %s x %s" % (w, h), fontsize=font_size)
    plt.imshow(image)
    plt.axis('off')

    print "  - Train 1D"
    som1D.trainAll()

    print "  - Train 2D"
    som2D.trainAll()

    som1D_plot = SOMPlot(som1D)
    som2D_plot = SOMPlot(som2D)
    plt.subplot(232)
    plt.title("SOM 1D", fontsize=font_size)
    som1D_plot.updateImage()
    plt.axis('off')

    plt.subplot(233)
    plt.title("SOM 2D", fontsize=font_size)
    som2D_plot.updateImage()
    plt.axis('off')

    ax1D = fig.add_subplot(235, projection='3d')
    plt.title("1D in 3D", fontsize=font_size)
    som1D_plot.plot3D(ax1D)

    ax2D = fig.add_subplot(236, projection='3d')
    plt.title("2D in 3D", fontsize=font_size)
    som2D_plot.plot3D(ax2D)

    result_file = resultFile("%s_single" % image_name)
    plt.savefig(result_file)
Exemplo n.º 10
0
def animationResultImp(image_file):
    image_name = os.path.basename(image_file)
    image_name = os.path.splitext(image_name)[0]

    image = loadRGB(image_file)

    som1D, som2D = setupSOM(image)

    fig = plt.figure()
    fig.subplots_adjust(left=0.05,
                        bottom=0.05,
                        right=0.95,
                        top=0.9,
                        wspace=0.1,
                        hspace=0.2)

    font_size = 15
    fig.suptitle("SOM-Color Manifolds (Animation)", fontsize=font_size)

    plt.subplot(131)
    plt.title("%s" % (image_name))
    plt.imshow(image)
    plt.axis('off')

    plt.subplot(132)
    plt.title("SOM 1D")
    som1D_plot = SOMPlot(som1D)
    ani1D = animation.FuncAnimation(fig,
                                    som1D_plot.trainAnimation,
                                    interval=0,
                                    blit=True)
    plt.axis('off')

    plt.subplot(133)
    plt.title("SOM 2D")
    som2D_plot = SOMPlot(som2D)
    ani2D = animation.FuncAnimation(fig,
                                    som2D_plot.trainAnimation,
                                    interval=0,
                                    blit=True)
    plt.axis('off')

    showMaximize()
Exemplo n.º 11
0
    def color_to_gray_debug(self, image_file, gray_name, gray_reverse_name,
                            debug):
        image = loadRGB(image_file)

        som1D, som2D = self.setupSOM(image)

        fig = plt.figure(figsize=(12, 10))
        fig.subplots_adjust(left=0.05,
                            bottom=0.05,
                            right=0.95,
                            top=0.9,
                            wspace=0.1,
                            hspace=0.2)

        font_size = 15
        fig.suptitle("SOM-Color Manifolds for Single Image",
                     fontsize=font_size)

        plt.subplot(331)
        h, w = image.shape[:2]
        plt.title("Original Image: %s x %s" % (w, h), fontsize=font_size)
        plt.imshow(image)
        plt.axis('off')

        print("  - Train 1D")
        som1D.trainAll()

        print("  - Train 2D")
        som2D.trainAll()

        som1D_plot = SOMPlot(som1D)
        som2D_plot = SOMPlot(som2D)
        plt.subplot(332)
        plt.title("SOM 1D", fontsize=font_size)
        # 如果改变updateImage函数的返回值,那么可以用以下语句,代替以下第二行语句。
        # plt.imshow(som1D_plot.updateImage())
        som1D_plot.updateImage()
        plt.axis('off')

        plt.subplot(333)
        plt.title("SOM 2D", fontsize=font_size)
        som2D_plot.updateImage()
        plt.axis('off')

        color_pixels = ColorPixels(image)
        pixels = color_pixels.pixels(color_space="rgb")
        ax = fig.add_subplot(334, projection='3d')
        plt.title("cloudPoint", fontsize=font_size)
        som1D_plot.plotCloud(ax, pixels)

        hist3D = Hist3D(image, num_bins=16)
        color_samples = hist3D.colorCoordinates()
        ax = fig.add_subplot(337, projection='3d')
        plt.title("cloudPoint", fontsize=font_size)
        som1D_plot.plotCloud(ax, color_samples)

        ax1D = fig.add_subplot(335, projection='3d')
        plt.title("1D in 3D", fontsize=font_size)
        som1D_plot.plot3D(ax1D)

        ax2D = fig.add_subplot(336, projection='3d')
        plt.title("2D in 3D", fontsize=font_size)
        som2D_plot.plot3D(ax2D)

        plt.subplot(338)
        plt.title("Gray", fontsize=font_size)

        # 如果改变updateImage函数的返回值,那么可以用以下语句,代替以下第二行语句。
        a, b = som2D_plot.showGrayImage2(image)

        plt.imshow(a, cmap='gray', vmin=0, vmax=1)
        plt.axis('off')
        plt.imsave(self.resultFile(gray_name), a, cmap='gray', vmin=0, vmax=1)
        plt.imsave(self.resultFile(gray_reverse_name),
                   b,
                   cmap='gray',
                   vmin=0,
                   vmax=1)

        plt.subplot(339)
        plt.title("Gray", fontsize=font_size)
        plt.imshow(b, cmap='gray', vmin=0, vmax=1)
        plt.axis('off')

        result_file = self.resultFile("%s_debug" % gray_name)
        plt.savefig(result_file)
Exemplo n.º 12
0
def multiImagesResult(data_name, data_ids):
    num_cols = len(data_ids)
    num_rows = 2

    fig = plt.figure(figsize=(12, 6))
    fig.subplots_adjust(left=0.05,
                        bottom=0.05,
                        right=0.95,
                        top=0.9,
                        wspace=0.1,
                        hspace=0.2)

    font_size = 15
    fig.suptitle("SOM-Color Manifolds for Multi Images", fontsize=font_size)

    color_samples = []
    col_id = 0
    for data_id in data_ids:
        image_file = dataFile(data_name, data_id)
        image_name = "%s_%s" % (data_name, data_id + 1)

        image = loadRGB(image_file)

        hist3D = Hist3D(image, num_bins=16)

        color_samples.extend(hist3D.colorCoordinates())

        plt.subplot2grid((num_rows, num_cols), (0, col_id))
        plt.title("%s" % image_name, fontsize=font_size)
        plt.imshow(image)
        plt.axis('off')

        col_id += 1

    color_samples = np.array(color_samples)
    print color_samples.shape

    som1D, som2D = setupSOM(color_samples)

    print "  - Train 1D"
    som1D.trainAll()

    print "  - Train 2D"
    som2D.trainAll()

    som1D_plot = SOMPlot(som1D)
    som2D_plot = SOMPlot(som2D)

    col_id = 1
    plt.subplot2grid((num_rows, num_cols), (1, col_id))
    plt.title("SOM 1D", fontsize=font_size)
    som1D_plot.updateImage()
    plt.axis('off')

    col_id += 1
    ax1D = plt.subplot2grid((num_rows, num_cols), (1, col_id),
                            projection='3d',
                            aspect='equal')
    plt.title("1D in 3D", fontsize=font_size)
    som1D_plot.plot3D(ax1D)

    col_id += 1
    plt.subplot2grid((num_rows, num_cols), (1, col_id))
    plt.title("SOM 2D", fontsize=font_size)
    som2D_plot.updateImage()
    plt.axis('off')

    col_id += 1
    ax2D = plt.subplot2grid((num_rows, num_cols), (1, col_id),
                            projection='3d',
                            aspect='equal')
    plt.title("2D in 3D", fontsize=font_size)
    som2D_plot.plot3D(ax2D)

    result_file = resultFile("%s_multi" % data_name)
    plt.savefig(result_file)
Exemplo n.º 13
0
def singleImageResult(image_file):
    print image_file
    image_name = os.path.basename(image_file)
    image_name = os.path.splitext(image_name)[0]

    image = loadRGB(image_file)
    import time
    start = time.time()

    som1D, som2D = setupSOM(image)

    fig = plt.figure(figsize=(12, 10))
    fig.subplots_adjust(left=0.05, bottom=0.05, right=0.95, top=0.9, wspace=0.1, hspace=0.2)

    print time.time() - start
    font_size = 15
    fig.suptitle("SOM-Color Manifolds for Single Image", fontsize=font_size)

    plt.subplot(331)
    h, w = image.shape[:2]
    plt.title("Original Image: %s x %s" % (w, h), fontsize=font_size)
    plt.imshow(image)
    plt.axis('off')

    print "  - Train 1D"
    som1D.trainAll()

    print "  - Train 2D"
    som2D.trainAll()

    som1D_plot = SOMPlot(som1D)
    som2D_plot = SOMPlot(som2D)
    plt.subplot(332)
    plt.title("SOM 1D", fontsize=font_size)
    # 如果改变updateImage函数的返回值,那么可以用以下语句,代替以下第二行语句。
    # plt.imshow(som1D_plot.updateImage())
    som1D_plot.updateImage()
    plt.axis('off')

    plt.subplot(333)
    plt.title("SOM 2D", fontsize=font_size)
    som2D_plot.updateImage()
    plt.axis('off')


    color_pixels = ColorPixels(image)
    pixels = color_pixels.pixels(color_space="rgb")
    ax = fig.add_subplot(334, projection='3d')
    plt.title("cloudPoint", fontsize=font_size)
    som1D_plot.plotCloud(ax, pixels)

    hist3D = Hist3D(image, num_bins=16)
    color_samples = hist3D.colorCoordinates()
    ax = fig.add_subplot(337, projection='3d')
    plt.title("cloudPoint", fontsize=font_size)
    som1D_plot.plotCloud(ax, color_samples)


    ax1D = fig.add_subplot(335, projection='3d')
    plt.title("1D in 3D", fontsize=font_size)
    som1D_plot.plot3D(ax1D)

    ax2D = fig.add_subplot(336, projection='3d')
    plt.title("2D in 3D", fontsize=font_size)
    som2D_plot.plot3D(ax2D)

    plt.subplot(338)
    plt.title("Gray", fontsize=font_size)

    # 如果改变updateImage函数的返回值,那么可以用以下语句,代替以下第二行语句。
    a,b = som2D_plot.showGrayImage2(image)

    plt.imshow(a, cmap='gray', vmin = 0, vmax = 1)
    plt.axis('off')
    plt.imsave('''./''' + image_name + '''.png''', a, cmap='gray',vmin = 0, vmax = 1)
    plt.imsave('''./''' + image_name + '''!.png''', b, cmap='gray',vmin = 0, vmax = 1)


    plt.subplot(339)
    plt.title("Gray", fontsize=font_size)
    plt.imshow(b, cmap='gray', vmin = 0, vmax = 1)
    plt.axis('off')

    result_file = resultFile("%s_single" % image_name)
    plt.savefig(result_file)