Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the commonly used image geometric transformations in OpenCV

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you the "commonly used image geometric transformation in OpenCV what are", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn about "OpenCV commonly used image geometric transformation in which there are" this article.

0 program environment and functions learned

In this chapter, the program needs to import the following three libraries and define a function to display the image.

Learned function

# # zooming in and out of cv.resize (img,dsize, [interpolation]) # # Translational transform M = np.array ([[...]], dtype=np.float32) cv.warpAffine (img, M, dsize) # # Mirror transform cv.flip (img, 1) # Vertical Mirror cv.flip (img, 0) # horizontal Mirror cv.flit (img,-1) # horizontal and Vertical simultaneously # # rotation transform M = cv.getRotationMatrix2D (center, angle) Scale) img_rotate = cv.rotate (img, cv.ROTATE_90_CLOCKWISE) # # Perspective transformation M = cv.getPerspectiveTransform (src, dst) img = cv.warpPerspective (img, M, dsize) 1 crop, zoom in and out

Read in image

Img = cv.imread ('pic/rabbit500x333.jpg') show (img)

Display

Cropping: array selection method (colon)

# crop rabbit = img [150 rabbit 450:] # limit the number of rows, columns and three-channel show (rabbit)

Display

Zoom in and out: resize () function

Interpolation method

Program realization

# Zoom in and out # cv.resize (img,dsize, [interpolation]) dsize indicates size. [interpolation] is an interpolation method. Optional, there is a default value img2 = cv.resize (img, (500400)) # zoomed in to a width of 500H and 40cm using a defined interpolation method # generally speaking, select the LINEAR method for magnifying and shrinking the AREA method img3 = cv.resize (img, (500400), interpolation=cv.INTER_NEAREST) show (np.hstack ([img2,img3]))

Display

2 translation transformation

Derivation of principle and translation matrix

Read in image

Img = cv.imread ('pic/rabbit500x333.jpg') show (img)

Display

Program realization

# M=np.array ([[...]], dtype=np.float32) # cv.warAffine (img,M,dsize) cv image affine transformation function, M is the upper matrix, and dsize is the output image size M=np.array ([[1d0100], [0d1J 50]], dtype=np.float32) # translate 100 pixels horizontally to the right and 50 pixels vertically down For the principle, see the theoretical part img2 = cv.warpAffine (img,M, (333500)) show (img2)

Display

3 miscut transformation

Principle and derivation of miscut matrix

Read in image

Img = cv.imread ('pic/rabbit500x333.jpg') show (img)

Display

Horizontal miscut

M = np.array ([[1jue 0.2d0], [0dl 1d0]], dtype=np.float32) img3 = cv.warpAffine (img,M, (533500)) show (img3)

Display

Vertical staggering

M = np.array ([[1mem0rect 0], [0.3 meme 1d0]], dtype=np.float32) img3 = cv.warpAffine (img,M, (333700)) show (img3)

Display

4 Mirror transform

Principle, derivation of mirror matrix

Read in image

Img = cv.imread ('pic/rabbit500x333.jpg') show (img)

Display

Horizontal mirroring

Mx = np.array ([[- 1rect 0333], [0meme 1d0]], dtype = np.float32) img2 = cv.warpAffine (img,Mx, (333500)) # affine transformation function show (img2)

Display

Vertical mirroring

My = np.array ([[1je 0jue 0], [0je mi 1500]], dtype=np.float32) img3 = cv.warpAffine (img,My, (333500)) show (img3)

Display

Opencv built-in function to realize Mirror Transformation

# Vertical Image cv.flip (img,1)

# horizontal image cv.flip (img,0)

# cv.flip horizontally and vertically (img,-1)

Program realization

Img4 = cv.flip (img,1) # Vertical Mirror img5 = cv.flip (img,0) # horizontal Mirror img6 = cv.flip (img,-1) # horizontal Vertical Mirror while show (np.hstack ([img4,img5,img6]))

Display

5 rotation transformation

Principle, derivation of rotation matrix

Read in image

Img = cv.imread ('pic/rabbit500x333.jpg') show (img)

Display

Image rotation

Beta = np.pi/4# rotation matrix M = np.array ([[np.cos (beta), np.sin (beta), 0], [- np.sin (beta), np.cos (beta), 0]], dtype=np.float32) img2 = cv.warpAffine (img,M, (633300)) show (img2)

Display

Opencv gets the rotation matrix function built-in:

M = cv.getRotationMatrix2D (center,angle,scale)

Center is the center of rotation, angle is the rotation angle, and scale means to zoom in or out.

Use the above function to obtain the rotation matrix and realize the image rotation

It is convenient to set the rotation center M2 = cv.getRotationMatrix2D ((wAccord 2) cv.getRotationMatrix2D 2), 45 img3 1) img3 = cv.warpAffine (img,M2, (533500)) # Affine function to realize show (img3)

Display

Implementation of Image rotation function in opencv

Img_rotate = cv.rotate (img,cv.ROTATE_90_COUNTERCLOCKWISE)

You can only rotate in multiples of 90 degrees.

Program realization

# rotate 90 degrees counterclockwise img_rotate = cv.rotate (img,cv.ROTATE_90_COUNTERCLOCKWISE) show (img_rotate)

Display

6 perspective transformation

M = cv.getPerspectiveTransform (str,dst)

Str: original image matrix endpoint position, dst: target image matrix location

Img2 = cv.warpPerspective (img,M, (wjinh))

Read in image

Img = cv.imread ('pic/parthenon500x750.jpg') show (img)

Display

Program realization

# locate four points in the original image, and here you are looking for the approximate position of the four points in front of the column Str = np.array found by eye observation ([[210 img.shape 50], [610270], [650470], [150450], dtype=np.float32) # Matrix dst = np.array in the target image ([[150,505050], [6505.50], [650470], [150470], dtype=np.float32) the perspective transformation of a similar rectangle is pulled into a rectangle M = cv.getPerspectiveTransform (str). Dst) img2 = cv.warpPerspective (img,M, (wjime h)) show (img2)

Display

Application: Lane detection, picture correction

7 nearest neighbor interpolation and bilinear interpolation

Principle:

Nearest neighbor interpolation icon:

Bilinear interpolation diagram

Read in image

Img = cv.imread ('pic/rabbit50x33.jpg') show (img)

Display

Program realization

Img1 = cv.resize (img, (330500), interpolation=cv.INTER_NEAREST) # nearest neighbor interpolation img2 = cv.resize (img, (330500), interpolation=cv.INTER_LINEAR_EXACT) # accurate bilinear new interpolation show (np.hstack ([img1,img2]))

Display

It can be seen that the nearest neighbor interpolation is still relatively fuzzy, and the transition result is not smooth with bilinear interpolation.

These are all the contents of the article "what are the geometric transformations commonly used in OpenCV?" Thank you for your reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report