About Opencv ③

Notes about Opencv3 series

Basically, it is a thing while checking the official document.

About memos About Opencv ① About Opencv②

The environment and images used are the same as last time.


① Resize

cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]]) Image of cv2.resize (1st argument, 2nd argument, 3rd argument)

Normal time of the image you are using

opencv.py


#Normal image
img = cv2,imread(cap_dir)
img.shape
>>>(340,255,3)

As an Opencv tutorial

opencv official.py


import cv2
import numpy as np

img = cv2.imread(cap_dir)
res = cv2.resize(img,None,fx=1, fy=1, interpolation = cv2.INTER_CUBIC)

img: Display image None: unused fx = 2: How many times x should be multiplied with the original image fy = 2: How many times y is multiplied by the original image interpolation = cv2.INTER_CUBIC: Interpolation method when enlarging the image

Or

opencv official.py


import cv2
import numpy as np

img = cv2.imread(cap_dir)

height, width = img.shape[:2]
res = cv2.resize(img,(2*width, 2*height), interpolation = cv2.INTER_CUBIC)

Capture the x and y values ​​of the image with img.shape [: 2] (2 * width, 2 * height) determines how many times the original size.

Both are doing the same thing. If you want to change the original image, click here. (Aspect ratio does not change)

If you want to zoom in / out without worrying about the aspect ratio

opencv.py


size = (300,200)
img_resize = cv2.resize(img,size)
#or
img_resize = cv2.resize(img,(300,200))

This alone is possible.

スクリーンショット (354).pngスクリーンショット(349).png The aspect ratio is super-appropriate. Rather than messing up after checking the aspect ratio of the image Is it smarter to capture with .shape [: 2]?


Method for the part described as the third argument

interpolation = method INTER_NEAREST INTER_LINEAR (default setting) INTER_AREA INTER_CUBIC INTER_LANCZOS4 There was quite a lot

When enlarging, interpolation = cv2.INTER_LINEAR (fast processing) or cv2.INTER_CUBIC (slow processing) interpolation = cv2.INTER_AREA when reduced Is it okay to recognize that ... Since it is written in English, detailed nuances cannot be read ...


② Image translation

opencv.py


img = cv2.imread(cap_dir)
h, w = img.shape[:2]
dx, dy = 30, 30

M = np.float32([[1,0,dx],[0,1,dy]])
img_afn = cv2.warpAffine(img,M,(w,h))

This is the process of moving the position of an object. Assuming that the amount of movement in the (x, y) direction is $ (t_x, t_y) $, the transformation matrix $ \ textbf {M} $ representing this translation is as follows:

M = \begin{bmatrix} 1 & 0 & t_x \\ 0 & 1 & t_y  \end{bmatrix}

by official

opencv.py


img.shape
>>>(340, 255, 3)

afn_mat = np.float32([[1,0,dx],[0,1,dy]])
print(afn_mat)
[[ 1.  0. 30.]
 [ 0.  1. 30.]]

np.float32 ([[1,0, dx], [0,1, dy]]) Set the amount of movement in the numpy array.

cv2.warpAffine (1st argument, 2nd argument, 3rd argument) First argument: image Second argument: amount of movement 3rd argument: Output image size ← There is a notation that an error will be thrown if the size is not officially specified

This will move the image. (I can't think of any use at this time)

スクリーンショット (354).pngスクリーンショット(357).png


③ Image rotation

opencv.py


img = cv2.imread(cap_dir,0)
h, w = img.shape

M = cv2.getRotationMatrix2D((w/2,h/2),45,1)
img_afn2 = cv2.warpAffine(img,M,(w,h))

cv2.getRotationMatrix2D((w/2,h/2),45,1) (w/2, h/2): Center of rotation position. If you change the value, the center position will shift. 45: Rotation angle. 1: Magnification. When set to 2, the image is doubled without changing the display frame.

スクリーンショット (360).png

Color display is possible without any problem, If you use color (vertical, horizontal, color), the information will increase. Color images can be handled by increasing the number of img.shapes stored. It means h, w, c = img.shape.

Official says


The transformation matrix for rotating the image at the rotation angle $ \ theta $ is as follows.

M = \begin{bmatrix} cos\theta & -sin\theta \\ sin\theta & cos\theta   \end{bmatrix}

The rotation provided by OpenCV can be scaled at the same time, and the center position of the rotation can be changed. The transformation matrix that represents this transformation is as follows.

\begin{bmatrix} \alpha &  \beta & (1- \alpha )  \cdot center.x -  \beta \cdot center.y \\ - \beta &  \alpha &  \beta \cdot center.x + (1- \alpha )  \cdot center.y \end{bmatrix}

here:

\begin{array}{l} \alpha =  scale \cdot \cos \theta , \\ \beta =  scale \cdot \sin \theta \end{array}

There is a function called cv2.getRotationMatrix2D to calculate this transformation matrix. The following example is trying a transformation that rotates 90 degrees with respect to the center of the image without scaling.

cv2.getRotationMatrix2D ((w/2, h/2), 45,1) seems to rotate the image using the above formula.

Although it is officially described as above, I honestly do not understand the formula. Because it is the result of trying what numerical value touches and how the image changes I think I have no choice but to give it a try and understand it with images.


④ Summary

Summary as separate use / non-use There are quite a lot of image processing, so I will probably use something I will summarize it separately.

Recommended Posts

About Opencv ②
About Opencv ③
About Opencv ①
Summary about Python3 + OpenCV3
About LangID
About CAGR
About virtiofs
About python-apt
About Permission
About sklearn.preprocessing.Imputer
About gunicorn
About locale
About permissions
About axis = 0, axis = 1
About import
About numpy
About pip
About Linux
About numpy.newaxis
About endian
About Linux
About import
About Linux
About Linux
About Linux ①
About cv2.imread
About _ and __
About wxPython
[OpenCV] About the array returned by imread
Adjust OpenCV contrast
Notepad about TecoGAN
About python slices
Briefly about __name__
[OpenCV] Personal memo
About Docker Volume
[Linux] About export
About reference type
About Twitter scraping
About the test
I tried'Beauty'with OpenCV
Learn about programming
About Flask customization
Python2.7 + CentOS7 + OpenCV3
About variable scope. .. ..
About python yield
Notes about with
About python, class
About Linear Models
About Go functions
OpenCV3 installation battle
About pandas describe
About Kivy root
About Firestore timeout
About python inheritance
About python, range ()
About Confusion Matrix
[Linux] About PATH
About python decorators
Linux (about groups)
OpenCV Samples (Python)
[Note] openCV + python