python图片二进制流转换成图片_将图像(np.数组)转换为二进制imag

本文为Python新手介绍如何使用scipy库中的图像处理功能,通过示例代码实现浣熊图像的灰度转换,并探讨如何设置阈值将图片转为黑白二进制。作者分享了自定义函数binary_racoon的过程,以及选择阈值的困惑和可能的优化方法。
摘要由CSDN通过智能技术生成

谢谢你阅读我的问题。

我是python新手,对scipy很感兴趣。我正试图弄清楚如何将浣熊的图像(在scipy misc中)转换成二进制图像(黑白)。这不是在scipy讲座教程中教的。

到目前为止这是我的代码:%matplotlib inline

import matplotlib.pyplot as plt

import numpy as np

from scipy import misc #here is how you get the racoon image

face = misc.face()

image = misc.face(gray=True)

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

print image.shape

def binary_racoon(image, lowerthreshold, upperthreshold):

img = image.copy()

shape = np.shape(img)

for i in range(shape[1]):

for j in range(shape[0]):

if img[i,j] < lowerthreshold and img[i,j] > upperthreshold:

#then assign black to the pixel

else:

#then assign white to the pixel

return img

convertedpicture = binary_racoon(image, 80, 100)

plt.imshow(convertedpicture, cmap=plt.cm.gist_gray)

我见过其他人使用OpenCV制作图片二进制文件,但我想知道如何通过在像素上循环来这样做?我不知道该给上下限值多少,所以我猜了80和100。还有办法确定吗?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值