opencv + python 读取像素点 BGRtoRGB 以及注意事项

import cv2
import numpy as np

fengmian='picture.jpg'

img3=cv2.imread(fengmian)
img4=cv2.cvtColor(img3, cv2.COLOR_BGR2RGB) # cv2默认为bgr顺序
h, w, _ = img3.shape #返回height,width,以及通道数,不用所以省略掉
print('行数%d,列数%d' % (h, w))
for i in img4:
    jihe.append(i)
print(jihe)
print('集合长度%d' % (len(jihe)))

注意cv2默认为 BGR顺序,而其他软件一般使用RGB,所以需要转换 

cv2.COLOR_BGR2RGB

官方文档说 imread 返回的是一个mat类型的变量

也就是用它来存储图片数据

为了弄明白mat到底是个啥玩意,我开始了测试

上面的代码输出结果为:

行数9,列数5
[array([[ 94, 121, 252],
       [ 92, 119, 250],
       [ 90, 117, 248],
       [ 89, 116, 247],
       [ 89, 116, 247]], dtype=uint8), array([[ 93, 120, 251],
       [ 90, 117, 248],
       [ 90, 117, 248],
       [ 91, 118, 249],
       [ 91, 118, 249]], dtype=uint8), array([[ 94, 121, 252],
       [ 91, 118, 249],
       [ 91, 118, 249],
       [ 93, 120, 251],
       [ 92, 119, 250]], dtype=uint8), array([[ 96, 123, 254],
       [ 92, 119, 250],
       [ 91, 118, 249],
       [ 93, 120, 251],
       [ 92, 119, 250]], dtype=uint8), array([[ 96, 123, 254],
       [ 90, 117, 248],
       [ 89, 116, 247],
       [ 91, 118, 249],
       [ 91, 118, 249]], dtype=uint8), array([[ 94, 121, 252],
       [ 90, 117, 248],
       [ 88, 115, 246],
       [ 88, 115, 246],
       [ 87, 114, 245]], dtype=uint8), array([[ 94, 121, 252],
       [ 91, 118, 249],
       [ 90, 117, 248],
       [ 88, 115, 246],
       [ 86, 113, 244]], dtype=uint8), array([[ 93, 120, 251],
       [ 92, 119, 250],
       [ 93, 120, 251],
       [ 90, 117, 248],
       [ 87, 114, 245]], dtype=uint8), array([[ 96, 123, 254],
       [ 93, 120, 251],
       [ 93, 120, 251],
       [ 92, 119, 250],
       [ 90, 117, 248]], dtype=uint8)]
集合长度9

可以看到,mat应该是一种矩阵,以图片中像素的行和列来排布

每一个像素对应的就是[  ,   ,   ,]中 RGB的值

 

图画的丑了点,大概就是这样的

每一个行是一个集合,其中包括着该行的每一列的一个数值

然后mat再把这些行包括起来

构成一个矩阵

 

转载于:https://www.cnblogs.com/mrfri/p/8650963.html

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值