opencv learning three: numpy array operations

1. Read a picture and output after modifying the color channel

The image can be obtained: the matrix of the number of rows, the number of columns, and the number of channels. The image pixels can be changed by operating the matrix. The
code is as follows:

import cv2 as cv  #导入cv模块
import numpy as np #np科学计数的包,通过numpy对数据进行处理

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("pixels_demo", image)
    
src = cv.imread("C:/Users/lenovo/Desktop/opencv/daima/banknum/template-matching-ocr/images/lena.jpg")  #读取图片位置
#blue green red
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)  #创建一个GUI
cv.imshow("input image", src) #对窗口图片进行展示
t1 = cv.getTickCount()
access_pixels(src)
t2 = cv.getTickCount()
time = (t2 - t1)*1000/cv.getTickFrequency()
print("time : %s" % time)
cv.waitKey(0)
cv.destroyAllWindows()  #释放所有的内存

Running screenshot:
Insert picture description herecv::getTickCount() can be used to measure the running time of a piece of code. This function returns the number of clock cycles since the last boot.

Since what we need is the number of milliseconds a certain code segment runs, we also need another function cv::getTickFrequency(). This function returns the number of clock cycles per second.

2. Customize a multi-channel picture

Functions used: zeros and ones
codes are as follows:

import cv2 as cv
import numpy as np
 
 
def create_image():
    img = np.zeros([400, 400, 3], np.uint8)#zeros:double类零矩阵  创建400*400 3个通道的矩阵图像 参数时classname为uint8
    img[:, :, 0] = np.ones([400, 400])*255#ones([400, 400])是创建一个400*400的全1矩阵,*255即是全255矩阵 并将这个矩阵的值赋给img的第一维
    img[:, :, 1] = np.ones([400, 400])*255#第二维全是255
    img[:, :, 2] = np.ones([400, 400])*255#第三维全是255
    cv.imshow("new image", img)#输出一张400*400的白色图片(255 255 255):蓝(B)、绿(G)、红(R)
 
create_image()
cv.waitKey(0)
cv.destroyAllWindows()

The result of the operation is to output a white picture. You can also modify 255 to other numbers to output pictures of different colors.
You can also use the ones function separately, the code is as follows:


import cv2 as cv
import numpy as np
 
 
def create_image():
    img = np.ones([400, 400, 3], np.uint8)
    img[:, :, 0] = img[:, :, 0]*255
    img[:, :, 1] = img[:, :, 1]*255
    img[:, :, 2] = img[:, :, 2]*255
    cv.imshow("new image", img)
 
create_image()
cv.waitKey(0)
cv.destroyAllWindows()

Two: Customize a single-channel picture

# -*- coding=GBK -*-
import cv2 as cv
import numpy as np
 
 
def create_image():
    img = np.ones([400, 400, 1], np.uint8)#该像素点只有一个通道,该函数使所有像素点的通道的灰度值为1
    img = img * 127 #使每个像素点单通道的灰度值变为127,0到255都可以,颜色由黑(0)转灰(127)转白(255)
    cv.imshow("new image", img)
 
create_image()
cv.waitKey(0)
cv.destroyAllWindows()

note:

1. img = img * 127 in the code means that each value in the array is multiplied by 127

2. The reason why the parameter type of the np.ones function is uint8 is because the range of uint8 number is 0~255, then it is black when it is 0, and it is white when it is 255. If the function parameter type is int8, the range of int8 type number is -128~127, then -128 is black, 127 is white

For the use of reshape() method

Insert picture description here
Insert picture description here
Insert picture description here

Three, call the library function to achieve pixel inversion

# -*- coding=GBK -*-
import cv2 as cv
import numpy as np
 
 
#像素取反
def inverse(image):
    dst = cv.bitwise_not(image)
    cv.imshow("取反", dst)
 
 
src = cv.imread("C://1.jpg")
cv.namedWindow("原来", cv.WINDOW_NORMAL)
cv.imshow("原来", src)
t1 = cv.getTickCount()
inverse(src)
t2 = cv.getTickCount()
time = (t2 - t1)*1000/cv.getTickFrequency()
print("time: %s" % time)
cv.waitKey(0)
cv.destroyAllWindows()

Calling the bitwise_not function runs very fast!

Guess you like

Origin blog.csdn.net/weixin_44145452/article/details/112411853