【问题标题】:How to perform OpenCV transform without explicitly saving the image如何在不显式保存图像的情况下执行 OpenCV 转换
【发布时间】:2019-08-04 09:12:11
【问题描述】:

当我输入图像 b 时

cv2.imwrite("contor.jpg", b)

我明白了

我想只保留图像 b 中的白色像素并删除其余的像素来做我所做的:

im = cv2.imread("contor.jpg")

im[np.any(im != [255, 255, 255], axis=-1)] = [0,0,0]

cv2.imwrite('box_mask.png', im)

在此之后我得到输出:

我的问题是每次得到如上所示的输出我必须保存图像 b。通过使用

cv2.imwrite("contor.jpg", b)

然后使用

回读
im = cv2.imread("contor.jpg")

然后将所有非白色像素更改为黑色。我想这样做而不是每次都保存图像并读取它

为此我做了:

im=b.copy()

im[np.any(im != [255, 255, 255], axis=-1)] = [0,0,0]

cv2.imwrite('box_mask.png', im)

为此我收到此错误:

python3 demo2.py --image 1.jpg 
Traceback (most recent call last):
  File "demo2.py", line 123, in <module>
    im[np.any(im != [255, 255, 255], axis=-1)] = [0,0,0]
ValueError: shape mismatch: value array of shape (3,) could not be broadcast to indexing result of shape (1,512,640)

如何避免每次都保存图像 b 并重新阅读?我想直接操作 b 并显示最终结果。

【问题讨论】:

标签: python python-3.x opencv computer-vision


【解决方案1】:

好的,这相对容易。我提到了Replace all the colors of a photo except from the existing black and white pixels PYTHON

并通过这样做:

im=b.copy()
im[im != 255] = 0

cv2.imshow("out.jpg",im)
cv2.waitKey(0)

我解决了这个问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-08
    • 2014-04-17
    • 1970-01-01
    • 2015-04-10
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    相关资源
    最近更新 更多