如何阻止 numpy hstack 更改 opencv 中的像素值

how to stop numpy hstack from changing pixel values in opencv

我正在尝试使用 opencv 在 python 中显示一个图像,上面有一个侧窗格。当我使用 np.hstack 时,主图变成了无法辨认的白色,只有少量颜色。这是我的代码:

    img = cv2.imread(filename)
    img_with_gt, gt_pane = Evaluator.return_annotated(img, annotations)
    both = np.hstack((img_with_gt, gt_pane))

    cv2.imshow("moo", both)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

这是生成的图片

但是如果我查看 img_with_gt 它看起来是正确的。

甚至可以与 gt_pane

一起使用

我似乎无法弄清楚为什么会这样。

我能看到发生这种情况的唯一方法是两个图像之间的数据类型不一致。确保在 return_annotated 方法中,img_with_gtgt_pane 都共享相同的数据类型。

您提到您正在为 gt_pane 分配 space 为 float64。这表示 [0-1] 范围内的强度/颜色。将图像转换为uint8,并将结果乘以255,以确保两个图像之间的兼容性。如果您想保持图像不变并处理分类图像(右边的图像),请转换为 float64 然后除以 255。

但是,如果您想保留该方法不变,一个简单的修复可能是:

both = np.hstack(((255*img_with_gt).astype(np.uint8), gt_pane))

你也可以反过来:

both = np.hstack((img_with_gt, gt_pane.astype(np.float64)/255.0))