vusr commited on
Commit
cf41815
·
verified ·
1 Parent(s): d3b31be

Upload Connect_Components_Preprocessing.py

Browse files
Files changed (1) hide show
  1. Connect_Components_Preprocessing.py +136 -0
Connect_Components_Preprocessing.py ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Created on Mon Oct 3 16:09:11 2022
4
+
5
+ @author: jpeeples
6
+ """
7
+ import cv2
8
+ from skimage.filters import threshold_li
9
+ from skimage.morphology import (erosion, dilation, opening, closing, # noqa
10
+ white_tophat)
11
+ import matplotlib.pyplot as plt
12
+ import numpy as np
13
+ from sklearn.decomposition import PCA
14
+ from sklearn.preprocessing import MinMaxScaler
15
+
16
+
17
+ def CCA_Preprocess(composite_img, k=2):
18
+
19
+ #Use pca to reduce vector
20
+ reshaped_composite_img = np.reshape(composite_img,(-1,3))
21
+
22
+ #Apply PCA
23
+ pca = PCA(n_components=1, whiten=True)
24
+ gray_vector = pca.fit_transform(reshaped_composite_img)
25
+
26
+ #Visualize image
27
+ gray_img = np.reshape(gray_vector,(512,512))
28
+
29
+ #Normalize and scale between 0 and 255 (inclusive)
30
+ scaler = MinMaxScaler(feature_range=(0, 1))
31
+ scaler.fit(gray_img)
32
+ gray_img = scaler.transform(gray_img)
33
+
34
+ # Applying 7x7 Gaussian Blur
35
+ blurred = cv2.GaussianBlur(np.uint8(gray_img*255), (5, 5), 0)
36
+
37
+ #Sharpen Image
38
+ kernel3 = np.array([[0, -1, 0],
39
+ [-1, 5, -1],
40
+ [0, -1, 0]])
41
+
42
+ blurred = cv2.filter2D(src=blurred, ddepth=-1, kernel=kernel3)
43
+
44
+
45
+ # #Threshold image from background for CCA
46
+ thresh = threshold_li(gray_img)
47
+ binary = gray_img > thresh
48
+
49
+ # kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 400))
50
+
51
+ # Mask = cv2.erode(np.uint8(binary*255), kernel)
52
+
53
+ # # Expand the mask in the vertical direction
54
+ # kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (10, 400))
55
+ # Mask = cv2.dilate(Mask, kernel)
56
+
57
+ # # Erase the connection by placing zeros
58
+ # binary[np.where(Mask != 0)] = 0
59
+
60
+
61
+ img = np.repeat(np.expand_dims(binary * gray_img, axis = -1), 3, axis=-1)
62
+
63
+ # Applying threshold
64
+ threshold = np.uint8(binary*255)
65
+
66
+ # Apply the Component analysis function
67
+ analysis = cv2.connectedComponentsWithStats(threshold,
68
+ 4,
69
+ cv2.CV_32S)
70
+ (totalLabels, label_ids, values, centroid) = analysis
71
+
72
+ # Initialize a new image to
73
+ # store all the output components
74
+ output = np.zeros(gray_img.shape, dtype="uint8")
75
+
76
+ # Loop through each component
77
+ for i in range(1, totalLabels):
78
+
79
+ # if (area > 10000) and (area < 50000):
80
+ if i in np.argsort(values[:,4])[-k:]:
81
+ # Create a new image for bounding boxes
82
+ new_img=img.copy()
83
+
84
+ # Now extract the coordinate points
85
+ x1 = values[i, cv2.CC_STAT_LEFT]
86
+ y1 = values[i, cv2.CC_STAT_TOP]
87
+ w = values[i, cv2.CC_STAT_WIDTH]
88
+ h = values[i, cv2.CC_STAT_HEIGHT]
89
+
90
+ # Coordinate of the bounding box
91
+ pt1 = (x1, y1)
92
+ pt2 = (x1+ w, y1+ h)
93
+ (X, Y) = centroid[i]
94
+
95
+ # Bounding boxes for each component
96
+ cv2.rectangle(new_img,pt1,pt2,
97
+ (0, 255, 0), 3)
98
+ cv2.circle(new_img, (int(X),
99
+ int(Y)),
100
+ 4, (0, 0, 255), -1)
101
+
102
+ # Create a new array to show individual component
103
+ component = np.zeros(gray_img.shape, dtype="uint8")
104
+ componentMask = (label_ids == i).astype("uint8") * 255
105
+
106
+ # Apply the mask using the bitwise operator
107
+ component = cv2.bitwise_or(component,componentMask)
108
+ output = cv2.bitwise_or(output, componentMask)
109
+
110
+ # fig, axes = plt.subplots(ncols=3, figsize=(8, 2.5))
111
+ # ax = axes.ravel()
112
+ # ax[0] = plt.subplot(1, 3, 1)
113
+ # ax[1] = plt.subplot(1, 3, 2, sharex=ax[0], sharey=ax[0])
114
+ # ax[2] = plt.subplot(1, 3, 3, sharex=ax[0], sharey=ax[0])
115
+ # # ax[3] = plt.subplot(1,4,4,sharex=ax[0], sharey=ax[0])
116
+
117
+ # ax[0].imshow(gray_img, cmap='BuGn')
118
+ # ax[0].set_title('Original')
119
+ # ax[0].axis('off')
120
+
121
+ # ax[1].imshow(output, cmap='BuGn')
122
+ # ax[1].set_title('Connected Components')
123
+ # ax[1].axis('off')
124
+
125
+ # ax[2].imshow(gray_img*(output/255), cmap='BuGn')
126
+ # ax[2].set_title('ROI')
127
+ # ax[2].axis('off')
128
+
129
+ # plt.suptitle(title)
130
+
131
+ # plt.tight_layout()
132
+ # fig.savefig('{}/Img_{}.png'.format(folder,img_index))
133
+ # plt.close()
134
+
135
+ #Return gray image and mask
136
+ return gray_img, output/255