python做图片四则运算结果都是灰色,imshow灰色图像和二进制图像python

I have a grayscale image and a binary image, and I want to plot them side by side using hstack. It looks like there is kind of adjustment that been made yielding to darken the binary. Anybody faced this problem?

oos05.png

Here is my code

O = (self.img >= t) * 1

I = img

both = np.hstack((I, O))

imshow(both, cmap='gray')

show()

解决方案

This is to demonstrate a somewhat different from your case which I don't know of its data. I suspect that all the values in your array 'O' are zero, thus, the plot came out as a black pane.

import numpy as np

import matplotlib.pyplot as plt

fig=plt.figure(figsize=(8, 4))

# make up some data for demo purposes

raw = np.random.randint(10, size=(6,6))

# apply some logic operatioin to the data

O = (raw >= 5) * 1 # get either 0 or 1 in the array

I = np.random.randint(10, size=(6,6)) # get 0-9 in the array

# plot each image ...

# ... side by side

fig.add_subplot(1, 2, 1) # subplot one

plt.imshow(I, cmap=plt.cm.gray)

fig.add_subplot(1, 2, 2) # subplot two

# my data is OK to use gray colormap (0:black, 1:white)

plt.imshow(O, cmap=plt.cm.gray) # use appropriate colormap here

plt.show()

The resulting image:

1zY0v.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要读取二进制图片,你可以使用Python的内置模块PIL(Python Imaging Library)或者OpenCV库。下面是使用PIL库读取二进制图片的示例代码: ```python from PIL import Image import io # 打开二进制图片文件 with open('image.jpg', 'rb') as f: # 创建一个BytesIO对象,并将文件内容写入到该对象中 bytes_io = io.BytesIO(f.read()) # 使用PIL库打开BytesIO对象中的图片 image = Image.open(bytes_io) # 可以对图片进行进一步的操作,比如显示、保存等 image.show() ``` 在上面的示例中,我们首先使用`open`函数打开二进制图片文件,并以二进制模式`'rb'`读取文件内容。然后,我们创建一个`BytesIO`对象,并将文件内容写入到该对象中。接下来,使用`Image.open`函数打开`BytesIO`对象中的图片。最后,你可以对图片进行进一步的操作,比如显示(`image.show()`)或保存(`image.save('output.jpg')`)。 注意,你需要将`image.jpg`替换为你实际的图片文件路径。 如果你想使用OpenCV库读取二进制图片,你可以使用`imdecode`函数来实现。下面是使用OpenCV库读取二进制图片的示例代码: ```python import cv2 import numpy as np # 读取二进制图片文件 with open('image.jpg', 'rb') as f: # 读取文件内容 image_data = f.read() # 将二进制数据转换为numpy数组 np_array = np.frombuffer(image_data, np.uint8) # 使用OpenCV库将numpy数组解码为图片 image = cv2.imdecode(np_array, cv2.IMREAD_COLOR) # 可以对图片进行进一步的操作,比如显示、保存等 cv2.imshow('Image', image) cv2.waitKey(0) cv2.destroyAllWindows() ``` 在上面的示例中,我们首先使用`open`函数打开二进制图片文件,并读取文件内容。然后,使用`frombuffer`函数将二进制数据转换为numpy数组。接下来,使用`imdecode`函数将numpy数组解码为图片。最后,你可以对图片进行进一步的操作,比如显示(`cv2.imshow`)或保存(`cv2.imwrite`)。 同样地,你需要将`image.jpg`替换为你实际的图片文件路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值