OpenCV: Set brightness of each RGB pixel so brightest channel (R, G, or B) is at maximum

Viewed 217

For each pixel in an RGB image, I want to increase its brightness so that the strongest channel of that pixel (R, G, or B) is maximized. For example, if a pixel has an RGB value of (128, 64, 32), and the maximum channel value is 255, then that pixel should be changed to approximately (255, 128, 64). This is sort of a poor man's shadow removal system.

I can do this by iterating the pixels explicitly, but that is very inefficient. What is the most efficient way to do this by using strictly OpenCV methods? It seems it might use YUV space?

(By the way, I am using C# with EmguCV, but a straight Python/OpenCV answer would be fine. EDIT: But I can't use Python libraries)

1 Answers

In Python, OpenCV images are just numpy array. So here's a python/numpy approach:

# toy sample
np.random.seed(1)
a = np.random.randint(0, 100, (4,4,3), dtype=np.uint8)

# get max values across channels, scaled by 255
maxx = np.max(a, axis=-1)/255

# scale a by maxx
a = (a / maxx[:,:, None]).astype(np.uint8)

Input (with plt.imshow() so in rgb):

enter image description here

Output:

enter image description here

Related