小言_互联网的博客

OpenCV——03用Numpy操作图片

387人阅读  评论(0)

OpenCV——03用Numpy操作图片

# -*- coding:utf-8 -*-
import cv2 as cv
import numpy as np


# 访问图片中的每个像素
def access_pixel(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)

# 通过numpy创建新图像
def create_image():
    '''
    img = np.zeros([400, 400, 3], np.uint8)
    # 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)
    img[:, :, 0] = np.ones([400, 400]) * 127
    cv.imshow("new image", img)
    '''

    m1 = np.ones([3, 3], np.float32)
    m1.fill(122.388)
    print(m1)


# 读取图片
src = cv.imread("D:\Python\Projects\OpenCV_toturial\images\example.png")
# 创建opencv的GUI窗口
# cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
# 将图片放入指定名字的窗口中显示出来
# cv.imshow("input image", src)
# t1 = cv.getTickCount()
# access_pixel(src)
# t2 = cv.getTickCount()
create_image()

# t = (t2-t1)/cv.getTickFrequency()
# print("time: %s ms"%(t*1000))
# 设置waitKey中的delay为0,程序会等待用户操作后关闭窗口
cv.waitKey(0)
cv.destroyAllWindows()

转载:https://blog.csdn.net/qq_36825778/article/details/101795474
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场