Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3.  
  4. # Colors (B, G, R)
  5. WHITE = (255, 255, 255)
  6. BLACK = (0, 0, 0)
  7.  
  8.  
  9. def create_blank(width, height, color=(0, 0, 0)):
  10. """Create new image(numpy array) filled with certain color in BGR"""
  11. image = np.zeros((height, width, 3), np.uint8)
  12. # Fill image with color
  13. image[:] = color
  14.  
  15. return image
  16.  
  17.  
  18. def draw_half_circle_rounded(image):
  19. height, width = image.shape[0:2]
  20. # Ellipse parameters
  21. radius = 100
  22. center = (width / 2, height - 25)
  23. axes = (radius, radius)
  24. angle = 0
  25. startAngle = 180
  26. endAngle = 360
  27. thickness = 10
  28.  
  29. # http://docs.opencv.org/modules/core/doc/drawing_functions.html#ellipse
  30. cv2.ellipse(image, center, axes, angle, startAngle, endAngle, BLACK, thickness)
  31.  
  32.  
  33. # Create new blank 300x150 white image
  34. width, height = 300, 150
  35. image = create_blank(width, height, color=WHITE)
  36. draw_half_circle_rounded(image)
  37. cv2.imwrite('half_circle_rounded.jpg', image)
  38.  
  39. import cv2
  40. import numpy as np
  41.  
  42. # Colors (B, G, R)
  43. WHITE = (255, 255, 255)
  44. BLACK = (0, 0, 0)
  45.  
  46.  
  47. def create_blank(width, height, color=(0, 0, 0)):
  48. """Create new image(numpy array) filled with certain color in BGR"""
  49. image = np.zeros((height, width, 3), np.uint8)
  50. # Fill image with color
  51. image[:] = color
  52.  
  53. return image
  54.  
  55.  
  56. def draw_half_circle_no_round(image):
  57. height, width = image.shape[0:2]
  58. # Ellipse parameters
  59. radius = 100
  60. center = (width / 2, height - 25)
  61. axes = (radius, radius)
  62. angle = 0
  63. startAngle = 180
  64. endAngle = 360
  65. # When thickness == -1 -> Fill shape
  66. thickness = -1
  67.  
  68. # Draw black half circle
  69. cv2.ellipse(image, center, axes, angle, startAngle, endAngle, BLACK, thickness)
  70.  
  71. axes = (radius - 10, radius - 10)
  72. # Draw a bit smaller white half circle
  73. cv2.ellipse(image, center, axes, angle, startAngle, endAngle, WHITE, thickness)
  74.  
  75.  
  76. # Create new blank 300x150 white image
  77. width, height = 300, 150
  78.  
  79. image = create_blank(width, height, color=WHITE)
  80. draw_half_circle_no_round(image)
  81. cv2.imwrite('half_circle_no_round.jpg', image)
  82.  
  83. int main()
  84. {
  85. cv::Mat outImage = cv::Mat(256,256,CV_8UC3, cv::Scalar(255,255,255));
  86.  
  87. cv::Point center(128,128);
  88. int radius = 100;
  89.  
  90. //draw upright black halfcircle
  91. cv::Scalar colorBlack = cv::Scalar(0,0,0);
  92. double startAngleUpright = 180;
  93. cv::ellipse(outImage,center,cv::Size(radius,radius),0,startAngleUpright,startAngleUpright+180,colorBlack,8,0);
  94.  
  95. //draw downright red halfcircle
  96. cv::Scalar colorRed = cv::Scalar(0,0,255);
  97. double startAngleDownright = 0;
  98. cv::ellipse(outImage,center,cv::Size(radius,radius),0,startAngleDownright,startAngleDownright+180,colorRed,8,0);
  99.  
  100. cv::imshow("outImage", outImage);
  101. cv::imwrite("DrawHalfCircle.png", outImage);
  102.  
  103. cv::waitKey(-1);
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement