简体   繁体   中英

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.

在此处输入图片说明

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

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:

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM