draw a circle over image opencv
Asked Answered
R

4

26

Im usign python and opencv to get a image from the webcam, and I want to know how to draw a circle over my image, just a simple green circle with transparent fill

enter image description here

my code:

import cv2
import numpy
import sys

if __name__ == '__main__':


    #get current frame from webcam
    cam = cv2.VideoCapture(0)
    img = cam.read()

    #how draw a circle????

    cv2.imshow('WebCam', img)

    cv2.waitKey()

Thanks in advance.

Ruffled answered 10/5, 2013 at 14:38 Comment(2)
cv2.circle draws a circle over my image, but I want just the border of the circle, any ideas?Ruffled
docs.opencv.org/2.4/modules/core/doc/drawing_functions.html Keep this handy while using drawing functions in opencvPineda
I
40
cv2.circle(img, center, radius, color, thickness=1, lineType=8, shift=0) → None
Draws a circle.

Parameters: 
img (CvArr) – Image where the circle is drawn
center (CvPoint) – Center of the circle
radius (int) – Radius of the circle
color (CvScalar) – Circle color
thickness (int) – Thickness of the circle outline if positive, otherwise this indicates that a filled circle is to be drawn
lineType (int) – Type of the circle boundary, see Line description
shift (int) – Number of fractional bits in the center coordinates and radius value

Use "thickness" parameter for only the border.

Importance answered 10/5, 2013 at 15:14 Comment(0)
C
32

Just an additional information:

The parameter "center" of OpenCV's drawing function cv2.circle() takes a tuple of two integers. The first is the width location and the second is the height location. This ordering is different from the usual array indexing. The following example demonstrates the issue.

import numpy as np
import cv2

height, width = 150, 200
img = np.zeros((height, width, 3), np.uint8)
img[:, :] = [255, 255, 255]

# Pixel position to draw at
row, col = 20, 100

# Draw a square with position 20, 100 as the top left corner
for i in range(row, 30):
    for j in range(col, 110):
        img[i, j] = [0, 0, 255]

# Will the following draw a circle at (20, 100)?
# Ans: No. It will draw at row index 100 and column index 20.
cv2.circle(img,(col, row), 5, (0,255,0), -1)

cv2.imwrite("square_circle_opencv.jpg", img)

python opencv cv2.circle center indexing issue

Celtuce answered 21/5, 2017 at 16:25 Comment(0)
R
4

try

cv2.circle(img, center, radius, color[, thickness[, lineType[, shift]]])

See the documentation for more details

Rosariorosarium answered 10/5, 2013 at 16:35 Comment(2)
this is also helpful aishack.in/2010/07/transparent-image-overlays-in-opencv it's histogram overlay but you can fork itRosariorosarium
i also found this sample code bistr-o-mathik.org/2012/06/13/simple-transparency-in-opencvRosariorosarium
P
1

To dynamically draw a circle with OpenCV,

import numpy as np
import cv2
import math
drawing = False # true if mouse is pressed
ix,iy = -1,-1

# Create a function based on a CV2 Event (Left button click)
def draw_circle(event,x,y,flags,param):
    global ix,iy,drawing
    
    if event == cv2.EVENT_LBUTTONDOWN:
        drawing = True
        # we take note of where that mouse was located
        ix,iy = x,y
        
    elif event == cv2.EVENT_MOUSEMOVE:
        drawing == True
        
    elif event == cv2.EVENT_LBUTTONUP:
        radius = int(math.sqrt( ((ix-x)**2)+((iy-y)**2)))
        cv2.circle(img,(ix,iy),radius,(0,0,255), thickness=1)
        drawing = False

# Create a black image
img = np.zeros((512,512,3), np.uint8)

# This names the window so we can reference it
cv2.namedWindow('image')

# Connects the mouse button to our callback function
cv2.setMouseCallback('image',draw_circle)

while(1):
    cv2.imshow('image',img)

    # EXPLANATION FOR THIS LINE OF CODE:
    # https://mcmap.net/q/209423/-what-39-s-0xff-for-in-cv2-waitkey-1/39201163
    k = cv2.waitKey(1) & 0xFF
    if k == 27:
        break
# Once script is done, its usually good practice to call this line
# It closes all windows (just in case you have multiple windows called)
cv2.destroyAllWindows()
Pastore answered 26/5, 2021 at 15:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.