opencv-python遍历整个图像的每个像素点

24 篇文章 1 订阅

from PIL import Image
import numpy as np
img = Image.open("F:/PYproject/unet_camvid/CamVid/testimage/2.png")
#img.show()
img_array = np.array(img)#把图像转成数组格式img = np.asarray(image)
shape = img_array.shape
print(img_array.shape)
for i in range(0,shape[0]):
    for j in range(0,shape[1]):
        value = img_array[i, j]
        #print("",value)
        if value[0] != 0:
            print("", value)
height = shape[0]
width = shape[1]
dst = np.zeros((height,width,3))
for h in range(0,height):
    for w in range (0,width):
        (b,g,r) = img_array[h,w]
        if (b,g,r)==(255,255,255):#白色
            img_array[h,w] = (0,255,255)#蓝色
        if (b, g, r) == (85, 85, 85):  # 深灰
            img_array[h, w] = (0, 128, 0)  # 绿色
        if (b, g, r) == (170, 170, 170):  # 灰色
            img_array[h, w] = (255, 255, 0)  # 黄色
        if (b, g, r) == (0, 0, 0):  # 黑色
            img_array[h, w] = (255, 0, 0)  # 红色
        dst[h,w] = img_array[h,w]
img2 = Image.fromarray(np.uint8(dst))
img2.show(img2)
img2.save("3.png","png")

行优先,一次访问。注意行坐标(y)的位置 

for  行 --- 行总数:
    for 列----列总数:
        value = img[行,列]

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值