How to draw a point in an image using given coordinate

user11312666 picture user11312666 · Apr 6, 2019 · Viewed 10.8k times · Source

I loaded the image and tried to draw a red point in an image

img=mpimg.imread('./images/im00001.jpg')
red = [0,0,255]
# Change one pixel
img[ 0.,-26.10911452,0. ]=red
imgplot = plt.imshow(img)

but the following error occurred

ValueError: assignment destination is read-only

Answer

nathancy picture nathancy · Apr 6, 2019

You're on the right track. You can change a pixel's property using Numpy splicing

img[x,y] = [B,G,R]

So for example, to change a pixel at (50,50) to red, you can do

img[50,50] = [0,0,255]

Here we change a single pixel to red (it's pretty tiny)

enter image description here

import cv2
import numpy as np

width = 100
height = 100

# Make empty black image of size (100,100)
img = np.zeros((height, width, 3), np.uint8)

red = [0,0,255]

# Change pixel (50,50) to red
img[50,50] = red

cv2.imshow('img', img)
cv2.waitKey(0)

An alternative method is to use cv2.circle() to draw your point inplace.

The function header is

cv2.circle(image, (x, y), radius, (B,G,R), thickness)

Using this, we obtain the same result

cv2.circle(img, (50,50), 1, red, -1)

enter image description here