Beispiel #1
0
def exp():
    image_dir = r"../images/medical_image_sets/DRIVE/images"
    mask_dir = r"../images/medical_image_sets/DRIVE/mask"
    out_dir = r"./temp_out/"
    [os.remove(path) for path in glob.glob(out_dir + "/*")]
    image_names = glob.glob(image_dir + "/*")

    gamma_list = []
    image_path_list = []
    for k, path in enumerate(image_names):
        # Step 1. read images
        mask_path = os.path.join(mask_dir, path.split(os.sep)[-1])
        img = cv2.imread(path)
        mask = cv2.imread(mask_path, 0)
        if img is None or img.shape[
                2] == 4:  # something wrong to read an image, or BGRA image, or other files
            print("Warning: path (" + path +
                  ") is not valid, we will skip this path...")
            continue
        _, mask = cv2.threshold(mask, 254, 255, cv2.THRESH_BINARY)

        # Step 2. conduct gamma estimation and image restoration with different methods or config
        _, img_cab = correct_average_brightness(img, mask=mask)  # CAB
        _, img_cab_agt = adaptive_gamma_transform(img, mask=mask,
                                                  visual=False)  # AGT
        gamma, img_cab_agt_visual = adaptive_gamma_transform(
            img, mask=mask, visual=True)  # AGT-ME-VISUAL
        _, img_bigc = blind_inverse_gamma_correction(img, "fast", True)  # BIGC

        # Step 3. save the images
        width = img.shape[0]  # 196 for paper eps file
        # names = ["ORIGINAL", "BIGC", "CAB", "AGT-ME", "AGT-ME-VISUAL"]
        names = ["ORIGINAL", "BIGC", "CAB", "AGT-ME"]
        images = [img, img_bigc, img_cab, img_cab_agt, img_cab_agt_visual]
        all_imgs = None
        for name, image in zip(names, images):
            temp_img = cv2.resize(image, (0, 0),
                                  fx=width / image.shape[1],
                                  fy=width / image.shape[1])
            # temp_img = cv2.putText(temp_img, name, (0, 25), cv2.FONT_HERSHEY_COMPLEX, 0.7, (255, 0, 0), 1)
            temp_img = cv2.putText(temp_img, name, (0, 50),
                                   cv2.FONT_HERSHEY_DUPLEX, 2.0, (255, 0, 0),
                                   2)

            if all_imgs is None:
                all_imgs = temp_img.copy()
            else:
                all_imgs = np.concatenate(
                    [all_imgs, 255 + np.zeros([all_imgs.shape[0], 11, 3])],
                    axis=1)
                all_imgs = np.concatenate([all_imgs, temp_img], axis=1)
        all_imgs = all_imgs.astype(np.uint8)
        cv2.imwrite(
            os.path.join(out_dir,
                         path.split(os.sep)[-1][:-4] + ".png"), all_imgs)
    os.system("nautilus " + out_dir)
Beispiel #2
0
def exp():
    image_dir = r"../images/medical_image_sets/CT"
    out_dir = r"./temp_out"
    [os.remove(path) for path in glob.glob(out_dir + "/*")]
    image_names = glob.glob(image_dir + "/*")

    for k, img_path in enumerate(image_names):
        print("processing " + img_path + "...")
        # Step 1. read CT images with pydicom package
        dcm = pydicom.read_file(img_path)
        dcm.image = dcm.pixel_array * dcm.RescaleSlope + dcm.RescaleIntercept
        img_raw = dcm.image.copy()

        if img_raw is None:  # something wrong to read an image, or BGRA image
            print("Warning: path (" + img_path + ") is not valid, we will skip this path...")
            continue

        # Step 2. Get the mask with a simple threshold strategy and normalize it to 0-1
        mask_threshold = -150
        mask = ((img_raw > mask_threshold) * 255).astype(np.uint8)
        img = 255 * (img_raw - mask_threshold) / (np.max(img_raw[:]) - mask_threshold)
        img[img < 0] = 0
        img = img.round().astype(np.uint8)

        # Step 2. conduct gamma estimation and image restoration with different methods or config
        _, img_cab = correct_average_brightness(img, mask=mask)  # CAB
        _, img_cab_agt = adaptive_gamma_transform(img, mask=mask, visual=False)  # AGT
        gamma, img_cab_agt_visual = adaptive_gamma_transform(img, mask=mask, visual=True)  # AGT-ME-VISUAL
        _, img_bigc = blind_inverse_gamma_correction(img, "fast", True)  # BIGC
        img_bigc[mask < 255] = 0

        # Step 3. save the images
        width = img.shape[0]  # 196 for paper eps file
        # names = ["ORIGINAL", "BIGC", "CAB", "AGT-ME", "AGT-ME-VISUAL"]
        names = ["ORIGINAL", "BIGC", "CAB", "AGT-ME"]
        images = [img, img_bigc, img_cab, img_cab_agt, img_cab_agt_visual]
        all_imgs = None
        for name, image in zip(names, images):
            temp_img = cv2.resize(image, (0, 0), fx=width / image.shape[1], fy=width / image.shape[1])
            temp_img = cv2.cvtColor(temp_img, cv2.COLOR_GRAY2BGR)
            # temp_img = cv2.putText(temp_img, name, (0, 25), cv2.FONT_HERSHEY_COMPLEX, 0.7, (255, 0, 0), 1)
            temp_img = cv2.putText(temp_img, name, (0, 50), cv2.FONT_HERSHEY_DUPLEX, 1.5, (255, 0, 0), 2)

            if all_imgs is None:
                all_imgs = temp_img.copy()
            else:
                all_imgs = np.concatenate([all_imgs, 255 + np.zeros([all_imgs.shape[0], 11, 3])], axis=1)
                all_imgs = np.concatenate([all_imgs, temp_img], axis=1)
        all_imgs = all_imgs.astype(np.uint8)
        cv2.imwrite(os.path.join(out_dir, img_path.split(os.sep)[-1] + ".png"), all_imgs)
        # cv2.imwrite(os.path.join(out_dir, img_path.split(os.sep)[-1] + "mask.png"), mask)
    os.system("nautilus " + out_dir)

    print("please check the results in dir:" + out_dir)
    os.system("nautilus " + out_dir)
Beispiel #3
0
def exp():
    image_dir = r"../images/medical_image_sets/MRI/"
    out_dir = r"./temp_out"
    [os.remove(path) for path in glob.glob(out_dir + "/*")]
    image_names = glob.glob(image_dir + "/*")

    for k, img_path in enumerate(image_names):
        # Step 1. read images
        img_raw = nib.load(img_path).dataobj[200:675, :, 5]
        img_raw = cv2.rotate(img_raw, cv2.ROTATE_90_COUNTERCLOCKWISE)

        mask = (img_raw > 100).astype(np.uint8) * 255
        img = (255 * (img_raw - np.min(img_raw[:])) /
               (np.max(img_raw[:]) - np.min(img_raw[:])))
        img = img.round().astype(np.uint8)

        # Step 2. conduct gamma estimation and image restoration with different methods or config
        _, img_cab = correct_average_brightness(img, mask=mask)  # CAB
        _, img_cab_agt = adaptive_gamma_transform(img, mask=mask,
                                                  visual=False)  # AGT
        gamma, img_cab_agt_visual = adaptive_gamma_transform(
            img, mask=mask, visual=True)  # AGT-ME-VISUAL
        _, img_bigc = blind_inverse_gamma_correction(img, "fast", True)  # BIGC
        img_bigc[mask < 255] = 0

        # Step 3. save the images
        width = img.shape[0]  # 196 for paper eps file
        # names = ["ORIGINAL", "BIGC", "CAB", "AGT-ME", "AGT-ME-VISUAL"]
        names = ["ORIGINAL", "BIGC", "CAB", "AGT-ME"]
        images = [img, img_bigc, img_cab, img_cab_agt, img_cab_agt_visual]
        all_imgs = None
        for name, image in zip(names, images):
            temp_img = cv2.resize(image, (0, 0),
                                  fx=width / image.shape[1],
                                  fy=width / image.shape[1])
            temp_img = cv2.cvtColor(temp_img, cv2.COLOR_GRAY2BGR)
            # temp_img = cv2.putText(temp_img, name, (0, 25), cv2.FONT_HERSHEY_COMPLEX, 0.7, (255, 0, 0), 1)
            temp_img = cv2.putText(temp_img, name, (0, 70),
                                   cv2.FONT_HERSHEY_DUPLEX, 3.0, (255, 0, 0),
                                   3)

            if all_imgs is None:
                all_imgs = temp_img.copy()
            else:
                all_imgs = np.concatenate(
                    [all_imgs, 255 + np.zeros([all_imgs.shape[0], 11, 3])],
                    axis=1)
                all_imgs = np.concatenate([all_imgs, temp_img], axis=1)
        all_imgs = all_imgs.astype(np.uint8)
        cv2.imwrite(
            os.path.join(out_dir,
                         img_path.split(os.sep)[-1][:-4] + ".png"), all_imgs)
    os.system("nautilus " + out_dir)
Beispiel #4
0
def exp():
    # Step.0 Set the image path
    image_dir = r"../images/BSD68"
    file_list = os.listdir(image_dir)[0:10]

    size_list = [(256, 256), (512, 512), (1024, 1024), (2048, 2048)]
    agt_time_list = []
    cab_time_list = []
    bigc_time_list = []
    for k, name in enumerate(file_list):
        print(str(k) + ":" + name)
        image = cv2.imread(os.path.join(image_dir, name), cv2.IMREAD_GRAYSCALE)
        if image is None:
            continue
        agt_time_list.append([])
        cab_time_list.append([])
        bigc_time_list.append([])
        for siz in size_list:
            img = cv2.resize(image, siz)  # Resize the images

            start_time = time.time()
            _, _ = adaptive_gamma_transform(img)
            end_time = time.time()
            agt_time_list[-1].append(end_time - start_time)

            start_time = time.time()
            _, _ = correct_average_brightness(img)
            end_time = time.time()
            cab_time_list[-1].append(end_time - start_time)

            start_time = time.time()
            _, _ = blind_inverse_gamma_correction(img)
            end_time = time.time()
            bigc_time_list[-1].append(end_time - start_time)

    agt_time_list = np.array(agt_time_list)
    cab_time_list = np.array(cab_time_list)
    bigc_time_list = np.array(bigc_time_list)
    print("AGT time cost:")
    print("    mean:" + str(np.mean(agt_time_list, axis=0)) + "(s)")
    print("    std: " + str(np.std(agt_time_list, axis=0)) + "(s)")

    print("CAB time cost:")
    print("    mean:" + str(np.mean(cab_time_list, axis=0)) + "(s)")
    print("    std: " + str(np.std(cab_time_list, axis=0)) + "(s)")

    print("BIGC time cost:")
    print("    mean:" + str(np.mean(bigc_time_list, axis=0)) + "(s)")
    print("    std: " + str(np.std(bigc_time_list, axis=0)) + "(s)")
Beispiel #5
0
def exp():
    # Step.0 Set the image dataset dir and a dir to store the results
    image_dir = r"../images/BSD68/"
    image_names = os.listdir(image_dir)[:MAX_NUMBER]  # Get the file name of the test images

    out_dir = r"./temp_out"

    # Step.1 the gamma set definition
    gamma_set = np.linspace(0.1, 3.0, 30)
    for gamma in gamma_set:  # save the distorted image for fun
        path = os.path.join(image_dir, image_names[0])
        img = cv2.imread(path, -1)
        distorted_img = gamma_trans(img, gamma)
        cv2.imwrite(out_dir + os.sep + str(gamma) + ".png", distorted_img)

    # Step.2 get the estimated gamma with different methods
    agt_gamma_estimated_list = []
    cab_gamma_estimated_list = []
    bigc_gamma_estimated_list = []
    for gamma in gamma_set:
        agt_gamma_estimated_list.append([])
        cab_gamma_estimated_list.append([])
        bigc_gamma_estimated_list.append([])
        for k, name in enumerate(image_names):
            print("gamma:" + str(gamma) + "; number:" + str(k) + ": " + name)
            path = os.path.join(image_dir, name)
            img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)  # read as gray image

            # Test with AGT method
            gamma_origin, _ = adaptive_gamma_transform(img, visual=False)  # Estimate the original gamma value
            distorted_img = gamma_trans(img, gamma)  # Add gamma distortion
            gamma_estimated, _ = adaptive_gamma_transform(distorted_img, visual=False)  # distorted gamma value
            agt_gamma_estimated_list[-1].append(gamma_origin / gamma_estimated)

            # Test with CAB method
            gamma_origin, _ = correct_average_brightness(img)  # Estimate the original gamma value
            distorted_img = gamma_trans(img, gamma)  # Add gamma distortion
            gamma_estimated, _ = correct_average_brightness(distorted_img)  # distorted gamma value
            cab_gamma_estimated_list[-1].append(gamma_origin / gamma_estimated)

            # Test with BIGC method
            gamma_origin, _ = blind_inverse_gamma_correction(img, visual=False)  # Estimate the original gamma value
            distorted_img = gamma_trans(img, gamma)  # Add gamma distortion
            gamma_estimated, _ = blind_inverse_gamma_correction(distorted_img, visual=False)  # distorted gamma value
            bigc_gamma_estimated_list[-1].append(gamma_origin / gamma_estimated)

    agt_gamma_estimated_list = np.array(agt_gamma_estimated_list)
    cab_gamma_estimated_list = np.array(cab_gamma_estimated_list)
    bigc_gamma_estimated_list = np.array(bigc_gamma_estimated_list)

    # figure 1. actual gamma VS estimated gamma (AGT method)
    plt.figure()
    plt.subplots_adjust(bottom=0.14, left=0.14)
    plt.grid()
    gamma_set = np.repeat(gamma_set[:, np.newaxis], agt_gamma_estimated_list.shape[1], axis=1)
    plt.scatter(gamma_set, agt_gamma_estimated_list, s=20, c='b')
    plt.xlim([-0.2, 3.2])
    plt.ylim([-0.2, 3.2])
    plt.xlabel("Gamma bias $\gamma_b$", fontsize=16)
    plt.ylabel("Recognized gamma $\gamma_r$", fontsize=16)
    plt.tick_params(labelsize=16)
    # plt.legend(fontsize=20)
    foo_fig = plt.gcf()
    foo_fig.savefig("figs/Fig4a.eps", format='eps', dpi=1200)
    plt.title("set-estimate")

    # figure 2. RMSE error curve for different methods
    rmse_agt = np.sqrt(np.mean((agt_gamma_estimated_list - gamma_set) ** 2, axis=1))
    rmse_cab = np.sqrt(np.mean((cab_gamma_estimated_list - gamma_set) ** 2, axis=1))
    temp = (bigc_gamma_estimated_list - gamma_set) ** 2
    print("Outlier percentage of BIGC:", 100.0 * np.sum(temp[:] > 0.25) / temp.size, "%")
    temp[temp > 0.5] = np.NaN  # special treatment for BIGC outliers
    rmse_bigc = np.sqrt(np.nanmean(temp, axis=1))
    rmse_agt[rmse_agt < 1e-3] = 1e-3
    rmse_cab[rmse_cab < 1e-3] = 1e-3
    rmse_bigc[rmse_bigc < 1e-3] = 1e-3
    print("mean rmse:", np.mean(rmse_agt[:]), "(AGT-ME) and ", np.mean(rmse_cab[:]), "(CAB) and ",
          np.mean(rmse_bigc[:]), "(BIGC)")

    # print(rmse_agt, rmse_bigc)
    plt.figure()
    plt.grid()
    plt.subplots_adjust(bottom=0.14, left=0.18)
    plt.plot(gamma_set[:, 0], rmse_bigc, "r-.", label="BIGC")
    plt.plot(gamma_set[:, 0], rmse_cab, "r:", label="CAB")
    plt.plot(gamma_set[:, 0], rmse_agt, "b-", label="AGT-ME")
    plt.xlim([0.0, 3.0])
    plt.ylim([1e-3, 1.0])
    plt.semilogy()
    plt.xlabel("Gamma bias $\gamma_b$", fontsize=16)
    plt.ylabel("RMSE", fontsize=16)
    plt.tick_params(labelsize=16)
    plt.legend(fontsize=16)

    foo_fig = plt.gcf()
    foo_fig.savefig("figs/Fig4b.eps", format='eps', dpi=1200)

    plt.show()
Beispiel #6
0
def line_value(image):
    return image[line_index, 250:850].astype(np.float)


# Step.0 path setting
image_dir = r"../images/slm image"
out_dir = r"./temp_out"
[os.remove(path) for path in glob.glob(out_dir + "/*")]

# Step.1 read images
path = glob.glob(image_dir + "/*")[0]
img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)  # read as gray image

# Step 2. conduct gamma estimation and image restoration with different methods or config
_, img_cab = correct_average_brightness(img)  # CAB
_, img_agt = adaptive_gamma_transform(img, visual=False)  # AGT
gamma, img_agt_visual = adaptive_gamma_transform(img,
                                                 visual=True)  # AGT-ME-VISUAL
_, img_bigc = blind_inverse_gamma_correction(img, "fast", True)  # BIGC

# Step.2 power spectrum analysis
names = ["ORIGINAL", "BIGC", "AGT-ME", "CAB"]
images = [img, img_bigc, img_agt, img_cab]
styles = ["g-", "r-.", "b-", "r:"]

line_index = 50  # 200
power_spec = [spectrum(temp) for temp in images]
line_values = [line_value(temp) for temp in images]

# Step.3 display the curves (intensity curve and power spectrum)
plt.figure(figsize=(6, 3))