How To Draw Concentric Circles In Java
Drawing over Images
In this, nosotros volition learn how to draw lines, rectangles, circles, etc. over images.
OpenCV provides easy to utilize functions for cartoon over an epitome. The most common drawing operations are given beneath:
- Line
- Circle
- Rectangle
- Text
Apart from these, there are other drawing operations similar polylines, fillPoly which are used in certain cases.
Creating a black canvas
Let's showtime off my making a blackness square image.
import cv2
import numpy every bit np # Create a black prototype
image = np.zeros((512,512,3), np.uint8) # Tin nosotros make this in black and white?
image_bw = np.zeros((512,512), np.uint8) cv2.imshow("Black Rectangle (Color)", image)
cv2.imshow("Black Rectangle (B&W)", image_bw) cv2.waitKey(0)
cv2.destroyAllWindows()
Output:
Python Functions for Drawing
Beneath nosotros have provided the arguments the function takes. Then we testify example code that draws different shapes on an paradigm.
cv2.line ( image, starting betoken , end indicate , color , line thickness, line type)
cv2.circle ( image, centre, radius, colour of edge, line thickness / fill type, line blazon)
cv2.ellipse ( image, eye, axes lengths, rotation caste of ellipse, starting angle , ending angle, colour, line thickness / fill type, line type)
cv2.rectangle ( prototype, upper left corner vertex, lower right corner vertex, line thickness / fill blazon, line type)
cv2.putText ( paradigm, text, starting point of text, font type, font scale, color, linetype )
The thickness of the boundary is controlled by the thickness parameter. By default, but an outline of the shape is drawn. To draw a shape that is filled with the specified colour, the thickness should be -1.
The lineType parameter controls the quality of rendering. When lineType is set to CV_AA, anti-aliased ( polish ) lines are fatigued. Type of line, whether 8-continued, anti-aliased line etc. By default, it is viii-continued. cv2.LINE_AA gives anti-aliased line which looks great for curves.
Describe lines
# Describe a diagonal blue line of thickness of five pixels
image = np.zeros((512,512,3), np.uint8) # black sail
cv2.line(prototype, (0,0), (511,511), (255,127,0), five)
cv2.imshow("Blue Line", epitome) cv2.waitKey(0)
cv2.destroyAllWindows()
# drawing a line cv2.line(imageLine, # image
(322, 179), # starting point
(400, 183), # terminate signal
(0, 255, 0), # color
thickness=two, # thickness
lineType=cv2.LINE_AA # line type
) cv2.imshow("imageLine", imageLine)
cv2.imwrite("imageLine.jpg", imageLine)
Draw a Rectangle
cv2.rectangle(image, starting vertex, opposite vertex, color, thickness)
# Describe a rectangle (thickness is a positive integer) cv2.rectangle(imageRectangle, # source epitome
(208, 55), # upper left corner vertex
(450, 355), # lower right corner vertex
(0, 255, 0), # color
thickness=2, # line thickness
lineType=cv2.LINE_8 # line blazon
) cv2.imshow("rectangle", imageRectangle)
cv2.imwrite("imageRectangle.jpg", imageRectangle)
To draw a shape that is filled with the specified colour, the thickness should exist -ane.
# Draw a Rectangle in
image = np.zeros((512,512,3), np.uint8) cv2.rectangle(paradigm, (100,100), (300,250), (127,50,127), -ane)
cv2.imshow("Rectangle", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Describe a Circle
cv2.cirlce(image, center, radius, color, make full)
cv2.circumvolve(imageCircle, # source image
(350, 200), # center
150, # radius
(0, 255, 0), # colour or border
thickness=two, # line thickness
lineType=cv2.LINE_AA # line type
) cv2.imshow("imageCircle", imageCircle)
cv2.imwrite("imageCircle.jpg", imageCircle)
image = np.zeros((512,512,iii), np.uint8) cv2.circle(paradigm, (350, 350), 100, (15,75,fifty), -i)
cv2.imshow("Circle", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Depict Concentric circles
# draw concentric white circles
# calculate the middle point of sheet
# generate circles using for loop canvas = np.zeros((300,300,three), dtype ="uint8")
white = (255,255,255)
(centerX, centerY) =
(sheet.shape[1]//2, sail.shape[0]//ii) # width, peak for r in range(0, 175, 25):
cv2.circle(canvas, (centerX, centerY), r, white) cv2.imshow("concentric circles", canvas)
cv2.waitKey(0)
cv2.destroyAllWindows()
Draw Random circles
# generate random radius, middle signal, color
# depict circles in for loop
canvass = np.zeros((300,300,3), dtype ="uint8") for i in range(0, 25):
radius = np.random.randint(5, high = 200)
color = np.random.randint(0, high = 256, size = (3,)).tolist()
pt = np.random.randint(0, high = 300, size = (2,))
cv2.circle(canvas, tuple(pt), radius, colour, -1) cv2.imshow("Sail", sheet)
cv2.waitKey(0)
cv2.destroyAllWindows()
Add Polygons
prototype = np.zeros((512,512,3), np.uint8) # Let's define four points
pts = np.array( [[ten,50], [400,fifty], [ninety,200], [fifty,500]], np.int32) # Let's now reshape our points in grade required by polylines
pts = pts.reshape((-one,1,2)) cv2.polylines(image, [pts], True, (0,0,255), iii)
cv2.imshow("Polygon", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Drawing Text over Image
cv2.putText(image, 'Text to Display', bottom left starting signal, Font, Font Size, Color, Thickness)
- FONT_HERSHEY_SIMPLEX, FONT_HERSHEY_PLAIN
- FONT_HERSHEY_DUPLEX,FONT_HERSHEY_COMPLEX
- FONT_HERSHEY_TRIPLEX, FONT_HERSHEY_COMPLEX_SMALL
- FONT_HERSHEY_SCRIPT_SIMPLEX
- FONT_HERSHEY_SCRIPT_COMPLEX
image = np.zeros((512,512,3), np.uint8) cv2.putText(image, 'Hello World!', (75,290), cv2.FONT_HERSHEY_COMPLEX, 2, (100,170,0), 3)
cv2.imshow("Hello World!", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Example Code
import cv2
import numpy as np prototype = cv2.imread('../information/images/mark.jpg') # Draw a line
imageLine = image.re-create()
cv2.line(imageLine, (322, 179), (400, 183), (0, 255, 0),
thickness=2, lineType=cv2.LINE_AA)
cv2.imshow("imageLine", imageLine)
cv2.imwrite("imageLine.jpg", imageLine) # Draw a circle
imageCircle = image.copy()
cv2.circle(imageCircle, (350, 200), 150, (0, 255, 0),
thickness=two, lineType=cv2.LINE_AA)
cv2.imshow("imageCircle", imageCircle)
cv2.imwrite("imageCircle.jpg", imageCircle) # Describe an ellipse
# IMP Note: Ellipse Centers and Centrality lengths must exist integers
imageEllipse = image.copy()
cv2.ellipse(imageEllipse, (360, 200), (100, 170), 45, 0, 360,
(255, 0, 0), thickness=2, lineType=cv2.LINE_AA)
cv2.ellipse(imageEllipse, (360, 200), (100, 170), 135, 0, 360,
(0, 0, 255), thickness=2, lineType=cv2.LINE_AA)
cv2.imshow("ellipse", imageEllipse)
cv2.imwrite("imageEllipse.jpg", imageEllipse) # Describe a rectangle (thickness is a positive integer)
imageRectangle = image.copy()
cv2.rectangle(imageRectangle, (208, 55), (450, 355), (0, 255, 0),
thickness=two, lineType=cv2.LINE_8)
cv2.imshow("rectangle", imageRectangle)
cv2.imwrite("imageRectangle.jpg", imageRectangle) # Put text into image
imageText = image.re-create()
cv2.putText(imageText, "Mark Zuckerberg", (205, 50),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
cv2.imshow("text", imageText)
cv2.imwrite("imageText.jpg", imageText) cv2.waitKey(0)
cv2.destroyAllWindows()
Output:
Source: https://medium.com/@rndayala/drawing-over-images-9789838ef558
Posted by: greenlyharsecy.blogspot.com
0 Response to "How To Draw Concentric Circles In Java"
Post a Comment