Reputation: 99
I am trying to create a greyscale filter that allows you to adjust the RGB percentages for all the pixels in an image, for example (Red=20%, Green=70%, Blue=10%). I would prefer to do this is base level, so minimal amount of conversions, just the use of formulas to allow for this to happen.
I would assume the code would look a lot like this:
import numpy as np
import cv2
img=cv2.imread(filepath)
imgBlue=img[:,:,0]
imgGreen=[:,:,1]
imgRed=[:,:,2]
blueCon=imgBlue*.1
greenCon=imgGreen*.7
redCon=imgRed*.2
greyscaledImg=img[:,:,:]
greyscaledImg[:,:,0]=blueCon
greyscaledImg[:,:,1]=greenCon
greyscaledImg[:,:,2]=redCon
cv2.imwrite(filepath,greyscaledImg)
Upvotes: 0
Views: 1087
Reputation: 18925
That might be a nice example for using OpenCV's trackbars, see this example.
But, I think the proposed concept of a grayscale filter won't work that way, since you're only linear scaling the RGB values for the whole image. For getting gray tones, you'd need to manipulate all of the pixels in your image individually.
Nevertheless, here's your concept built into the above example:
import cv2
# Setup maximum factors for each channel
r_max = 100
g_max = 100
b_max = 100
# Window title
title_window = 'Custom gray scale'
# Event for R trackbar change
def on_r_trackbar(val):
dst[:, :, 2] = val / r_max * src[:, :, 2]
cv2.imshow(title_window, dst)
# Event for G trackbar change
def on_g_trackbar(val):
dst[:, :, 1] = val / g_max * src[:, :, 1]
cv2.imshow(title_window, dst)
# Event for B trackbar change
def on_b_trackbar(val):
dst[:, :, 0] = val / b_max * src[:, :, 0]
cv2.imshow(title_window, dst)
# Read some input image
src = cv2.imread('path/to/your/image.png')
dst = src.copy()
# Setup window and create trackbars for each channel
cv2.namedWindow(title_window)
cv2.createTrackbar('R', title_window, 100, r_max, on_r_trackbar)
cv2.createTrackbar('G', title_window, 100, g_max, on_g_trackbar)
cv2.createTrackbar('B', title_window, 100, b_max, on_b_trackbar)
# Show something, and wait until user presses some key
cv2.imshow(title_window, dst)
cv2.waitKey()
That's what the window looks like initially:
And, setting up your values, gives:
So, for getting some gray-ish appearance, I'd stick to the HSV color space, and simply modify the saturation channel:
import cv2
# Setup maximum factor for the saturation
s_max = 100
# Window title
title_window = 'Custom gray scale'
# Event for S (saturation) trackbar change
def on_s_trackbar(val):
dst[:, :, 1] = val / s_max * hsv[:, :, 1]
cv2.imshow(title_window, cv2.cvtColor(dst, cv2.COLOR_HSV2BGR))
# Read some input image, and convert to HSV color space
src = cv2.imread('path/to/your/image.png')
hsv = cv2.cvtColor(src, cv2.COLOR_BGR2HSV)
dst = hsv.copy()
# Setup window and create trackbar for the S (saturation) channel
cv2.namedWindow(title_window)
cv2.createTrackbar('S', title_window, 100, s_max, on_s_trackbar)
# Show something, and wait until user presses some key
cv2.imshow(title_window, cv2.cvtColor(dst, cv2.COLOR_HSV2BGR))
cv2.waitKey()
Again, the initial window, there's only one trackbar left for the saturation:
And, some adjusting of the saturation gives:
----------------------------------------
System information
----------------------------------------
Platform: Windows-10-10.0.16299-SP0
Python: 3.8.5
OpenCV: 4.4.0
----------------------------------------
Upvotes: 2
Reputation: 1113
Does This Snippet Answer Your Question?
import cv2
# Reading color image as grayscale
gray = cv2.imread("image.jpeg", 0) # PLEASE MAKE SURE TO REPLACE WITH YOUR OWN IMAGE! AND the '0' is the code for the grey scaled image. if it is '1' then it will be back to normal i it's orginal color
# Showing grayscale image
cv2.imshow("Grayscale Image", gray)
# waiting for key event
cv2.waitKey(0)
# destroying all windows
cv2.destroyAllWindows()
Upvotes: 0