Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. import argparse
  2. import sys
  3. import os.path
  4. import trans #pip install trans
  5. import time
  6. import cv2
  7. import math
  8. import skimage
  9.  
  10. import numpy as np
  11.  
  12. from skimage.morphology import skeletonize
  13. from skimage import util
  14.  
  15. ap = argparse.ArgumentParser()
  16. ap.add_argument("-i", "--image", required = True,
  17. help = "Path to the image")
  18. args = vars(ap.parse_args())
  19.  
  20. img = cv2.imread(args["image"])
  21.  
  22.  
  23. # create gray image for further processing
  24. #gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  25. #cv2.imwrite("gray.png", gray_img)
  26.  
  27. # create a CLAHE object (Arguments are optional).
  28. clahe = cv2.createCLAHE(clipLimit=3.25, tileGridSize=(4,4))
  29. # apply CLAHE histogram expansion to find squares better with canny edge detection
  30. #gray_img = clahe.apply(gray_img)
  31. #cv2.imwrite("CLAHE.png", gray_img)
  32.  
  33. #gray_blur = cv2.GaussianBlur(gray_img, (15, 15), 0)
  34. #thresh = cv2.adaptiveThreshold(gray_blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 15, 1)
  35. #cv2.imwrite("adaptthresh.png", thresh)
  36.  
  37. #kernel = np.ones((3, 3), np.uint8)
  38. #closing = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=4)
  39. #cv2.imwrite("close.png", closing)
  40.  
  41. #pyrMSF = cv2.pyrMeanShiftFiltering(img, 30, 10)
  42. #cv2.imwrite("pyrMSF.png", pyrMSF)
  43. #edges = cv2.Canny(img, 100, 200)
  44. #cv2.imwrite("coloredges.png", edges)
  45.  
  46.  
  47. #edges = cv2.Canny(gray_img, 100, 200)
  48. #cv2.imwrite("grayedges.png", edges)
  49.  
  50.  
  51. #-----Converting image to LAB Color model-----------------------------------
  52. lab= cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
  53.  
  54. #-----Splitting the LAB image to different channels-------------------------
  55. l, a, b = cv2.split(lab)
  56.  
  57. #-----Applying CLAHE to L-channel-------------------------------------------
  58. cl = clahe.apply(l)
  59.  
  60. #-----Merge the CLAHE enhanced L-channel with the a and b channel-----------
  61. limg = cv2.merge((cl,a,b))
  62.  
  63. #-----Converting image from LAB Color model to RGB model--------------------
  64. final = cv2.cvtColor(limg, cv2.COLOR_LAB2BGR)
  65. cv2.imwrite("1colorCLAHE.png", final)
  66. #_____END_____#
  67.  
  68. blur = cv2.GaussianBlur(final,(5,5),0)
  69. cv2.imwrite("2colorCLAHEgauss.png", blur)
  70.  
  71. meanshift = cv2.pyrMeanShiftFiltering(blur, sp=75, sr=30, maxLevel=1, termcrit=(cv2.TERM_CRITERIA_EPS+cv2.TERM_CRITERIA_MAX_ITER, 5, 1))
  72. cv2.imwrite("3meanshiftCLAHE.png", meanshift)
  73.  
  74. edgeCLAHE = cv2.Canny(meanshift, 100, 200)
  75. cv2.imwrite("4edgeCLAHE.png", edgeCLAHE)
  76.  
  77. #colCLAHEpyrMSF = cv2.pyrMeanShiftFiltering(final, 30, 10)
  78. #cv2.imwrite("colorCLAHEpyrMSF.png", colCLAHEpyrMSF)
  79.  
  80.  
  81. #img_float = np.float32(final) # Convert image from unsigned 8 bit to 32 bit float
  82. #criteria = (cv2.TERM_CRITERIA_EPS+cv2.TERM_CRITERIA_MAX_ITER, 10, 1)
  83. # Defining the criteria ( type, max_iter, epsilon )
  84. # cv2.TERM_CRITERIA_EPS - stop the algorithm iteration if specified accuracy, epsilon, is reached.
  85. # cv2.TERM_CRITERIA_MAX_ITER - stop the algorithm after the specified number of iterations, max_iter.
  86. # cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER - stop the iteration when any of the above condition is met.
  87. # max_iter - An integer specifying maximum number of iterations.In this case it is 10
  88. # epsilon - Required accuracy.In this case it is 1
  89. #k = 15 # Number of clusters
  90. #ret, label, centers = cv2.kmeans(img_float, k, None, criteria, 50, cv2.KMEANS_RANDOM_CENTERS)
  91. # apply kmeans algorithm with random centers approach
  92. #center = np.uint8(centers)
  93. # Convert the image from float to unsigned integer
  94. #res = center[label.flatten()]
  95. # This will flatten the label
  96. # res2 = res.reshape(img.shape)
  97. # # Reshape the image
  98. # cv2.imwrite("1.jpg", res2) # Write image onto disk
  99. # #meanshift = cv2.pyrMeanShiftFiltering(img, sp=8, sr=16, maxLevel=1, termcrit=(cv2.TERM_CRITERIA_EPS+cv2.TERM_CRITERIA_MAX_ITER, 5, 1))
  100. # #cv2.imwrite("2.jpg", meanshift)
  101. #
  102. #
  103. # # Write image onto disk
  104. # gray = cv2.cvtColor(final, cv2.COLOR_BGR2GRAY)
  105. # # Convert image from RGB to GRAY
  106. # ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
  107. # # apply thresholding to convert the image to binary
  108. # fg = cv2.erode(thresh, None, iterations=1)
  109. # # erode the image
  110. # bgt = cv2.dilate(thresh, None, iterations=1)
  111. # # Dilate the image
  112. # ret, bg = cv2.threshold(bgt, 1, 128, 1)
  113. # # Apply thresholding
  114. # marker = cv2.add(fg, bg)
  115. # # Add foreground and background
  116. # canny = cv2.Canny(marker, 110, 150)
  117. # # Apply canny edge detector
  118. # new, contours, hierarchy = cv2.findContours(canny, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  119. # # Finding the contors in the image using chain approximation
  120. # marker32 = np.int32(marker)
  121. # # converting the marker to float 32 bit
  122. # cv2.watershed(final, marker32)
  123. # # Apply watershed algorithm
  124. # m = cv2.convertScaleAbs(marker32)
  125. # ret, thresh = cv2.threshold(m, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
  126. # # Apply thresholding on the image to convert to binary image
  127. # thresh_inv = cv2.bitwise_not(thresh)
  128. # # Invert the thresh
  129. # res = cv2.bitwise_and(final, final, mask=thresh)
  130. # # Bitwise and with the image mask thresh
  131. # res3 = cv2.bitwise_and(final, final, mask=thresh_inv)
  132. # # Bitwise and the image with mask as threshold invert
  133. # res4 = cv2.addWeighted(res, 1, res3, 1, 0)
  134. # # Take the weighted average
  135. # final = cv2.drawContours(res4, contours, -1, (0, 255, 0), 1)
  136. # # Draw the contours on the image with green color and pixel width is 1
  137. # cv2.imwrite("3.jpg", final) # Write the image
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement