This is the 15th day of my participation in the August Text Challenge.More challenges in August

Multiply and divide cv2.multiply and divide cv2.divide This is followed by some pixel logic and a comprehensive case study

cv2.multiply

The syntax for this function is as follows:

cv2.multiply(src1, src2, dst=None, scale=None, dtype=None)
Copy the code

Parameter description: SRC1: the first image SRc2: the second image DST: This parameter is optional. The target image needs to be allocated in advance and can be omitted. The depth of the output image array, which defaults to -1

Test the following code and, after trying it out, find that the multiplication can easily be all black or all white. After all, two numbers can easily exceed 255 when multiplied.

import cv2 as cv

def multiply_demo(src1, src2) :
    ret = cv.multiply(src1, src2)
    cv.imshow("multiply_demo", ret)
    cv.waitKey(0)

if __name__ == "__main__":
    src1 = cv.imread("./18_3.jpeg")
    src2 = cv.imread("./18_4.jpg")
    multiply_demo(src1, src2)
Copy the code

If the second image of the cv2.multiply function is a scalar, the result is a stack for a single channel.

import cv2 as cv

from matplotlib import pyplot as plt

def multiply_demo(src1, src2) :
    ret = cv.multiply(src1, 1.5)

    plt.subplot(121), plt.imshow(src1), plt.title('src1')
    plt.subplot(122), plt.imshow(ret), plt.title('ret')
    plt.show()

if __name__ == "__main__":
    src1 = cv.imread("./18_3.jpeg")
    src2 = cv.imread("./18_4.jpg")
    multiply_demo(cv.cvtColor(src1,cv.COLOR_BGR2RGB), cv.cvtColor(src2,cv.COLOR_BGR2RGB))
Copy the code

Operation effect screenshot:

cv2.divide

The syntax for this function is:

divide(src1, src2, dst=None, scale=None, dtype=None)
Copy the code

This is basically the same thing as multiplying, but it’s easy to divide and get an all black graph.

Specific can try by oneself.

Logical operation of pixels

The logical operation of pixels involves basic operations such as and, or, not, Xor, etc. (To perform the logical operation, the shape of two images must be the same)

The corresponding function format is as follows

cv2.bitwise_and(src1, src2,mask)

Function: the binary data “and” operation, that is, the image (gray image or color image can be) each pixel value of the binary “and” operation, 1&1=1, 1&0=0, 0&1=0, 0&0=0, this inside the mask is very useful, we will elaborate.

Before you can learn film acting thoroughly, you need to be exposed to a new concept and learn something new.

And in the next few blogs, I will focus on film performance and ROI, because it is very important to discover this part of knowledge when learning.

ROI (Region Of Interest, Region Of Internet)

Let’s find the ROI and move the ROI. The code is as follows:

The following code tests the image using:

What we’re after is Rem’s horns

import cv2 as cv

path = r"1919.jpeg"

# fetch image
image = cv.imread(path)
# Output image shapes
# Notice the output tuple (rows,cols,dtype)
print(image.shape)
# cv.imshow("image", image)
# Capture the Horn area
horn = image[60:100.180:240]
# cv.imshow("horn", horn)
# Box select corner area
# image = cv.rectangle(image, (180, 55), (240, 105), (0, 0, 255), thickness=2)
# print(horn.shape)
# Copy a horn
# image[60:100, 120:180] = image[60:100, 180:240]
# You can move ROI to another part as long as the shape and size are the same
# image[100:140, 220:280] = horn
# Transform the Angle to grayscale
horn = cv.cvtColor(horn, cv.COLOR_BGR2GRAY)
# Grayscale image is being converted to BGR image
back_horn = cv.cvtColor(horn, cv.COLOR_GRAY2BGR)
# image[250:370, 250:350] = back_horn
image[60:100.180:240]  = back_horn

cv.imshow("Region Of Interest(ROI)", horn)
cv.imshow("horn", image)

cv.waitKey()
cv.destroyAllWindows()
Copy the code

The above code, you can be in the process of learning to unlock their own access. The important parts are already in the notes.

After obtaining the Angle diagram, we conducted binarization operation on it, and the specific code is as follows

import cv2 as cv

path = r"1919.jpeg"

# fetch image
image = cv.imread(path)
# Output image shapes
# Notice the output tuple (rows,cols,dtype)
print(image.shape)
# cv.imshow("image", image)
# Capture the Horn area
horn = image[60:100.180:240]

# Transform the Angle to grayscale
gray_horn = cv.cvtColor(horn, cv.COLOR_BGR2GRAY)

# Binarization image of the horn was obtained
thresh, mask = cv.threshold(gray_horn, 160.255, cv.THRESH_BINARY)

# Get invert image of mask
mask_inv = cv.bitwise_not(mask)
# cv.imshow("mask", mask)


# Open a colorful candy picture
colors = cv.imread(r"colors.jpg")
# Select a piece of ROI of the same size as the mask
rows, cols = mask.shape[:2]
pie_colors = colors[10:10+rows, 20:20+cols]
print(pie_colors.shape)
print(mask.shape)
cv.imshow("pie_colors", pie_colors)


# Select the horns
img1_bg = cv.bitwise_and(horn, pie_colors, mask=mask)
# cv.imshow("img1_bg", img1_bg)
img2_fg = cv.bitwise_and(horn, horn, mask=mask_inv)
# cv.imshow("img2_fg", img2_fg)
# The two horns merge together
add_img = image.copy()  # Copy the original image
dst = cv.addWeighted(img1_bg, 0.3, img2_fg, 1.0)
# cv.imshow("dst", dst)
add_img[60:100.180:240] = dst
# image[60:100, 180:240] = back_horn

cv.imshow("origin", image)
cv.imshow("new", add_img)

cv.waitKey()
cv.destroyAllWindows()
Copy the code

The above code, to understand the difficulty of writing, this stage first try to understand can be (eraser in writing, also dizzy, always feel the lack of some knowledge, did not understand)

With the previous learning of binarization operation, with gray map, but also with the image overlay.

It is also possible that the case selection was not good, and the final picture after the replacement of the horns was ugly, as shown in the picture below.

There seems to be a lack of fusion. You may have a deeper understanding of this when you have more comprehensive knowledge.

The effect is ugly, but it can operate the designated area.

The above code also uses the following logical operations to understand the meaning first and then gradually expand the content.

cv2.bitwise_or(src1, src2,mask)

Function: Performs “or” operations on binary values at the same position on different channels (the result is 1 if one of the corresponding positions is 1).

cv2.bitwise_not()

Function: reverse by bit

cv2.bitwise_xor(src1,src2,dst,mask)

Effect: Output xor of two images DST = src1 ^ src2

The test code is as follows:

import cv2
import numpy as np
from matplotlib import pyplot as plt

src1 = cv2.imread('18_1.jpg')
src2 = cv2.imread('18_2.jpg')
d_and = cv2.bitwise_and(src1, src2)
d_or = cv2.bitwise_or(src1, src2)
d_not = cv2.bitwise_not(src1)
d_xor = cv2.bitwise_xor(src1, src2)

title = ['src1'.'src2'.'and'.'or'.'not'.'xor']
images = [src1, src2, d_and, d_or, d_not, d_xor]
for i in range(6):
    plt.subplot(2.3, i+1)
    plt.imshow(cv2.cvtColor(
        images[i], cv2.COLOR_BGR2RGB), 'gray'), plt.title(title[i])
plt.show()
Copy the code

The code looks like this after it runs:

In the next blog post, I will use the previous content, plus the logical operation of pixels to complete a “button effect”.

After learning the effect of today, I found that THERE are omissions in the places I have learned before. In the following several blogs, based on the knowledge I have learned, I will consolidate it for a few days.

OpenCV end

An hour has passed. Have you mastered the Python OpenCV related knowledge points?

In your spare time, you can subscribe to eraser’s crawler course to learn crawler knowledge.

If you have ideas or techniques you’d like to share, feel free to leave them in the comments section.


Blogger ID: Dream eraser, hope you like, comment, favorites.