How to Draw a point in an image using given co-ordinate with python opencv?

后端 未结 2 1020
难免孤独
难免孤独 2020-12-31 03:03

I have one image and one co-ordinate (X, Y). How to draw a point with this co-ordinate on the image. I want to use Python OpenCV.

相关标签:
2条回答
  • 2020-12-31 03:22

    I'm learning the Python bindings to OpenCV too. Here's one way:

    #!/usr/local/bin/python3
    import cv2
    import numpy as np
    
    w=40
    h=20
    # Make empty black image
    image=np.zeros((h,w,3),np.uint8)
    
    # Fill left half with yellow
    image[:,0:int(w/2)]=(0,255,255)
    
    # Fill right half with blue
    image[:,int(w/2):w]=(255,0,0)
    
    # Create a named colour
    red = [0,0,255]
    
    # Change one pixel
    image[10,5]=red
    
    # Save
    cv2.imwrite("result.png",image)
    

    Here's the result - enlarged so you can see it.


    Here's the very concise, but less fun, answer:

    #!/usr/local/bin/python3
    import cv2
    import numpy as np
    
    # Make empty black image
    image=np.zeros((20,40,3),np.uint8)
    
    # Make one pixel red
    image[10,5]=[0,0,255]
    
    # Save
    cv2.imwrite("result.png",image)
    

    0 讨论(0)
  • 2020-12-31 03:37

    You can use cv2.circle() function opencv module:

    image = cv.circle(image, centerOfCircle, radius, color, thickness)
    

    Keep radius as 0 for plotting a single point and thickness as a negative number for filled circle

    import cv2
    image = cv2.circle(image, (x,y), radius=0, color=(0, 0, 255), thickness=-1)
    
    0 讨论(0)
提交回复
热议问题