【OpenCV + Python】图像像素遍历&初始化图像&初始化结构体&运行时间统计

图像像素遍历&初始化图像&初始化结构体&运行时间统计等基本知识点。


import cv2
import numpy as np
def access_pixel(img):
    print(img.shape)
    height = img.shape[0]
    width = img.shape[1]
    channels = img.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 = img[row,col,c]
                img[row,col,c] = 255 - pv
    cv2.imshow("pixel_image",img)

def create_image():
    img = np.zeros([528,400,3],np.uint8)
    img[:,:,0] = np.ones([528,400])*255
    cv2.imshow("img_creat",img)
def creat_struct():
    m1 = np.ones([3,3],np.float32)
    m1.fill(12.23)
    print(m1)
    m2 = m1.reshape([1,9])
    print("...........")
    print(m2)
def filter_creat():
    m3 = np.array([[1,2,3],[4,5,6],[7,8,9]],np.uint8)
    print(m3)
    m3.fill(9)
    print("_________")
    print(m3)
img = cv2.imread('5.jpg',1)
cv2.imshow('img', img)
t1 = cv2.getTickCount()
access_pixel(img)
create_image()
creat_struct()
filter_creat()
t2 = cv2.getTickCount()
print("time: %s" %(str((t2-t1)/cv2.getTickFrequency()*1000)))
cv2.waitKey(0)
cv2.destroyAllWindows()

运行结果:
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/zhouzongzong/article/details/94413134
今日推荐