|
2 | 2 | import numpy as np
|
3 | 3 | import matplotlib.pyplot as plt
|
4 | 4 | import cv2
|
| 5 | +from brightfield_helper import show_hist, show_equal |
5 | 6 |
|
6 | 7 | # MACROS FOR SHOWING IMAGES OF WHAT IS HAPPENING
|
7 | 8 | show_histograms = False
|
|
11 | 12 | # Import Clean and TestBrightfield Image
|
12 | 13 | main_bf_c = cv2.imread("data/brightfield_main_minerva.tif")
|
13 | 14 | test_bf_c = cv2.imread("data/f0113/F0113_10012021_initial_BF.tif")
|
14 |
| -#main_bf_c = cv2.imread("data/EGaudi_1.jpg") |
15 |
| -#test_bf_c = cv2.imread("data/EGaudi_2.jpg") |
16 | 15 |
|
17 | 16 | # Convert to grayscale
|
18 | 17 | main_bf = cv2.cvtColor(main_bf_c, cv2.COLOR_BGR2GRAY)
|
19 | 18 | test_bf = cv2.cvtColor(test_bf_c, cv2.COLOR_BGR2GRAY)
|
20 | 19 |
|
21 |
| -kernel_size = 5 |
22 |
| -main_bf = cv2.GaussianBlur(main_bf, (kernel_size, kernel_size), 0) |
23 |
| -test_bf = cv2.GaussianBlur(test_bf, (kernel_size, kernel_size), 0) |
24 |
| -# plt.imshow(main_bf) |
| 20 | +# f = np.fft.fft2(test_bf) |
| 21 | +# fshift = np.fft.fftshift(f) |
| 22 | +# magnitude_spectrum = 20*np.log(np.abs(fshift)) |
| 23 | +# plt.subplot(121),plt.imshow(test_bf, cmap = 'gray') |
| 24 | +# plt.title('Input Image') |
| 25 | +# plt.subplot(122),plt.imshow(magnitude_spectrum, cmap = 'gray') |
| 26 | +# plt.title('Magnitude Spectrum') |
25 | 27 | # plt.show()
|
26 | 28 |
|
27 |
| -# dst = cv2.cornerHarris(main_bf, 15, 15, 0.03) |
28 |
| -# dst = cv2.dilate(dst, None) |
29 |
| -# main_bf_c[dst > 0.01 * dst.max()]=[255,0,0] |
30 |
| -# plt.imshow(main_bf_c) |
| 29 | +# f = np.fft.fft2(main_bf) |
| 30 | +# fshift = np.fft.fftshift(f) |
| 31 | +# magnitude_spectrum = 20*np.log(np.abs(fshift)) |
| 32 | +# plt.subplot(121),plt.imshow(main_bf, cmap = 'gray') |
| 33 | +# plt.title('Input Image') |
| 34 | +# plt.subplot(122),plt.imshow(magnitude_spectrum, cmap = 'gray') |
| 35 | +# plt.title('Magnitude Spectrum') |
31 | 36 | # plt.show()
|
32 | 37 |
|
33 |
| -# Equalize Histogram to get Same Brightness and Contrast of Both Images |
34 |
| -if show_histograms: |
35 |
| - # TODO: Put this in a function and another file so that it is easier to read |
36 |
| - # Main Brightfield Image |
37 |
| - hist,bins = np.histogram(main_bf.flatten(),256,[0,256]) |
38 |
| - cdf_normalized = hist.cumsum() * float(hist.max()) / hist.cumsum().max() |
39 |
| - fig, ax = plt.subplots(1,2) |
40 |
| - ax[0].plot(cdf_normalized, color = 'b') |
41 |
| - ax[0].hist(main_bf.flatten(),256,[0,256], color = 'r') |
42 |
| - ax[0].legend(('cdf','histogram'), loc = 'upper left') |
43 |
| - ax[0].set_title("Main BF Histogram") |
44 |
| - |
45 |
| - # Test Brightfield Image |
46 |
| - hist,bins = np.histogram(test_bf.flatten(),256,[0,256]) |
47 |
| - cdf_normalized = hist.cumsum() * float(hist.max()) / hist.cumsum().max() |
48 |
| - ax[1].plot(cdf_normalized, color = 'b') |
49 |
| - ax[1].hist(test_bf.flatten(),256,[0,256], color = 'r') |
50 |
| - ax[1].legend(('cdf','histogram'), loc = 'upper left') |
51 |
| - ax[1].set_title("Test BF Histogram") |
52 | 38 |
|
53 |
| - plt.show() |
54 | 39 |
|
| 40 | +kernel_size = 5 |
| 41 | +main_bf = cv2.GaussianBlur(main_bf, (kernel_size, kernel_size), 0) |
| 42 | +#main_bf = main_bf - main_bf_blur |
| 43 | +test_bf = cv2.GaussianBlur(test_bf, (kernel_size, kernel_size), 0) |
| 44 | +#test_bf = test_bf - test_bf_blur |
| 45 | + |
| 46 | +print("MEOW") |
| 47 | +# Equalize Histogram to get Same Brightness and Contrast of Both Images |
| 48 | +show_hist(main_bf, test_bf, show_histograms) |
55 | 49 | equ_main = cv2.equalizeHist(main_bf)
|
56 | 50 | equ_test = cv2.equalizeHist(test_bf)
|
| 51 | +show_equal(main_bf, equ_main, test_bf, equ_test, show_equalized) |
57 | 52 |
|
58 |
| -if show_equalized: |
59 |
| - # TODO: Put this in a function and another file so that it is easier to read |
60 |
| - fig = plt.figure(2) |
61 |
| - fig.add_subplot(2,2,1) |
62 |
| - plt.title("Main Brightfield") |
63 |
| - plt.imshow(main_bf) |
64 |
| - |
65 |
| - fig.add_subplot(2,2,2) |
66 |
| - plt.title("Equalized Main Brightfield") |
67 |
| - plt.imshow(equ_main) |
68 |
| - |
69 |
| - fig.add_subplot(2,2,3) |
70 |
| - plt.title("Test Brightfield") |
71 |
| - plt.imshow(test_bf) |
72 |
| - |
73 |
| - fig.add_subplot(2,2,4) |
74 |
| - plt.title("Equalized Test Brightfield") |
75 |
| - plt.imshow(equ_test) |
76 |
| - plt.show() |
| 53 | +print("MEOW1") |
77 | 54 |
|
78 | 55 | # Apply Feature Match Algorithm between Brightfield Images
|
79 | 56 | sift_detect = cv2.SIFT_create()
|
80 | 57 | kp1,d1 = sift_detect.detectAndCompute(equ_main, None)
|
81 | 58 | kp2,d2 = sift_detect.detectAndCompute(equ_test, None)
|
82 | 59 |
|
| 60 | +print("MEOW2") |
| 61 | + |
83 | 62 | FLAN_INDEX_KDTREE = 0
|
84 | 63 | index_params = dict (algorithm = FLAN_INDEX_KDTREE, trees=5)
|
85 |
| -search_params = dict (checks=50) |
| 64 | +search_params = dict(checks=50) |
86 | 65 |
|
87 | 66 | flann = cv2.FlannBasedMatcher(index_params, search_params)
|
88 |
| - |
89 | 67 | matches = flann.knnMatch(d1, d2, k=2)
|
90 | 68 |
|
| 69 | +print("MEOW3") |
| 70 | + |
91 | 71 | # Determine Uniqueness of Flann Matches using ratio test
|
| 72 | +RATIO = 0.6 |
92 | 73 | unique_matches = []
|
93 | 74 | for a,b in matches:
|
94 |
| - if a.distance < 0.6 * b.distance: |
| 75 | + if a.distance < RATIO * b.distance: |
95 | 76 | unique_matches.append(a)
|
96 | 77 |
|
97 | 78 | if draw_matches:
|
|
110 | 91 | dst_pts = np.float32([kp2[m.trainIdx].pt for m in unique_matches]).reshape(-1,1,2)
|
111 | 92 |
|
112 | 93 | M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC,5.0)
|
113 |
| -print(M) |
114 | 94 |
|
| 95 | +print("MEOW4") |
115 | 96 | # All this stuff is just to have
|
116 | 97 | matchesMask = mask.ravel().tolist()
|
117 | 98 | h,w = main_bf.shape
|
|
127 | 108 | plt.show()
|
128 | 109 |
|
129 | 110 | # With Matrix, we can find chip corner, knowing where it is on clean brightfield
|
| 111 | +# TLC, TRC, BLC, BRC |
| 112 | +CLEAN_BRIGHTFIELD_CORNERS = [(241, 765), (1827, 802), (224, 1561), (1810, 1596)] |
| 113 | +NEW_CORNERS = [(0,0), (1600, 0), (0, 800), (1600, 800)] |
| 114 | + |
| 115 | +# Identify chip corners on experimental setup |
| 116 | +bf_test_corners = cv2.perspectiveTransform(np.float32(CLEAN_BRIGHTFIELD_CORNERS).reshape(-1,1,2), M) |
| 117 | +bf_test_corners = sum(np.int32(bf_test_corners.round()).tolist(), []) |
| 118 | + |
| 119 | +print(bf_test_corners) |
| 120 | + |
| 121 | +for (x,y) in bf_test_corners: |
| 122 | + test_bf_c = cv2.circle(test_bf_c, (x,y), radius=3, color=(255, 0, 0), thickness=-1) |
| 123 | + |
| 124 | +plt.imshow(test_bf_c) |
| 125 | +plt.show() |
| 126 | + |
| 127 | +for (x,y) in CLEAN_BRIGHTFIELD_CORNERS: |
| 128 | + main_bf_c = cv2.circle(main_bf_c, (x,y), radius=0, color=(255, 0, 0), thickness=-1) |
| 129 | + |
| 130 | +plt.imshow(main_bf_c) |
| 131 | +plt.show() |
| 132 | + |
| 133 | +M_clean_to_sense = cv2.getPerspectiveTransform(np.float32(CLEAN_BRIGHTFIELD_CORNERS),np.float32(NEW_CORNERS)) |
| 134 | +dst = cv2.warpPerspective(main_bf_c,M_clean_to_sense,(1600,800)) |
| 135 | + |
| 136 | +M_test_to_clean = cv2.getPerspectiveTransform(np.float32(bf_test_corners), np.float32(NEW_CORNERS)) |
| 137 | +dst2 = cv2.warpPerspective(test_bf_c, M_test_to_clean, (1600,800)) |
| 138 | + |
| 139 | +plt.imshow(dst2) |
| 140 | +plt.show() |
130 | 141 |
|
131 | 142 | # Multiply Affine Matrix with clean brightfield image to get to square pixels
|
132 | 143 |
|
|
0 commit comments