/
Positive_cells.ipynb.json
117 lines (117 loc) · 6.12 KB
/
Positive_cells.ipynb.json
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
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter the file name of the positively stained cells image (output from ColorUnmixing.py): Figure_he.png\n"
]
},
{
"ename": "error",
"evalue": "OpenCV(4.2.0) /io/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'\n",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31merror\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-2-0bc46ac4dad2>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[0mimg\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcv\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbitwise_not\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mimg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 20\u001b[0m \u001b[0mshifted\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcv\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpyrMeanShiftFiltering\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mimg\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m21\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m51\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 21\u001b[0;31m \u001b[0mgray\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcv\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcvtColor\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mshifted\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcv\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mCOLOR_BGR2GRAY\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 22\u001b[0m \u001b[0mthresh\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcv\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mthreshold\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgray\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m255\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcv\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mTHRESH_BINARY\u001b[0m \u001b[0;34m|\u001b[0m \u001b[0mcv\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mTHRESH_OTSU\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 23\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31merror\u001b[0m: OpenCV(4.2.0) /io/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'\n"
]
}
],
"source": [
"from __future__ import print_function\n",
"from scipy import ndimage\n",
"from skimage.feature import peak_local_max\n",
"from skimage.segmentation import watershed\n",
"\n",
"import cv2 as cv\n",
"import imutils\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import skimage.io as io\n",
"\n",
"#1 9 5 for DAB_seg.png & Figure_HE.png\n",
"#1 1 1 for DAB3_seg.png\n",
"\n",
"#input the DAB image obtained from ColorUnmixing.py\n",
"user_input = input (\"Enter the file name of the positively stained cells image (output from ColorUnmixing.py): \")\n",
"img = cv.imread(user_input)\n",
"#white objects on black background \n",
"img = cv.bitwise_not(img)\n",
"shifted = cv.pyrMeanShiftFiltering(img, 21, 51)\n",
"gray = cv.cvtColor(shifted, cv.COLOR_BGR2GRAY)\n",
"thresh = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)[1]\n",
"\n",
"userInput1 = int(input (\"Enter the no. of iterations for [open]: \"))\n",
"userInput2 = int(input (\"Enter the no. of iterations for [dilate]: \"))\n",
"userInput3 = int(input (\"Enter the min. distance (in pixels) between peaks (1-20): \"))\n",
"\n",
"kernel = np.ones((3,3),np.uint8)\n",
"thresh = cv.morphologyEx(thresh, cv.MORPH_OPEN, kernel, iterations = userInput1)\n",
"thresh = cv.dilate(thresh, None, iterations=userInput2)\n",
"D = ndimage.distance_transform_edt(thresh)\n",
"localMax = peak_local_max(D, indices=False, min_distance=userInput3,labels=thresh)\n",
"markers = ndimage.label(localMax, structure=np.ones((3, 3)))[0]\n",
"labels = watershed(-D, markers, mask=thresh)\n",
"print(\"No. of positively stained cells: {}\".format(len(np.unique(labels)) - 1))\n",
"\n",
"userInput = input (\"Enter the file name of the all-cells image (output from ColorUnmixing.py) for accuracy check: \")\n",
"img_rgb = io.imread(userInput)\n",
"#img_rgb = cv.imread(userInput,cv.COLOR_BGR2RGB)\n",
"for label in np.unique(labels):\n",
" if label == 0:\n",
" continue\n",
" mask = np.zeros(gray.shape, dtype=\"uint8\")\n",
" mask[labels == label] = 255\n",
" cnts = cv.findContours(mask.copy(), cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)\n",
" cnts = imutils.grab_contours(cnts)\n",
" c = max(cnts, key=cv.contourArea)\n",
" ((x, y), r) = cv.minEnclosingCircle(c)\n",
" cv.circle(img_rgb, (int(x), int(y)), int(r), (0, 255, 0), 2)\n",
" cv.putText(img_rgb, \"#{}\".format(label), (int(x) - 10, int(y)), cv.FONT_HERSHEY_COMPLEX, 0.5, (0, 255, 0), 1) \n",
"\n",
"fig = plt.figure(frameon=False)\n",
"plt.rcParams['figure.figsize'] = 10, 10\n",
"plt.imshow(img_rgb)\n",
"plt.axis('off')\n",
"long_title = 'Settings : no. of iterations = {} & {} & min. distance = {}'\n",
"plt.title(long_title.format(userInput1, userInput2, userInput3))\n",
"plt.show()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}