Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.45 KB | None | 0 0
  1. import cv2 as cv2
  2. import numpy as np
  3.  
  4. source = cv2.imread("N2.png", cv2.IMREAD_GRAYSCALE)
  5. dst = cv2.copyMakeBorder(source, 1, 1, 1, 1, cv2.BORDER_CONSTANT, None, 0)
  6.  
  7. final = dst[:]
  8. window = [0] * 9
  9. for y in range(1, dst.shape[0] - 1):
  10.     for x in range(1, dst.shape[1] - 1):
  11.         window[0] = dst[y - 1, x - 1]
  12.         window[1] = dst[y, x - 1]
  13.         window[2] = dst[y + 1, x - 1]
  14.         window[3] = dst[y - 1, x]
  15.         window[4] = dst[y, x]
  16.         window[5] = dst[y + 1, x]
  17.         window[6] = dst[y - 1, x + 1]
  18.         window[7] = dst[y, x + 1]
  19.         window[8] = dst[y + 1, x + 1]
  20.         window.sort()
  21.         final[y, x] = window[4]
  22. final = final[1:final.shape[0] - 1, 1:final.shape[1] - 1]
  23.  
  24. cv2.imshow('source', final)  # Show the image
  25. cv2.waitKey(0)
  26.  
  27. thresh, final_Binary= cv2.threshold(final, 57, 255, cv2.THRESH_BINARY)
  28.  
  29. cv2.imshow('Final_Picture1_Binary', final_Binary)  # Show the image
  30. cv2.waitKey(0)
  31.  
  32. kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3))
  33. final_transformed = cv2.morphologyEx(final_Binary, cv2.MORPH_OPEN, kernel,iterations=3)
  34. cv2.imshow('Final_Picture12_', final_transformed)  # Show the image
  35. cv2.waitKey(0)
  36.  
  37. final_transformed = cv2.morphologyEx(final_transformed, cv2.MORPH_CLOSE, kernel,iterations=1)
  38. cv2.imshow('Final_Picture12', final_transformed)  # Show the image
  39. cv2.waitKey(0)
  40.  
  41.  
  42. _, contours, _ = cv2.findContours(final_transformed, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
  43.  
  44. print("Number of Cells in the image including those touching the borders " + str(len(contours)))
  45.  
  46. final_copy=final.copy()
  47. cv2.drawContours(final_copy, contours, -1, (0,255,0), 3)
  48. cv2.imshow('contours',final_copy)
  49. cv2.waitKey(0)
  50.  
  51.  
  52. def isContourAtBorder(contour,image):
  53.     x, y, w, h = cv2.boundingRect(contour)
  54.     xMin = 0
  55.     yMin = 0
  56.     xMax = image.shape[1]
  57.     yMax = image.shape[0]
  58.     if x <= xMin or y <= yMin or x+w >= xMax or y+h >= yMax:
  59.         return True
  60.     else:
  61.         return False
  62.  
  63.  
  64. #find the bordering contours
  65. badconindex = np.array([])
  66. i=0
  67. for c in contours:
  68.     if isContourAtBorder(c,final_transformed):
  69.         badconindex = np.append(badconindex,i)
  70.     i = i + 1
  71. contours = np.delete(contours, badconindex)
  72.  
  73.  
  74. print("Number of Cells in the image without those touching the borders " + str(len(contours)))
  75.  
  76. final_copy2=final.copy()
  77.  
  78. def countAreaContour(contour):
  79.     x, y, w, h = cv2.boundingRect(contour)
  80.     size=0
  81.     for y1 in range(y,y+h):
  82.         for x1 in range(x,x+w):
  83.             if cv2.pointPolygonTest(contour, (x1,y1),False)>= 0:
  84.                 if final_transformed[y1][x1]==255:
  85.                     size=size+1
  86.     return size
  87.  
  88. i=0
  89. for c in contours:
  90.     final_labeled = cv2.putText(final_copy2, str(i), cv2.boundingRect(contours[i])[:2], cv2.FONT_HERSHEY_COMPLEX, 1, [255])
  91.     area = cv2.contourArea(c)
  92.     print("Area of cell number (using ccntourArea) ",i," is", area)
  93.     print("Area of cell number (using my method) ",i," is", countAreaContour(c))
  94.  
  95.     i = i + 1
  96. #cv2.drawContours(final_labeled, contours, -1, (0,255,0), 3)
  97. cv2.imshow('contours',final_labeled)
  98. cv2.waitKey(0)
  99.  
  100.  
  101.  
  102. integral_img=cv2.integral(final,cv2.CV_64F)
  103. i=0
  104. for c in contours:
  105.     x, y, w, h = cv2.boundingRect(c)
  106.     sumOfPixels=integral_img[y][x] + integral_img[y+h+1][x+w+1] - integral_img[y][x+w+1] - integral_img[y+h+1][x]
  107.     meanGrayValue = sumOfPixels/(h*w)
  108.     print("Mean Gray Value of bounding box of  Cell number ",i, "is ", meanGrayValue)
  109.     i=i+1
  110. cv2.waitKey(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement