SUN JIAWEI
SUN JIAWEI

Reputation: 117

Is there some difference between colormap of opencv and matplotlib?

I am doing some prediction of depth. So I used Colormap both in Opencv and Matplotlib on the predicted image. However, it performed well in Matplotlib but not in OpenCV(colormap_jet).

How can I solve this problem in Opencv? Because I want to use Opencv in real-time. Matplotlib is too slow in real-time.

Here is the result. matplotlib opencv

Upvotes: 0

Views: 2066

Answers (1)

Kinght 金
Kinght 金

Reputation: 18321

Use matplotlib.pylab.cm to colorize the image.

#!/usr/bin/python3
# 2017.12.28 16:26:26 CST
import matplotlib.pyplot as plt
from matplotlib.pylab import cm
import numpy as np
import cv2

## use matplot jet for opencv
def colorize(img):
    gray = None
    if img.ndim == 2:
        gray = img.copy()
    if len(img.shape) == 3:
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    canvas = np.uint8(cm.jet(gray)*255)
    canvas = cv2.cvtColor(canvas, cv2.COLOR_RGBA2BGR)
    return canvas

## process
img = cv2.imread("test.png")
res = colorize(img)
cv2.imwrite("res.png", res)

Example 1:

Generate the colorized the image (as the preview code).

enter image description here

Example 2:

Colorize the same data in Matplotlib (in jet) and OpenCV(using my function colorize).

enter image description here

Upvotes: 1

Related Questions