opencv的colormap和matplotlib有什么区别吗?

2024-06-26 14:23:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我在做深度预测。所以我在Opencv和Matplotlib中都对预测的图像使用了Colormap。 然而,在colorMatu中表现不佳。在

如何在Opencv中解决这个问题?因为我想实时使用Opencv。Matplotlib实时速度太慢。在

这是结果。 matplotlibopencv


Tags: 图像matplotlibopencv速度colormapcolormatu
1条回答
网友
1楼 · 发布于 2024-06-26 14:23:10

使用matplotlib.pylab.cm为图像着色。在

#!/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)

例1:

生成彩色图像(作为预览代码)。在

enter image description here

例2:

Matplotlib(在jet)和{}(使用我的函数colorize)中为相同的数据着色。在

enter image description here

相关问题 更多 >