Guest User

Untitled

a guest
Dec 13th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.33 KB | None | 0 0
  1. RNG rng(12345);
  2. int main() {
  3. sorting_contours();
  4. return 0;}
  5. void sorting_contours() {
  6. Mat image = imread("7.jpg");
  7. Mat orig = image.clone();
  8. Mat gray;
  9. cvtColor(image, gray, CV_BGR2GRAY);
  10. threshold(gray, gray, 229, 255, CV_THRESH_BINARY);
  11. gray = 255 - gray;
  12. Mat element = getStructuringElement(MORPH_ELLIPSE, Size(3, 3), Point(2, 2));
  13. morphologyEx(gray, gray, MORPH_CLOSE, element);
  14. Mat edged = imutils::auto_canny(gray);
  15. vector<Vec4i> hierarchy;
  16. vector<vector<Point>> contours;
  17. findContours(edged, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
  18. Mat drawing = image.clone();
  19.  
  20. vector<Rect> boundRect;
  21. contours = imutils::sort_contours(contours, boundRect, imutils::SortContoursMethods::left_to_right);
  22. int pixelsPerMetric;
  23. Point2f tl, tr, br, bl, tltrX, tltrY, blbrX, blbrY;
  24. vector<RotatedRect> minRect(contours.size());
  25. for (int i = 0; i < contours.size(); i++)
  26. {
  27. if (contours[i].size() > 100)
  28. {
  29. minRect[i] = minAreaRect(Mat(contours[i]));
  30. Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
  31.  
  32. Point2f rect_points[4]; minRect[i].points(rect_points);
  33. for (int j = 0; j < 4; j++)
  34. line(drawing, rect_points[j], rect_points[(j + 1) % 4], color, 1, 8);
  35. }
  36. }
  37. imshow("left_to_right", drawing);
  38. waitKey(0);}
  39.  
  40. boxPoints(minRect[i], pointss);
  41. pointss = imutils::order_points(pointss);
  42. //what i should do next?
  43.  
  44. import imageio
  45. import numpy
  46. import scipy
  47. import skimage
  48.  
  49. import skimage.morphology
  50. import skimage.segmentation
  51. import skimage.io
  52.  
  53. image = imageio.imread(r'C:UsersJeremiahPicturesR9j7GPe.jpg')
  54. image_array = numpy.float64(image)
  55.  
  56. #This mess of code here is for a sobel filter. I always hard-code it because I don't
  57. #like the way that any of the pre-built functions that I have tried do the filter for
  58. #color images. If you want to use a stock filter instead, replace everything between
  59. #here and the next comment with that function. You will probably need to convert the
  60. #image to grayscale if you do that though.
  61.  
  62. R_x = scipy.ndimage.filters.correlate(image_array[:, :, 0], [[1, 2, 1], [0, 0, 0], [-1, -2, -1]])
  63. G_x = scipy.ndimage.filters.correlate(image_array[:, :, 1], [[1, 2, 1], [0, 0, 0], [-1, -2, -1]])
  64. B_x = scipy.ndimage.filters.correlate(image_array[:, :, 2], [[1, 2, 1], [0, 0, 0], [-1, -2, -1]])
  65.  
  66. R_y = scipy.ndimage.filters.correlate(image_array[:, :, 0], [[1, 0 , -1], [2, 0, -2], [1, 0, -1]])
  67. G_y = scipy.ndimage.filters.correlate(image_array[:, :, 1], [[1, 0 , -1], [2, 0, -2], [1, 0, -1]])
  68. B_y = scipy.ndimage.filters.correlate(image_array[:, :, 2], [[1, 0 , -1], [2, 0, -2], [1, 0, -1]])
  69.  
  70. Jacobian_x = R_x**2 + G_x**2 + B_x**2
  71. Jacobian_y = R_y**2 + G_y**2 + B_y**2
  72. Jacobian_xy = R_x * R_y + G_x * G_y + B_x * B_y
  73. Determinant = numpy.sqrt(numpy.fabs((Jacobian_x**2) - (2 * Jacobian_x * Jacobian_y) + (Jacobian_y**2) + 4 * (Jacobian_xy**2)))
  74. Maximum_Eigenvalue = (Jacobian_x + Jacobian_y + Determinant) / 2
  75. Edges = numpy.sqrt(Maximum_Eigenvalue)
  76.  
  77. #Next, you convert the image to binary and dilate it to close any holes.
  78. Threshold = skimage.filters.threshold_mean(Edges)
  79. Binary_Image = Edges > Threshold
  80. Dilated_Binary_Image = scipy.ndimage.morphology.binary_dilation(Binary_Image)
  81.  
  82. #Then clear the noise on the border, and fill in all objects. After the fill is
  83. #done, an erosion step is necessary to restore all objects to their original
  84. #dimensions. Once this is complete, you can extract the edges using thresholding.
  85.  
  86. Cleared_Borders = skimage.segmentation.clear_border(Dilated_Binary_Image, buffer_size=10)
  87. Filled_Holes = scipy.ndimage.morphology.binary_fill_holes(Cleared_Borders)
  88. Restored_Height = scipy.ndimage.morphology.binary_erosion(Filled_Holes)
  89. selem = skimage.morphology.disk(1)
  90. Object_Borders = (skimage.filters.rank.minimum(Restored_Height, selem) == 0) & (skimage.filters.rank.maximum(Restored_Height, selem) == 255)
  91.  
  92. #Notice that, in the image above, the rectangles are isolated, but, if you look back
  93. #at the dilated image, you will see that the people make closed objects in themselves
  94. #and so the rectangles aren't actually necessary in order to extract the highest and
  95. #lowest points on the individuals. The final step is to label our objects. Labeled
  96. #Edges is a slice containing each individual object. Features is the number of
  97. #objects in the image.
  98.  
  99. Labeled_Edges, features = scipy.ndimage.label(Object_Borders)
  100.  
  101. #So now we know where our objects are. If you want to keep using the rectangles, you
  102. #can basically start here. The key is to extract the x and y coordinates of your
  103. #object borders, rather than their indices. That way, you can just subtract your
  104. #max and min y values. In this case, this is actually a tiny bit off. This is
  105. #because the stars on the edges of your rectangles take up pixels. If the rectangles
  106. #weren't there, and the people themselves were the objects, this would go from the
  107. #top of their head to the bottom of their shoes every time.
  108.  
  109. Heights = []
  110. for objects in range(features):
  111. sliced = scipy.ndimage.find_objects(Labeled_Edges)[objects] #Get slice containing each object.
  112. slices = Labeled_Edges[sliced] #Get the actual pixels contained in the slice.
  113. [y, x] = numpy.where(slices > 0) #finds the x and y coordinates of the object edges.
  114. Height = max(y) - min(y)
  115. if Height > 10: #Clears out noise by ignoring objects too small to be people.
  116. Heights.append(Height)
  117.  
  118. print(Heights)
  119. >>>[289, 287, 285, 273]
Add Comment
Please, Sign In to add comment