Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Given a JPG image of coloured balloons, find the yellow balloon and change it to black
- import cv2
- img = cv2.imread("Balloons.jpg")
- cv2.imshow("img", img) # Show the original image
- cv2.waitKey(0) # Wait for a key press
- yellow = (0, 255, 255) # Colour code of Yellow, in Blue, Green, Red sequence (as per CV2)
- for y, line in enumerate(img): # y is line counter
- for x, bgr in enumerate(line): # x is horizontal pixel counter
- # find a pixel that is yellowish by computing a numerical difference from yellow
- diffred = abs(yellow[2] - bgr[2])
- diffgrn = abs(yellow[1] - bgr[1])
- diffblu = abs(yellow[0] - bgr[0])
- diff = diffred + diffgrn + diffblu
- if diff < 180: # 180 was found empirically, to be a value that worked well
- img[y][x] = (0, 0, 0) # Set this pixel to black
- cv2.imshow("img", img) # Show the modified image
- cv2.waitKey(0) # Wait for a key press
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement