How to using custom colormap with pixel calculation, gray scale value by opencv and make it workable on camera

Viewed 49

I tried to implement the mcolor colormap in the paper at page 8 to 10.

I developed the code to process a image file and it works ok. Now, I want to adopt this image processing method to the camera which is captured by opencv. I implemented it by python and opencv. It looks like that it's not workable as the computing limitation. Can anyone give me the direction to improve it? I expected to adopt my image processing algorithm to all frames of camera. Thank you. Below is my complete source code of python

import numpy as np
import cv2

cap = cv2.VideoCapture(0)
while True:
    ret, img = cap.read()
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    img_new = np.ones([img.shape[0], img.shape[1], 3], np.uint8)
    mcolor = np.ones([img.shape[0], img.shape[1], 3], np.uint8)
    for i in range(img.shape[0]):
        for j in range(img.shape[1]):
            val = img_gray[i][j] % 36
            if img_gray[i][j] <= 72:
                if val >= 1 and val <= 6:
                    mcolor[i][j] = (0, 0, 35)
                elif val >= 7 and val <= 12:
                    mcolor[i][j] = (i * 10, 0, 0)
                elif val >= 13 and val <= 18:
                    mcolor[i][j] = (1, 0, 35)
                elif val >= 19 and val <= 24:
                    mcolor[i][j] = (i * 20, 0, i * 20)
                elif val >= 25 and val <= 30:
                    mcolor[i][j] = (i * 10, i * 5, i * 5)
                elif val >= 31 and val <= 36:
                    mcolor[i][j] = (i * 10, 0, i * 4)
            elif img_gray[i][j] >= 73 and img_gray[i][j] <= 240:
                if val >= 1 and val <= 6:
                    mcolor[i][j] = (0, 0, i * 6)
                elif val >= 7 and val <= 12:
                    mcolor[i][j] = (0, i * 6, i * 6)
                elif val >= 13 and val <= 18:
                    mcolor[i][j] = (0, i * 6, 0)
                elif val >= 19 and val <= 24:
                    mcolor[i][j] = (i * 6, i * 6, 0)
                elif val >= 25 and val <= 30:
                    mcolor[i][j] = (i * 6, 0, i * 6)
                elif val >= 31 and val <= 36:
                    mcolor[i][j] = (i * 6, 0, 0)
            elif img_gray[i][j] > 241:
                if val >= 1 and val <= 6:
                    mcolor[i][j] = (0, i * 10, 30)
                elif val >= 7 and val <= 12:
                    mcolor[i][j] = (0, i * 10, 30)
                elif val >= 13 and val <= 18:
                    mcolor[i][j] = (0, i * 10, 30)
                elif val >= 19 and val <= 24:
                    mcolor[i][j] = (i * 10, 0, 0)
                elif val >= 25 and val <= 30:
                    mcolor[i][j] = (i * 10, 0, 0)
                elif val >= 31 and val <= 36:
                    mcolor[i][j] = (i * 10, 0, 0)

    for i in range(img.shape[0]):
        for j in range(img.shape[1]):
            b1, g1, r1 = img[i][j]
            r2, g2, b2 = mcolor[i][j]
            r, g, b = (
                min(round((int(r1) + int(r2) + 1) / 1.2), 255),
                min(round((int(g1) + int(g2) + 1) / 1.2), 255),
                min(round((int(b1) + int(b2) + 1) / 1.2), 255),
            )
            img_new[i][j] = (r, g, b)

    cv2.imshow("live", img_new)
    # cv2.imwrite("img_new.jpeg", img_new)
    # cv2.waitKey(0)
    if cv2.waitKey(1) == ord("q"):
        break
cap.release()
cv2.destroyAllWindows()
1 Answers
Related