3
\$\begingroup\$

For academic purposes I want to reinvent Blue Green Red to Grayscale function in Python. I am new to Python so I believe my code below can still be optimized.

import cv2
import numpy as np


data = np.array([[[255, 0, 0], [0, 255, 0], [0, 0, 255]], [
    [0, 0, 0], [128, 128, 128], [255, 255, 255], ]], dtype=np.uint8)

rows = len(data)
cols = len(data[0])


grayed = []
for i in range(rows):
    row = []
    for j in range(cols):
        blue, green, red = data[i, j]
        gray = int(0.114 * blue + 0.587 * green + 0.299 * red)
        row.append(gray)
    grayed.append(row)
grayed = np.array(grayed, dtype=np.uint8)

print(data)
print(grayed)

wndData = "data"
wndGrayed = "greyed"

cv2.namedWindow(wndData, cv2.WINDOW_NORMAL)
cv2.imshow(wndData, data)
cv2.namedWindow(wndGrayed, cv2.WINDOW_NORMAL)
cv2.imshow(wndGrayed, grayed)
cv2.waitKey()

Could you review my code above and make it much better?

\$\endgroup\$
2
  • \$\begingroup\$ How is 0.114 * blue + 0.587 * green + 0.299 * red grey? I would expect grey to be blue/3 + green/3 + red/3 \$\endgroup\$
    – Reinderien
    Jun 9, 2021 at 10:39
  • \$\begingroup\$ @Reinderien: There are many algorithms to convert one color space to others. \$\endgroup\$ Jun 9, 2021 at 14:08

1 Answer 1

5
\$\begingroup\$

It appears that you are calculating linear combinations. If you are already using numpy, then the same can be achieved by broadcasting the dot product (with @):

import numpy as np

data = np.array([[[255, 0, 0], [0, 255, 0], [0, 0, 255]], 
                 [[0, 0, 0], [128, 128, 128], [255, 255, 255]]], dtype=np.uint8)

coefficients = np.array([0.114,0.587, 0.299])

greyed = (data @ coefficients).astype(np.uint8)
\$\endgroup\$
1
  • \$\begingroup\$ Just another note that grayed = np.round(data @ coefficients).astype(np.uint8) will make the result identical to one produced by cv2.cvtColor function. Kevin from Phyton chat room found this solution. :-) \$\endgroup\$ Jun 9, 2021 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.