Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. infiles = list(map(addpath, os.listdir('./photos/source/')))
  2. statfile = open('stat_resized_pyr-2.txt', 'w')
  3. for egg_image in infiles:
  4. #read and compress photo
  5. #
  6. #
  7. orig_img = cv2.imread(egg_image)
  8.  
  9. scale_percent = 20
  10. width = int(orig_img.shape[1] * scale_percent / 100)
  11. height = int(orig_img.shape[0] * scale_percent / 100)
  12. dim = (width, height)
  13. img = cv2.resize(orig_img, dim, interpolation = cv2.INTER_AREA)
  14. # img = orig_img
  15. #color in range
  16. #
  17. #
  18. img = cv2.pyrMeanShiftFiltering(img, 120, 51)
  19. img = cv2.GaussianBlur(img, (7, 7), 2)
  20. shifted_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  21. shifted_hsv = cv2.cvtColor(shifted_rgb, cv2.COLOR_RGB2HSV)
  22.  
  23. #HSV->HSV convertation
  24. #cv2 HSV: 0-179, 0-255, 0-255
  25. #paint.net HSV: 0-360, 0-100, 0-100
  26. lb, db = (175, 0, 80), (210, 40, 100)
  27. light_blue = (lb[0]/2, int(lb[1]*2.55), int(lb[2]*2.55))
  28. dark_blue = (db[0]/2, int(db[1]*2.55), int(db[2]*2.55))
  29.  
  30. mask = cv2.inRange(shifted_hsv, light_blue, dark_blue)
  31. res_mask = cv2.bitwise_and(shifted_rgb, shifted_rgb, mask = mask)
  32.  
  33. #res_mask_shifted = cv2.pyrMeanShiftFiltering(res_mask, 120, 51)
  34. #res_mask_shifted = cv2.pyrMeanShiftFiltering(res_mask, 20, 51)
  35.  
  36. #plt.imshow(res_mask_shifted)
  37. #print(mask.shape)
  38.  
  39. gray = cv2.cvtColor(res_mask,cv2.COLOR_BGR2GRAY)
  40. ret, thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
  41. #plt.imshow(res_mask)
  42.  
  43.  
  44. # noise removal
  45. #
  46. #
  47. kernel = np.ones((3,3),np.uint8)
  48. opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, iterations = 3)
  49. for string in opening:
  50. for i in range(len(string)):
  51. if(string[i] == 255): string[i] = 0
  52. else: string[i] = 255
  53. #plt.imshow(opening)
  54.  
  55.  
  56. # sure background area
  57. #
  58. #
  59. sure_bg = cv2.dilate(opening,kernel,iterations=3)
  60. #else: string[i] = 255
  61. #plt.imshow(sure_bg)
  62.  
  63. # Finding sure foreground area
  64. #
  65. #
  66. dist_transform = cv2.distanceTransform(opening,cv2.DIST_L2,5)
  67. ret, sure_fg = cv2.threshold(dist_transform,0.4*dist_transform.max(),255,0)
  68.  
  69. #plt.imshow(sure_fg)
  70. #plt.imshow(dist_transform, 'gray')
  71.  
  72. # Finding unknown region
  73. #
  74. #
  75. sure_fg = np.uint8(sure_fg)
  76. unknown = cv2.subtract(sure_bg,sure_fg)
  77. #plt.imshow(sure_fg)
  78.  
  79.  
  80. # Marker labelling
  81. #
  82. #
  83. %matplotlib inline
  84. ret, markers = cv2.connectedComponents(sure_fg)
  85. #print(ret)
  86. #plt.imshow(markers)
  87.  
  88.  
  89. # Add one to all labels so that sure background is not 0, but 1
  90. markers = markers+1
  91. # Now, mark the region of unknown with zero
  92. markers[unknown==255] = 0
  93. #plt.imshow(markers)
  94.  
  95. #
  96. #
  97. %matplotlib inline
  98.  
  99. import time
  100. markers = cv2.watershed(res_mask,markers)
  101. res_mask[markers == -1] = [255,0,0]
  102. plt.imshow(markers)
  103. #print(markers)
  104. eggs = []
  105. result = img.copy()
  106. for egg_i in range(0, ret+1):
  107. egg = np.zeros(shape = markers.shape)
  108. for i in range(len(markers)):
  109. for j in range(len(markers[i])):
  110. if(markers[i,j] == egg_i):
  111. egg[i,j] = egg_i
  112. eggs.append(egg)
  113.  
  114. counter = 0
  115. print(len(eggs))
  116. for i in range(2, len(eggs)):
  117.  
  118. result1 = cv2.bitwise_and(img, img, mask=np.int8(eggs[i]))
  119.  
  120. contours = findContours(result1)
  121. maxArea = 0
  122. ind = 0
  123.  
  124. for c in range(len(contours)):
  125. area = cv2.contourArea(contours[c]) # площадь
  126. if(area > maxArea):
  127. ind = c
  128. maxArea = area
  129. try:
  130. kk = contours[ind]
  131. except:
  132. continue
  133. if(len(contours[ind]) < 5): continue
  134. (x, y), (MA, ma), angle = cv2.fitEllipse(contours[ind])
  135. el_area = 3.14 * MA * ma / 4
  136. size = MA*ma*ma*4*3.14/3
  137. if(el_area > area*1.2 or el_area < area*0.2): continue
  138.  
  139. cv2.ellipse(result1, (int(x), int(y)), (int(MA/2), int(ma/2)), int(angle), startAngle = 0, endAngle = 360, color = (0,255,0), thickness = 1)
  140. cv2.imwrite('./photos/output_rsz_pyr-2/'+egg_image.split('/')[3][:-4]+'_result_'+str(i-1) +'.jpg',result1)
  141.  
  142. statfile.write(egg_image.split('/')[3]+'\t')
  143.  
  144. statfile.write('{}\t'.format(i-1))
  145. statfile.write('{}\t'.format(area))
  146. statfile.write('{}\t'.format(el_area))
  147. statfile.write('{}\t'.format(round(size,2)))
  148. statfile.write('{}\t'.format(MA))
  149. statfile.write('{}\n'.format(ma))
  150. counter += 1
  151. statfile.write('eggs: {}\n\n'.format(counter))
  152. statfile.close()
  153. statfile = open('stat_resized_pyr-2.txt', 'a')
  154. statfile.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement