-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_parser.py
More file actions
124 lines (92 loc) · 3.6 KB
/
image_parser.py
File metadata and controls
124 lines (92 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
def wrapp_the_image():
"""
IN PROGRESS
-> wrapp mrxs image into PIL image object
"""
## importation
from openslide import OpenSlide
from PIL import Image
from skimage import filters
from PIL import ImageFilter
import numpy
from matplotlib import pyplot as plt
## parameters
filename = "BGSA1.mrxs"
## Extract a PIL object
mrx_image = OpenSlide(filename)
truc = mrx_image.get_thumbnail((500,500))
# Find the edges by applying the filter ImageFilter.FIND_EDGES
imageWithEdges = truc.filter(ImageFilter.FIND_EDGES)
# display the new image with edge detection done
imageWithEdges.show()
print imageWithEdges
"""
pil_image = truc.convert('RGB')
open_cv_image = numpy.array(pil_image)
# Convert RGB to BGR
open_cv_image = open_cv_image[:, :, ::-1].copy()
import cv2
#Load the image in black and white (0 - b/w, 1 - color).
img = open_cv_image
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY);
#Get the height and width of the image.
h, w = img.shape[:2]
#Invert the image to be white on black for compatibility with findContours function.
imgray = 255 - img
#Binarize the image and call it thresh.
ret, thresh = cv2.threshold(imgray, 52, 255, cv2.THRESH_BINARY)
thresh = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY,11,2)
#Find all the contours in thresh. In your case the 3 and the additional strike
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
#Calculate bounding rectangles for each contour.
rects = [cv2.boundingRect(cnt) for cnt in contours]
print rects
#Calculate the combined bounding rectangle points.
top_x = min([x for (x, y, w, h) in rects])
top_y = min([y for (x, y, w, h) in rects])
bottom_x = max([x+w for (x, y, w, h) in rects])
bottom_y = max([y+h for (x, y, w, h) in rects])
#Draw the rectangle on the image
out = cv2.rectangle(img, (top_x, top_y), (bottom_x, bottom_y), (0, 255, 0), 2)
#Save it as out.jpg
cv2.imwrite('out.jpg', img)
"""
"""
ret,th1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
th2 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY,11,2)
th3 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,11,2)
titles = ['Original Image', 'Global Thresholding (v = 127)','Adaptive Mean Thresholding', 'Adaptive Gaussian Thresholding']
images = [img, th1, th2, th3]
for i in xrange(4):
plt.subplot(2,2,i+1),plt.imshow(images[i],'gray')
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
plt.show()
"""
"""
from skimage import measure
import numpy as np
# Construct some test data
x, y = np.ogrid[-np.pi:np.pi:100j, -np.pi:np.pi:100j]
r = np.sin(np.exp((np.sin(x)**3 + np.cos(y)**2)))
r = numpy.array(truc)
# Find contours at a constant value of 0.8
contours = measure.find_contours(r, 0.8)
# Display the image and plot all contours found
fig, ax = plt.subplots()
ax.imshow(r, interpolation='nearest', cmap=plt.cm.gray)
for n, contour in enumerate(contours):
ax.plot(contour[:, 1], contour[:, 0], linewidth=2)
ax.axis('image')
ax.set_xticks([])
ax.set_yticks([])
plt.show()
"""
import cv2
from numpy import array
pil_image = truc.convert('RGB')
img = numpy.array(pil_image)
img2 = array( 200 * (img[:,:,2] > img[:,:, 1]), dtype='uint8')
edges = cv2.Canny(img2, 70, 50)
cv2.imwrite('edges.png', edges)
wrapp_the_image()