opencv learning five: picture pixel operations-numerical operations and logical operations

Pixel operation

Insert picture description here

One, arithmetic operations

Insert picture description here1.1 Add, subtract, multiply and divide
Opencv's own picture color processing functions:
add: add()
, subtract: subtract(),
multiply: multiply()
, divide: divide(), the
principle is: get two (only two at a time) Zhang) The color value of the same position of a picture to realize the calculation.
Operational requirements: the shape of the two pictures must be the same.
Example image:
Insert picture description here
Code:

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

def add_demo(m1, m2):
    dst = cv.add(m1, m2)
    cv.imshow("add_demo", dst)


def subtract_demo(m1, m2):
    dst = cv.subtract(m1, m2)
    cv.imshow("subtract_demo", dst)


def multiply_demo(m1, m2):
    dst = cv.multiply(m1, m2)
    cv.imshow("multiply_demo", dst)

def divide_demo(m1, m2):
    dst = cv.divide(m1, m2)
    cv.imshow("divide_demo", dst)


src1 = cv.imread("D:/opencv3.4.13/opencv/sources/samples/data/LinuxLogo.jpg")  #读取图片位置
src2 = cv.imread("D:/opencv3.4.13/opencv/sources/samples/data/WindowsLogo.jpg")  #读取图片位置
print(src1.shape)
print(src2.shape)
cv.namedWindow("image1", cv.WINDOW_AUTOSIZE)  #创建一个GUI
cv.imshow("image1", src1) #对窗口图片进行展示
cv.imshow("image2", src2) #对窗口图片进行展示
add_demo(src1, src2)
subtract_demo(src1, src2)
multiply_demo(src1, src2)
divide_demo(src1, src2)
cv.waitKey(0)
cv.destroyAllWindows()  #释放所有的内存

The code can be simplified:

import cv2 as cv
 
 
#数值运算:加减乘除
def shu_image(src11, src22):
    src = cv.add(src11, src22)#加
    cv.imshow("add", src)
    src = cv.subtract(src11, src22)#减
    cv.imshow("subtract", src)
    src = cv.subtract(src11, src22)#乘
    cv.imshow("subtract", src)
    src = cv.divide(src11, src22)#除
    cv.imshow("divide", src)
 
 
 
 
src1 = cv.imread("01.jpg")
src2 = cv.imread("02.jpg")
cv.imshow("image1", src1)
cv.imshow("image2", src2)
shu_image(src1, src2)
cv.waitKey(0)
cv.destroyAllWindows()

Run screenshot:
Insert picture description here

1.2 Adjust brightness and adjust contrast
Basic principle: Two pictures are combined.

First, create a new picture with all zero pigments according to the original picture format, and then synthesize a new picture according to the different proportions of the two pictures. Mainly used function: addWeighted function

code show as below:

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

# 粗略的调节对比度和亮度
def contrast_brightness_image(src1, a, g):
    h, w, ch = src1.shape  # 获取shape的数值,height和width、通道

    # 新建全零图片数组src2,将height和width,类型设置为原图片的通道类型(色素全为零,输出为全黑图片)
    src2 = np.zeros([h, w, ch], src1.dtype)
    dst = cv.addWeighted(src1, a, src2, 1 - a, g)  # addWeighted函数说明如下
    cv.imshow("con-bri-demo", dst)

src = cv.imread("C:/Users/lenovo/Desktop/opencv/daima/banknum/template-matching-ocr/images/lena.jpg") 
cv.namedWindow("image1", cv.WINDOW_AUTOSIZE)  #创建一个GUI
cv.imshow("image1", src) #对窗口图片进行展示
contrast_brightness_image(src, 1.5, 10)#第一个1.2为对比度  第二个为亮度数值越大越亮

cv.waitKey(0)
cv.destroyAllWindows()  #释放所有的内存

Run screenshot:
Insert picture description hereaddWeighted function: official: calculate the weight of two image arrays and my understanding is to synthesize two images according to the proportion.
Opencv: cv2.addWeighted() image fusion

addWeighted(InputArray src1, double alpha, InputArray src2, double beta, double gamma, OutputArray dst, int dtype=-1);

There are a total of seven parameters: the first 4 are the two pictures to be synthesized and their proportions, the fifth double gamma plays a fine-tuning role, the sixth OutputArray dst is the synthesized picture, and the seventh output picture type (Optional parameter, default -1)

There is a formula to get the picture output by adding two pictures: dst = src1[I] * alpha + src2[I] * beta + gamma

Reference: Opencv advanced study notes 3: Pixel operation and image brightness and contrast adjustment
The larger the c, the brighter.
What works is the coefficient 1, and the amount of brightness adjustment.
The coefficient 2 multiplied by the array of all 0s is useless, and it is used as a parameter here.

Two, logical operations

Insert picture description here2.1 AND, OR, NOT

Opencv comes with image color processing functions:
and: bitwise_add()
or: bitwise_or()
non: bitwise_not()
XOR: bitwise_xor() The
code is as follows:

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

def logic_demo(m1, m2):
    dst = cv.bitwise_and(m1, m2)
    dst = cv.bitwise_or(m1, m2)
    image = cv.imread("D:/opencv3.4.13/opencv/sources/samples/data/LinuxLogo.jpg")  # 读取图片位
    dst = cv.bitwise_not(image)
    dst = cv.bitwise_xor(m1, m2)
    cv.imshow("logic_demo", dst)

src1 = cv.imread("D:/opencv3.4.13/opencv/sources/samples/data/LinuxLogo.jpg")  #读取图片位置
src2 = cv.imread("D:/opencv3.4.13/opencv/sources/samples/data/WindowsLogo.jpg")  #读取图片位置
print(src1.shape)
print(src2.shape)
cv.namedWindow("image1", cv.WINDOW_AUTOSIZE)  #创建一个GUI
cv.imshow("image1", src1) #对窗口图片进行展示
cv.imshow("image2", src2) #对窗口图片进行展示
logic_demo(src1, src2)
cv.waitKey(0)
cv.destroyAllWindows()  #释放所有的内存

Run screenshot:
Insert picture description here
2.2 Mask layer control The
code is as follows:

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

def extrace_object_demo():
    capture = cv.VideoCapture("C:/Users/lenovo/Desktop/opencv/daima/banknum/test.mp4")
    while(True):
        ret, frame = capture.read()
        if ret == False:
            break;
        hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
        lower_hsv = np.array([37, 43, 46])
        upper_hsv = np.array([77, 255, 255])
        mask = cv.inRange(hsv, lowerb=lower_hsv, upperb=upper_hsv)
        dst = cv.bitwise_and(frame, frame, mask=mask)
        cv.imshow("video", frame)
        cv.imshow("mask", dst)
        c = cv.waitKey(40)
        if c == 27:
            break
            
extrace_object_demo()
cv.waitKey(0)
cv.destroyAllWindows()  #释放所有的内存


Run screenshot:
Insert picture description hereexplain the meaning of the parameters in res = cv2.bitwise_and (img, img, mask = mask)
src1: the first image (the merged first object) src2: the second image (the merged second object) mask : Understand the rules to be merged. If the area of ​​the image (scaled in grayscale and then masked) has black (value 0), it is not merged (the merged area of ​​the first image and the merged area of ​​the second image). Vice versa, it will be executed

Guess you like

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