python怎么把图片二进制输出_如何使用python制作二进制图像?

I built my image with cv2.THRESH_BINARY and the function

ret, im_thresh = cv2.threshold( gray_image, 40, 255,

cv2.THRESH_BINARY )

BUT White must be 1 to Work with my program.

To help I made:

height = int(np.size(im_thresh, 0))

width = int(np.size(im_thresh, 1))

for x in range(height):

for y in range(width):

if im_thresh[x,y]==255:

im_thresh[x,y] = 1

My question: Have any way to do this faster in python?

解决方案

Try using a boolean index to get and set the values of the arrays, which avoids the nested for loop.

import numpy as np

from numpy import random

# Generating an image of values between 1 and 255.

im_thresh = random.randint(1,256, (64,64))

# Set anything less than 255 to 0. Unnecessary if cv2 does this during threshold.

# Must go before the operation below in order not to set all values to 0.

im_thresh[im_thresh<255] = 0

# Set all values at indices where the array equals 255 to 1.

im_thresh[im_thresh==255] = 1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值