zoukankan      html  css  js  c++  java
  • 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再把这些行包括起来

    构成一个矩阵

  • 相关阅读:
    jquery ajax和php实现返回值 json类型
    jQuery一句话实现多选框全选/取消
    九宫格拼图 支持44 55等
    php通过$_SERVER['HTTP_USER_AGENT']获取浏览器useAgent
    微信刮刮卡代码
    从QQ网站中提取的纯JS省市区三级联动
    Codeigniter+PHPExcel导出数据到Excel文件
    flv视频播放器停止时带图片
    html5+js实现刮刮卡效果
    解决Tensorflow 使用时cpu编译不支持警告:that this TensorFlow binary was not compiled to use: AVX AVX2
  • 原文地址:https://www.cnblogs.com/mrfri/p/8650963.html
Copyright © 2011-2022 走看看