python+opencv修改像素值、创建图像

import cv2 as cv
import numpy as np


# 将图像中的像素值改为   255-原像素值
def access_pixels(image):
    print(image.shape)
    height = image.shape[0]
    width = image.shape[1]
    channels = image.shape[2]
    print('width: %s, height: %s, channels: %s'%(width, height, 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('pixel_demo', image)


# 作用等同于上面的access_pixels
def inverse(image):
    dst = cv.bitwise_not(image)
    cv.imshow('inverse demo', dst)


# 创建三通道、单通道图像
def creat_demo():
    # img = np.zeros([400, 400, 3], np.uint8)  # 三通道顺序是BGR
    # # img[:, :, 0] = np.ones([400, 400]) * 255
    # img[:, :, 2] = np.ones([400, 400])*255
    # cv.imshow('new image', img)

    #单通道
    # img = np.ones([400, 400, 1], np.uint8)  # 需要指明通道数1
    # # img[:, :, 0] = np.ones([400, 400]) * 127
    # img = img*127
    # cv.imshow('new image', img)
    # cv.imwrite('C:/Users/Y/Pictures/Saved Pictures/myImage.png', img)

    m1 = np.ones([3, 3], np.uint8)
    m1.fill(12222.388)
    print(m1)

    m2 = m1.reshape([1, 9])
    print(m2)


src = cv.imread('C:/Users/Y/Pictures/Saved Pictures/demo.png')
cv.namedWindow('input image', cv.WINDOW_AUTOSIZE)
cv.imshow('input image', src)
t1 = cv.getTickCount()
inverse(src)
t2 = cv.getTickCount()
time = (t2-t1)/cv.getTickFrequency()
print('time: %s ms' % time*1000)
cv.waitKey(0)
cv.destroyAllWindows()

 

你可能感兴趣的:(图像处理,python+opencv)