1
\$\begingroup\$

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() 
\$\endgroup\$
6
  • 5
    \$\begingroup\$"It looks like that it's not workable as the computing limitation." Are you saying the code works, it's just too slow? What happens if you reduce the framerate of your camera to a much lower value (0.5 FPS), does the program fully work at that point? At the moment, you're not doing any rate-limiting at all it seems.\$\endgroup\$
    – Mast
    CommentedSep 24, 2022 at 3:45
  • 1
    \$\begingroup\$(I wish I never looked at pages outside the referred range.)\$\endgroup\$
    – greybeard
    CommentedSep 24, 2022 at 11:55
  • 1
    \$\begingroup\$The algorithm is good image processing practice, but otherwise the cited paper is true, high-grade pseudoscientific nonsense.\$\endgroup\$CommentedSep 24, 2022 at 16:30
  • 1
    \$\begingroup\$Right, so. The algorithmic specification is internally inconsistent and rife with nonsense. The "lim" is not a limit; the author is attempting to describe a loop. And the colour mapping has overlapping indices for which the output is ambiguous. Given these internal inconsistencies, there is no such thing as a correct implementation.\$\endgroup\$CommentedSep 24, 2022 at 17:16
  • 1
    \$\begingroup\$Your implementation is not even close to what the paper intended. For fun, I wrote a vectorised implementation, and the results look the same as figure 14. But this entire endeavour is just so very, very silly.\$\endgroup\$CommentedSep 24, 2022 at 19:55

1 Answer 1

1
\$\begingroup\$

Looking at just the code, I find it difficult to see its purpose.
In particular, I don't find colormap:

Document your code. In the code.

Define a function for the mapping presented.
Find or define a function to apply it to an image.

For non-negative values, img_gray[i][j] % 36 may take on values from 0 up, but never 36 up.
Prefer

 if lower < val <= mid: pass # handle lower range elif val <= upper: # 1 comparison less than mid < val <= upper pass # handle upper range 

Consider a table-driven approach for populating mcolor.
While following the paper in separating cases that are handled identically increases recognisability, it sure looks off in the code without a comment. (Looks another error in the paper, at that - where does that where i >= 240. come from?)

While next to all the numeric literals in the paper look magic to me,
avoid magic numbers like 36 and 255: give them names.

There are three occurrences of min(round((int(r1) + int(r2) + 1) / 1.2), 255): define a function. (isn't round((int(r1) + int(r2) + 1) / 2) equal to ceil((int(r1) + int(r2)) / 2)?)

\$\endgroup\$

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.