Skip to content

Commit e9dc0e1

Browse files
committed
Super Resolution and Brightfield Fourier Transforms
1 parent bfb22d7 commit e9dc0e1

File tree

5 files changed

+1206
-53
lines changed

5 files changed

+1206
-53
lines changed

brightfield_helper.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import sys
2+
import numpy as np
3+
import matplotlib.pyplot as plt
4+
import cv2
5+
6+
def show_hist(main_bf, test_bf, show_histograms):
7+
if show_histograms:
8+
# Main Brightfield Image
9+
hist,bins = np.histogram(main_bf.flatten(),256,[0,256])
10+
cdf_normalized = hist.cumsum() * float(hist.max()) / hist.cumsum().max()
11+
fig, ax = plt.subplots(1,2)
12+
ax[0].plot(cdf_normalized, color = 'b')
13+
ax[0].hist(main_bf.flatten(),256,[0,256], color = 'r')
14+
ax[0].legend(('cdf','histogram'), loc = 'upper left')
15+
ax[0].set_title("Main BF Histogram")
16+
17+
# Test Brightfield Image
18+
hist,bins = np.histogram(test_bf.flatten(),256,[0,256])
19+
cdf_normalized = hist.cumsum() * float(hist.max()) / hist.cumsum().max()
20+
ax[1].plot(cdf_normalized, color = 'b')
21+
ax[1].hist(test_bf.flatten(),256,[0,256], color = 'r')
22+
ax[1].legend(('cdf','histogram'), loc = 'upper left')
23+
ax[1].set_title("Test BF Histogram")
24+
25+
plt.show()
26+
27+
def show_equal(main_bf, equ_main, test_bf, equ_test, show_equalized):
28+
if show_equalized:
29+
# TODO: Put this in a function and another file so that it is easier to read
30+
fig = plt.figure(2)
31+
fig.add_subplot(2,2,1)
32+
plt.title("Main Brightfield")
33+
plt.imshow(main_bf)
34+
35+
fig.add_subplot(2,2,2)
36+
plt.title("Equalized Main Brightfield")
37+
plt.imshow(equ_main)
38+
39+
fig.add_subplot(2,2,3)
40+
plt.title("Test Brightfield")
41+
plt.imshow(test_bf)
42+
43+
fig.add_subplot(2,2,4)
44+
plt.title("Equalized Test Brightfield")
45+
plt.imshow(equ_test)
46+
plt.show()

brightfield_match_attempt_2.py

Lines changed: 64 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import numpy as np
33
import matplotlib.pyplot as plt
44
import cv2
5+
from brightfield_helper import show_hist, show_equal
56

67
# MACROS FOR SHOWING IMAGES OF WHAT IS HAPPENING
78
show_histograms = False
@@ -11,87 +12,67 @@
1112
# Import Clean and TestBrightfield Image
1213
main_bf_c = cv2.imread("data/brightfield_main_minerva.tif")
1314
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")
1615

1716
# Convert to grayscale
1817
main_bf = cv2.cvtColor(main_bf_c, cv2.COLOR_BGR2GRAY)
1918
test_bf = cv2.cvtColor(test_bf_c, cv2.COLOR_BGR2GRAY)
2019

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')
2527
# plt.show()
2628

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')
3136
# plt.show()
3237

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")
5238

53-
plt.show()
5439

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)
5549
equ_main = cv2.equalizeHist(main_bf)
5650
equ_test = cv2.equalizeHist(test_bf)
51+
show_equal(main_bf, equ_main, test_bf, equ_test, show_equalized)
5752

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")
7754

7855
# Apply Feature Match Algorithm between Brightfield Images
7956
sift_detect = cv2.SIFT_create()
8057
kp1,d1 = sift_detect.detectAndCompute(equ_main, None)
8158
kp2,d2 = sift_detect.detectAndCompute(equ_test, None)
8259

60+
print("MEOW2")
61+
8362
FLAN_INDEX_KDTREE = 0
8463
index_params = dict (algorithm = FLAN_INDEX_KDTREE, trees=5)
85-
search_params = dict (checks=50)
64+
search_params = dict(checks=50)
8665

8766
flann = cv2.FlannBasedMatcher(index_params, search_params)
88-
8967
matches = flann.knnMatch(d1, d2, k=2)
9068

69+
print("MEOW3")
70+
9171
# Determine Uniqueness of Flann Matches using ratio test
72+
RATIO = 0.6
9273
unique_matches = []
9374
for a,b in matches:
94-
if a.distance < 0.6 * b.distance:
75+
if a.distance < RATIO * b.distance:
9576
unique_matches.append(a)
9677

9778
if draw_matches:
@@ -110,8 +91,8 @@
11091
dst_pts = np.float32([kp2[m.trainIdx].pt for m in unique_matches]).reshape(-1,1,2)
11192

11293
M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC,5.0)
113-
print(M)
11494

95+
print("MEOW4")
11596
# All this stuff is just to have
11697
matchesMask = mask.ravel().tolist()
11798
h,w = main_bf.shape
@@ -127,6 +108,36 @@
127108
plt.show()
128109

129110
# 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()
130141

131142
# Multiply Affine Matrix with clean brightfield image to get to square pixels
132143

0 commit comments

Comments
 (0)