5

I need to color a pixel in an image. I use opencv and python.
I tried img[x,y]=[255 255 255] to color a pixel(x,y) but it wont work :(

Is there is any mistake in this?
Can you suggest any method?

Thanks in advance.

d6bels
  • 1,432
  • 2
  • 18
  • 30
Prasika
  • 57
  • 1
  • 1
  • 7

3 Answers3

6

img[x,y]=[255, 255, 255] is wrong because opencv img[a,b] is a matrics then you need to change x,y then you must use img[y,x]

actualy mistake in the order of x,y if you want to change color of point x,y use this >> img[y,x] = color

slfan
  • 8,950
  • 115
  • 65
  • 78
dinuka saminda
  • 181
  • 3
  • 6
4

Try it with comma's between the 255's:

img[x,y]=[255, 255, 255]
Sander
  • 591
  • 6
  • 16
0

This works for me, just change it to load your own image:

import cv2

img = cv2.imread("C:\calibrate\chess\color001.jpg", cv2.CV_LOAD_IMAGE_COLOR);

## Make pixels row and column 300-400 black
img[300:400,300:400] = (0,0,0)

cv2.imshow('title',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Deepfreeze
  • 1,755
  • 1
  • 13
  • 18
  • the code is (x,y),radius = cv2.minEnclosingCircle(cnt) . here (x,y) is a center point.I want to color the center point .so I use img[x,y] .Is this wrg? how can I represent the center point? – Prasika Mar 11 '15 at 15:35
  • First try if my code is working for you to color pixels. Than you can say that this has answered you question. The other question -> print out the x,y coordinates and check if they are what you would expect. Perhaps 1 pxl is to small to see. – Deepfreeze Mar 12 '15 at 08:41
  • Ur code is wrking to color a block of pixels.I print x,y .The o/p is(97,417).It is x,y coordiante of a pixel.Am i ryt? I need to color a point img[97,417]=(0,0,0). For tat It is not wrking :( – Prasika Mar 12 '15 at 08:51
  • You probably need to use img[417,97]=(0,0,0) – Deepfreeze Mar 12 '15 at 09:06
  • It shows the following error msg : " img[417,97]=(0,0,0) ValueError: setting an array element with a sequence. " – Prasika Mar 12 '15 at 09:12
  • This is your 3th question in a single stackoverflow question. If you get such an error msg, just search with google and try to understand why it is giving this error. If you can't find the answer, post a new question with some code, such that we don't need to guess what's causing this error. – Deepfreeze Mar 12 '15 at 09:26