0
votes

I'm a beginner working on an Opencv project for the very first time. I wrote the program below to convert white pixels in a RGB image to black ones but the error as shown in the title appeared. It would be great if you could explain to me on what has went wrong and how I can make it work. Any help is appreciated.

import cv2
import numpy as np

image = cv2.imread("MAP.png")
print ("Your image has been opened.")

cv2.imshow("Image", image)

x,y = image [0:500,0:500]

print (image[297,365])

e = image[:,:,0]
r = image[:,:,1]
t = image[:,:,2]


image = [e,r,t]

for i in range (x,y):
    if [e,r,t] == [255,255,255]:
        [e,r,t] = [0,0,0]

print (image[297,365])

cv2.waitKey(0)
cv2.destroyAllWindows()


1
You need to include the image and the full TraceBack. - Trenton McKinney
I bet it's on x,y = image[0:500,0:500]... not really sure what the author had in mind when they wrote that, doesn't really make sense. - Dan MaĊĦek
always put full error message (starting at word "Traceback") in question (not comment) as text (not screenshot). There are other useful information. - furas

1 Answers

0
votes

Code image[0:500,0:500] gives you single array (part of image), not two values which you could assign to two variables x,y.

But because cv uses numpy array then you can change pixels in this part of image without using x,y and for-loop

part_of_image = image[0:500,0:500]
part_of_image[ np.all(part_of_image == [255,255,255]) ] = [0,0,0]

BTW: you have to remember that cv uses colors in order B,G,R instead of R,G,B.


import cv2
import numpy as np

image = cv2.imread("MAP.png")

part_of_image = image[0:500,0:500]
part_of_image[ np.all(part_of_image == [255,255,255]) ] = [0,0,0]

cv2.imshow("Image", image)

cv2.waitKey(0)
cv2.destroyAllWindows()