Jesper
Jesper

Reputation: 20

How to replace color for colored objects in image?

I am trying to detect edges in images of a video, but edge detection methods such as canny does not work very well might be due to in similarity between boxes's color and floor color or brightness so I want to find a way to make all red and blue boxes look as white as possible, or may be the best way to detect edges as perfect as possible for every frame since that is the ultimate goal.

enter image description here

Upvotes: 0

Views: 468

Answers (2)

api55
api55

Reputation: 11420

Just to complete my comment in your question. One can use HSV/HLS colorspaces and use inRanges with the Hue channel. For example:

import numpy as np
import cv2

# load image and threshold it
original = cv2.imread("a.jpg")
hsvframe = cv2.cvtColor(original, cv2.COLOR_BGR2HLS)
mask = cv2.inRange(hsvframe, (160,40,40), (180, 255, 255))
mask = mask +  cv2.inRange(hsvframe, (0,40,40), (12, 255, 255)) # color red is at the beginning and end of the hue wheel

original[mask==255] = (0,255,0)
cv2.imshow("image", original)

cv2.waitKey(0)
cv2.destroyAllWindows()

Things to remember, Hue goes from 0-180 in np.uint8. This means if you need hue 300-360 the limits will be 150-180. The other two values are 0-255 where 255 = 100%.

The result of this small code is:

enter image description here

It is not perfect, but one can refine it using the methods suggested by the other answer. I hope this helps.

Upvotes: 1

Albert H M
Albert H M

Reputation: 266

I recommend you using color tracking then.

  1. Convert to HSV

cv2.bgr2hsv

Why hsv? eventhough the brightness change, u can still detect that color

  1. Filtering

You can use cv2.inrange

  1. Noise cancelling

Use cv2.Gaussianblur

  1. Contouring

use cv2.findContours

  1. Find the edge use ur method

Repeat this step for every color of your box

Hope this help

Upvotes: 1

Related Questions