opencv-python learning (eight): picture cutting, merging

The principle is to obtain or combine images at specified positions by manipulating the image matrix

Capture a specified area in a picture or add a picture in a specified area

code show as below:

# 引入包
import cv2 as cv

def jie_image(src1):
    src2 = src1[369:637, 572:922] # 截取指定的区域
    cv.namedWindow("splice", cv.WINDOW_NORMAL)
    cv.imshow("splice", src2)
    src1[169:437, 572:922] = src2 # 指定位置填充,大小要一样才能填充
    cv.namedWindow("merge", cv.WINDOW_NORMAL)
    cv.imshow("merge", src1)

src = cv.imread("./static/image/blur.jpg")
cv.namedWindow("oldImage", cv.WINDOW_NORMAL)
cv.imshow("oldImage", src)
jie_image(src)
cv.waitKey(0)
cv.destroyAllWindows()

Code explanation:
Let's analyze it according to this picturesrc1[369:637, 572:922]meaning.

0:375 refers to intercepting 369~637 from the vertical direction
240:480 refers to intercepting 572~922 from the horizontal direction
where the origin is the upper left corner of the picture

∣−−--------369,637 − − − − − − − − − − − − − − − − − >x
∣            ∣               ∣                  
∣            ∣               ∣                
∣            ∣               ∣               
∣            ∣− − − − − − −572,922
∣ 
∨ 
y 

Of course, we can also use slices to select our region of interest (ROI, Region of Interest)

PS. At first, I found it puzzling that xy is reversed here, but in fact, this is because the concept of two-dimensional ndarray index is not clear. The two-dimensional ndarray index is actually the same as the two-dimensional array index in C++/Java. It finds the row first, and then finds the column. It won't feel strange to understand this way. To be more specific, it is actually finding the first axis and then finding the second axis. The concept of axis can refer to the first three paragraphs of The Basics. y represents the first axis, and x represents the second axis. They always find the first axis and then the second axis.

src1[169:437, 572:922] = src2
Here [169:437, 572:922] should be the same size as the image in src2, otherwise an error will be reported

operation result:
insert image description here

Guess you like

Origin blog.csdn.net/weixin_33538887/article/details/118385008