Syed313
Syed313

Reputation: 13

Finding only the red pixel in a image and darkening it to more red

I want to find only the red pixel in a image and darkening it to more red, currently i have this basic code, Is there any better way to get very good results, Please do suggest. Thank You

import cv2
import numpy as np

img = cv2.imread('2.jpg', 1)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

import matplotlib.pyplot as plt
plt.imshow(img)
plt.show()

img = cv2.addWeighted(img, 0.9, np.zeros(img.shape, img.dtype), 0, 0)

rows,cols, val = img.shape
for i in range(rows):
    for j in range(cols):
        k = img[i,j]
        #print(k)
        if(k[0] < 255):
            k[0] = 255
            print(k)

contrast_img = cv2.addWeighted(img, 0.8, np.zeros(img.shape, img.dtype), 0, 0)

plt.imshow(contrast_img)

Input Expected output

Upvotes: 1

Views: 539

Answers (1)

fmw42
fmw42

Reputation: 53081

Here is one way to increase the contrast of the red (and all colors) using Python/OpenCV/Skimage. Adjust the factor of min in the skimage.exposure function.

Input:

enter image description here

import cv2
import numpy as np
import skimage.exposure

# read image
img = cv2.imread('red_text.jpg')

# get min and max values
min = np.amin(img)
max = np.amax(img)
print("min=",min, "max=",max)

# increase contrast
result = skimage.exposure.rescale_intensity(img, in_range=(2*min,max), out_range=(0,255))

# save output image
cv2.imwrite('red_text_enhanced.jpg', result)

# display IN and OUT images
cv2.imshow('RESULT', result)
cv2.waitKey(0)
cv2.destroyAllWindows()

enter image description here

Upvotes: 1

Related Questions