【问题标题】:how to stop numpy hstack from changing pixel values in opencv如何阻止numpy hstack改变opencv中的像素值
【发布时间】:2015-10-09 01:05:18
【问题描述】:

我正在尝试使用 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

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

【问题讨论】:

  • 两张图片的数据类型是什么?具体来说,当您在 REPL 中执行 img_with_gt.dtypegt_pane.dtype 时会发生什么?他们是同一个班级吗?
  • gt_pane 是 float64,img_with_gt 是 uint8 ..我在对 np.zeroes 的调用中使用可选的 dtype 更改了结构,它运行良好。谢谢!!如果你回答,我可以给你打勾。
  • 好了。这是数据类型的冲突。是的,让我现在写一个答案:)

标签: python opencv numpy image-processing


【解决方案1】:

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

您提到您正在为gt_pane 分配空间为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))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-03
    • 1970-01-01
    • 2017-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多