Last active
February 18, 2022 14:18
-
-
Save XinyueZ/9c35de8a716996a4774ccdb64fada592 to your computer and use it in GitHub Desktop.
Simple segmentation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import matplotlib.pyplot as plt | |
| import cv2 | |
| import numpy as np | |
| !wget https://dl.dropbox.com/s/i1e1brycec3hy3k/pilot-view.jpeg -O pilot-view.png | |
| # For this evalution, we assume that we load image with 3 channels. | |
| def toRGB(img): | |
| return cv2.cvtColor(img, cv2.COLOR_BGR2RGB) | |
| def do_segmentation(input_img, threshold, max_value=255, min_value=0): | |
| N, M, C = input_img.shape | |
| image_out = np.zeros((N, M), dtype=np.uint8) | |
| label_out = np.zeros((N, M), dtype=np.uint8) | |
| for i in range(N): | |
| for j in range(M): | |
| if np.average(input_img[i, j, :]) >= threshold: | |
| image_out[i, j] = max_value | |
| label_out[i, j] = 1 | |
| else: | |
| image_out[i, j] = min_value | |
| label_out[i, j] = 0 | |
| return image_out, label_out | |
| def extract_hist(img): | |
| list = [] | |
| _, _, C = img.shape | |
| for i in range(C): | |
| list.append(cv2.calcHist([img], [ | |
| i], None, [256], [0, 256])) | |
| return list, ["black"] if C == 1 else ["red", "green", "blue"] | |
| pilot_view_img = toRGB(cv2.imread("pilot-view.png")) | |
| print(pilot_view_img.shape) | |
| M, N, C = pilot_view_img.shape | |
| # For this evalution, we assume that we load image with 3 channels. | |
| assert C == 3 | |
| # Show the interesting starts with contrast and high brightness background | |
| pilot_view_neg_img = -1 * pilot_view_img + 255 | |
| plt.imshow(pilot_view_neg_img) | |
| plt.show() | |
| # The digital image has intensity valus of each pixel from 0~255, #size:256 | |
| intensity_values = np.arange(256) | |
| # Extract the intensity distributions of all channels of each pixels | |
| hist_list, colors = extract_hist(pilot_view_img) | |
| # Extract pixels which have high intensity (brightness) | |
| # The output image has pixel gray intensity 255 when the same pixel position of input has intensity >= 150, otherwise 0. | |
| # For the label output the pixel given 255 has label "1", else "0". | |
| result, label = do_segmentation(pilot_view_img, 150, 255, 0) | |
| print(f"result shape {result.shape}") | |
| plt.figure(figsize=(20, 20)) | |
| plt.subplot(131) | |
| plt.title("origin") | |
| plt.imshow(pilot_view_img) | |
| plt.subplot(132) | |
| plt.title("segmentation result") | |
| plt.imshow(result) | |
| # Bouns: Plot labels, the interesting pixel has been labeled as "1", otherwise be "0" | |
| plt.subplot(133) | |
| plt.title("outline by labels") | |
| M, N = label.shape | |
| label_image = np.zeros((M, N, C)) | |
| for i in range(M): | |
| for j in range(N): | |
| label_image[i, j, :] = 0 if label[i, j] == 0 else 255 | |
| plt.imshow(label_image) | |
| plt.show() | |
| # Let's compare the distribution of inensitiy distrubution | |
| # Intensity distribution of origin image | |
| plt.figure(figsize=(15, 4)) | |
| hist_orign_list, colors = extract_hist(pilot_view_img) | |
| for i, hist, in enumerate(hist_orign_list): | |
| plt.subplot(1, len(hist_orign_list), i+1) | |
| plt.title(f"{colors[i]} channel #{i+1}") | |
| plt.bar(intensity_values, hist[:, 0], color=[colors[i]]) | |
| plt.show() | |
| # Intensity distribution of result | |
| plt.figure(figsize=(15, 4)) | |
| hist_result_list, colors = extract_hist(np.expand_dims(result, -1)) | |
| for i, hist, in enumerate(hist_result_list): | |
| plt.subplot(1, len(hist_result_list), i+1) | |
| plt.title(f"Channel {i+1}") | |
| plt.bar(intensity_values, hist[:, 0], color=[colors[i]]) | |
| plt.show() | |
| # Plot continuous function for all channels intensity and greyscale of result | |
| plt.figure(figsize=(15, 15)) | |
| all_list = hist_orign_list + hist_result_list | |
| all_colors = ["r", "g", "b", "m"] | |
| for i, hist in enumerate(all_list): | |
| plt.plot(intensity_values, hist, color=all_colors[i]) | |
| plt.axis([0, 300, 0, 1000]) | |
| plt.legend(["red ch", "green ch", "blue ch", "result graysale"]) | |
| plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment