• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest May 16th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import imageio
2. import numpy as np
3. import matplotlib.pyplot as plt
4.
5. compartments_quantity = 50
6.
7.
8. def normalize(image):
9.     """
10.    This method normalize the image value
11.    :param image: value after the conversion to grayscale
12.    :return: normalized output
13.    """
14.     offset = 255.0 / compartments_quantity
15.     output = np.zeros((image.shape[0], image.shape[0]))
16.     i = 0
17.     tmp = 0
18.     while i < 255.0:
19.         for x in range(0, image.shape[0]):
20.             for y in range(0, image.shape[1]):
21.                 if i < image[x][y] <= i + offset:
22.                     output[x][y] = tmp
23.         tmp = tmp + 1
24.         i = i + offset
25.     return output
26.
27.
28. def calculate_sharpness(coincident_matrix):
29.     """
30.    Method calculate the sharpness
31.    as the sum of diagonal pixels
32.    :param coincident_matrix: concident matrix
33.    :return: number of pixels on the diagonal
34.    """
35.     sum_diag_pixels = 0
36.     for i in range(0, coincident_matrix.shape[0]):
37.         for j in range(0, coincident_matrix.shape[1]):
38.             if coincident_matrix[i][j] != 0:
39.                 sum_diag_pixels += 1
40.     return sum_diag_pixels
41.
42.
43. def convert_rgb_2_gray(img_rgb):
44.     """
45.    Method convert image from rgb to grayscale
46.    :param img_rgb: image matrix in rgb
47.    :return: image value in grayscale
48.    """
49.     r = img_rgb[:, :, 0]
50.     g = img_rgb[:, :, 1]
51.     b = img_rgb[:, :, 2]
52.     img_gray = 0.2126 * r + 0.7152 * g + 0.0722 * b
53.     return img_gray
54.
55.
56. def create_coincident_matrix(img, dlx, dly):
57.     """
58.    Method create the coincident_matrix
59.    from image in grayscale.
60.    :param img: Image in grayscale
61.    :param dlx: lenght of x
62.    :param dly: length of y
63.    :return: Coincident matrix
64.    """
65.     p_matrix = np.max(img).astype(int) + 1
66.     coincident_matrix = np.zeros((p_matrix, p_matrix))
67.     height = len(img)
68.     width = len(img[0])
69.
70.     for x in range(0, height - dlx):
71.         for y in range(0, width - dly):
72.             xx = img[x, y]
73.             xx = xx.astype(int)
74.             yy = img[x + dlx, y + dly]
75.             yy = yy.astype(int)
76.             coincident_matrix[xx, yy] = coincident_matrix[xx, yy] + 1
77.
78.     return coincident_matrix
79.
80.
81. if __name__ == '__main__':
82.
83.     # First image
84.
86.     img_1 = convert_rgb_2_gray(img_1)   # Converting rgb -> grayscale
87.     img_1 = normalize(img_1)            # Value normalization
88.
89.     plt.subplot(2, 2, 1)                # Cut the plot workspace to nrows x ncols x index
90.     # Draw image
91.     plt.imshow(img_1, cmap=plt.cm.gray, vmin=0, vmax=compartments_quantity)
92.     plt.title("oko1.png")               # Show the plot title
93.
94.     plt.subplot(2, 2, 2)
95.     # Draw histogram
96.     plt.imshow(create_coincident_matrix(img_1, 0, 1), cmap=plt.cm.gray, vmin=0, vmax=compartments_quantity)
97.     plt.title("macierz wspowwystepowania oko1.png")
98.
99.     print("Ostrosc oko1: " + str(calculate_sharpness(create_coincident_matrix(img_1, 0, 1))))
100.
101.     # Second image
102.
104.     img_2 = convert_rgb_2_gray(img_2)
105.     img_2 = normalize(img_2)
106.
107.     plt.subplot(2, 2, 3)
108.     plt.imshow(img_2, cmap=plt.cm.gray, vmin=0, vmax=compartments_quantity)
109.     plt.title("oko2.png")
110.
111.     plt.subplot(2, 2, 4)
112.     plt.imshow(create_coincident_matrix(img_2, 0, 1), cmap=plt.cm.gray, vmin=0, vmax=compartments_quantity)
113.     plt.title("macierz wspowwystepowania oko2.png")
114.
115.     print("Ostrosc oko2: " + str(calculate_sharpness(create_coincident_matrix(img_2, 0, 1))))
116.
117.     # Show all calculations and histograms
118.
119.     plt.show()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top