Python-OpenCV图像处理-02-numpy数组操作

 创建新图像:

利用数组操作

import cv2 as cv
import numpy as np

像素属性读取: 

这里的图像说白了就是二维的数组每个数组元素的数值代表颜色,由(bgr)三张单色的图片混合而成。这里叫做通道数:

def access_pixels(image):#像素属性读取函数
    print(image.shape)
    height = image.shape[0]
    width =image.shape[1]
    channels =image.shape[2]
    print("height:  %s width: %s channels: %s"%(height,width,channels))
    for row in range(height):
        for col in range(width):
            for c in range(channels):
                pv = image[row,col,c]
                image[row,col,c] =255 -pv
    cv.imshow("pixels_demo",image)

创建数组:

m1 =np.ones([3,3],np.uint8)    
m1.fill(2)#在数组中元素全填上2

因为前文也说了彩色图像实际上就是由数组定义的单色图像的叠加

创建图像:

def create_image():
    #图像全部变成0
    img = np.zeros([400,400,3],np.uint8)#创建一个图像 多通道 bgr
    img[:,:,0] =255#b g r 三个通道
    img[:,:,1] = np.ones([400,400])*255
    cv.imshow("new image",img)
    #创建单通道的图像  灰度图像
    img1 =np.zeros([400,400,1],np.uint8)
    img1[:,:,0] = np.ones([400,400])*111
    cv.imshow("new image :",img1)

猜你喜欢

转载自blog.csdn.net/weixin_41445387/article/details/86553520