Introduction to PYTHON--OPENCV (i) __python

Source: Internet
Author: User
Tags cos sin
1, the structure of OpenCV

Like Python, the current OPENCV also has two large versions, OpenCV2 and OpenCV3. Compared to OPENCV2,OPENCV3 provides more features and more convenience features. However, considering the compatibility with the depth learning framework, and the difficulty of hands-on installation, this part is first introduced in 2.

Functional interfaces in OpenCV can generally be divided into the following sections, depending on the function and requirements:

Core: Kernel module, mainly contains the most basic structure (matrix, dot line and shape, etc.) in OpenCV, and the related basic operation/operation.

Imgproc: Image processing module, which contains basic functions related to image (filter, gradient, change size, etc.), as well as some derived advanced functions (image segmentation, histogram, morphological analysis and edge/line extraction, etc.).

Highgui: Provides basic functions of user interface and file reading, such as the generation and control of Image Display window, io of image/video files, etc.

If you do not consider video applications, the above three are the most core and commonly used modules. OPENCV also provides strong support for video and special visual applications:

Video: A common feature used for visual analysis, such as the optical flow method (optical) and target tracking.

Calib3d: three-dimensional reconstruction, stereo vision and camera calibration, and other related functions.

FEATURES2D: Features related to two-dimensional features, mainly some patent-protected, commercial-friendly feature point detection and matching functions such as Orb features.

Object: Target detection module, including cascading classification and latent SVM

ML: The machine learning algorithm module, which contains some of the most common traditional machine learning algorithms in vision.

Flann: Nearest neighbor Algorithm library, Fast library for approximate
Nearest neighbors is used for clustering and retrieval in multidimensional space, often matched with key points.

GPU: Contains some GPU-accelerated interfaces, and the bottom acceleration is cuda implementation.

Photo: Computational Photography (computational photography) related interface, of course, this is only a name, in fact, only image repair and noise reduction.

Stitching: Image mosaic module, with it can generate their own panoramic photos.

Nonfree: Some of the algorithms that are protected by patents are actually sift and surf.

Contrib: Some of the experimental properties of the algorithm are considered in the future version of the added.

Legacy: Literal heritage, meaning that some of the discarded interfaces are reserved for backward compatibility.

OCL: Some interfaces that use OpenCL to accelerate in parallel.

Superres: Super resolution module, is actually BTV-L1 (biliteral total VARIATION–L1
regularization) algorithm

Viz: the base of the 3D rendering module, in fact, the bottom is the famous 3D Toolkit VTK (visualization Toolkit).

From the point of view of use, the main changes in OpenCV3 are more features and finer module partitioning than OpenCV2. 2. PYTHON--OPENCV Foundation 2.1 Representation of images

The known single channel grayscale image in the computer representation is a 8-bit unsigned reshaping of the Matrix, in the ONCV C + + code, the image has a special structure cv::mat, but Python has numpy this powerful basic tool, so the matrix is represented by NumPy array, Multi-channel is red-green-blue (RGB) three channels.

2.2 Basic Processing

(1) Read image: Cv.imread ()

Can be read in different modes, usually the most commonly used to read a single channel grayscale, or directly read the default multi-channel

(2) Storage Image: Cv.imwrite ()

Import cv2
color_img=cv2.imread (' 4.jpg ')
print (Color_img.shape)

# reads single channel
gray_img=cv2.imread (' 4. JPG ', Cv2. Imread_grayscale)
print (gray_img.shape)

#把单通道图像保存后, read again, is still 3 channels, equivalent to a single channel copy to 3 channels to save
Cv2.imwrite (' Grayscale_4.jpg ', gray_img)
reload_grayscale=cv2.imread (' grayscale_4.jpg ')
print (Reload_grayscale.shape

# Specify JPG quality, range from 1~100, default 95, the higher the value the better, the larger the file
cv2.imwrite (' anglababy.jpg ', color_img, Cv2. imwrite_jpeg_quality,20))

(2) scaling, cutting, filling edge

Scaling: Im.resize ()

Cropping: Using array subscript interception to achieve

Import cv2
img=cv2.imread (' dog.jpg ')

# reduced to 200x200 square
img_200x200=cv2.resize (IMG, (200,200))

# Does not specify the scaled size directly, specify the scaling ratio through FX and FY, 0.5 for the width of each half
# interpolation method defaults to Cv2. Inter_linear, which is specified here as the nearest neighbor interpolation
img_half=cv2.resize (IMG, (0,0), Fx=0.5,fy=0.5,interpolation=cv2. inter_nearest)

# up and down each 50 pixel black edge
img_add=cv2.copymakeborder (img,50,50,0,0,cv2. Border_constant,value= (0,0,0))

# crop
patch_img=img[20:150,-180:-50]

cv2.imshow ("image", img_200x200)
cv2.imshow ("Img_half", Img_half) cv2.imshow ("Img_add",
img_add)
cv2.imshow ("Patch_img", patch_img )
Cv2.waitkey (0)

(3) Hue, shade, histogram, gamma curve

For example, you can adjust the hue and shade through the HSV space. The HSV space is made by the American graphic Science expert a. R. Smith proposes a color space in which the HSV is Hue (Hue), saturation (saturation), and lightness (Value). Adjusting in the HSV space avoids the direct adjustment in the RGB space and requires consideration of the three-channel dependencies. The value of h in OpenCV is [0, 180], and the other two channels are [0, 256], and the following example is followed by the example code, which adjusts the image through the HSV space:

Import cv2

img=cv2.imread (' mushroom.jpg ') to
transfer images from RGB to HSV
Img_hsv=cv2.cvtcolor (IMG,CV2) via Cv2.cvtcolor. COLOR_BGR2HSV)

# h space, the green is higher than the yellow value, so give each pixel +15, yellow will turn green
turn_green_hsv=img_hsv.copy ()
turn_green_hsv[:,:,0 ]= (turn_green_hsv[:,:,0]+15)
Turn_green_img=cv2.cvtcolor (Turn_green_hsv,cv2. COLOR_HSV2BGR)

cv2.imshow ("turn_green_img", turn_green_img)

# Reducing saturation can make the image lose color and become more gray
Colorless_hsv=img_ Hsv.copy ()
colorless_hsv[:,:,1]=0.5*colorless_hsv[:,:,1]
colorless_img=cv2.cvtcolor (COLORLESS_HSV, Cv2. COLOR_HSV2BGR)
cv2.imshow ("colorless_img", colorless_img)

# reduced to half the original
darker_hsv=img_hsv.copy ()
darker_hsv[:,:,2]=0.5*darker_hsv[:,:,2]
darker_img=cv2.cvtcolor (darker_hsv,cv2. COLOR_HSV2BGR)
cv2.imshow ("darker_img", darker_img)

cv2.waitkey (0)

Histogram: Convenient to the image of the pixel distribution to understand more clearly, low pixel value for the dark part of the high value represents a large part of the brightness, but the display may appear when the dark part of the details of the lack of detail or the light of the details of the loss of the situation.

Gamma transform: Enhance the dark detail, gamma transform is a corrective camera direct imaging and human eye perception of the difference between a common means, in simple terms, through nonlinear transformation of the image from the exposure intensity of the linear response to become more close to the human eye response.

Import NumPy as NP import CV2 import Matplotlib.pylab as plt from Mpl_toolkits.mplot3d import axes3d img=cv2.imread (' 4.jp G ') # Divide the channel to compute the histogram hist_b=cv2.calchist ([img],[0],none,[256],[0,256]) Hist_g=cv2.calchist of each channel ([img],[1],none,[256],[ 0,256]) hist_r=cv2.calchist ([img],[2],none,[256],[0,256]) # Gamma transform function def gamma_trans (Img,gamma): # First normalized to 1, then use gamma Find the new value as an exponent, and then restore Gamma_table=[np.power (X/255.0,gamma) *255.0 for x in range (256)] Gamma_table=np.round (Np.array able)). Astype (np.uint8) # uses the OpenCV look-up table function to implement the mapping return CV2.
LUT (img,gamma_table) # performs gamma transform, a value less than 1 allows for a large amount of dark detail to ascend, while the light detail is slightly elevated Img_corrected=gamma_trans (img,0.5) cv2.imshow ("IMG", IMG) Cv2.imshow ("img_corrected", img_corrected) # Cv2.waitkey (0) # sub-channel calculates gamma-corrected histogram hist_b_corrected=cv2.calchist ([img_ CORRECTED],[0],NONE,[256],[0,256]) hist_g_corrected=cv2.calchist ([img_corrected],[1],none,[256],[0,256]) hist_r_ Corrected=cv2.calchist ([img_corrected],[2],none,[256],[0,256]) # Histogram visualization fig=plt.figure () pix_hists=[[hist_b, Hist_g,hist_r], [hist_b_corrected,hist_g_corrected,hist_r_corrected]] Pix_vals=range (256) for sub_plt,pix_hist in Z IP ([121,122],pix_hists): Ax=fig.add_subplot (sub_plt,projection= ' 3d ') for c,z,channel_hist in zip ([' B ', ' G ', ' r ')],[20
               , 10,0],pix_hist): cs=[c]*256 ax.bar (pix_vals,channel_hist,zs=z,zdir= ' y ', color=cs,alpha=0.618, Edgecolor= ' None ', lw=0) Ax.set_xlabel (' Pixel Values ') Ax.set_xlim ([0,256]) Ax.set_ylabel (' channels ') ax.
 Set_zlabel (' Counts ') plt.show ()



(4) affine transformation

The affine transformation of image involves the change of the shape position angle of the image, which is a common function in the depth learning preprocessing. Affine transformation specific to the application of the image, mainly to the image of the scaling, rotation, shearing, flipping and moving the combination of the peace. In OpenCV, the matrix of the affine transformation is a 2x3 matrix in which the 2x2 matrix on the left is the linear transformation matrix, and the 2x1 on the right is the translational term:

For any position on the image (X,y), the affine transformation performs the following actions:

It should be noted that for the image, the width direction is x, the height direction is y, the coordinate order and image pixel correspond to subscript. So the origin position is not the lower left corner but the upper right corner, the y direction is not up, but downward. The affine transformation in OpenCV is accomplished by affine transformation matrices and Cv2.warpaffine () functions:

Import NumPy as NP
import cv2

img = cv2.imread (' dog.jpg ')

# Enlarge 1.6 times times along the transverse longitudinal axis, then translate ( -150,-240), and finally intercept along the original size, which is equivalent to clipping and amplifying
M_crop_dog = Np.array ([
    [1.6, 0, -150],
    [0, 1.6,-240]
], Dtype=np.float32)

# cv2.warpaffine (original image, transform matrix, transformed image size)
Img_dog = Cv2.warpaffine (img, M_crop_dog, )
cv2.imshow ("Img_dog", Img_dog)


# X-axis shearing transformation, counterclockwise rotation angle 15°
theta=15*np.pi/180
m_shear= Np.array ([
    [1,np.tan (Theta), 0],
    [0,1,0]
],dtype=np.float32)

img_sheared=cv2.warpaffine (img , M_shear, (400,600))
cv2.imshow ("img_sheared", img_sheared)

# Rotate clockwise, angle 15°
m_rotate=np.array ([
    [Np.cos (Theta),-np.sin (Theta), 0],
    [Np.sin (Theta), Np.cos (Theta), 0]
],dtype=np.float32)

im_rotate=cv2.warpaffine (Img,m_rotate, (400,600))
cv2.imshow ("Im_rotate", im_rotate)

# Rotate + zoom + Rotate combination to understand M=np.array by SVD decomposition
([
    [1,1.5,-400],
    [0.5,2,-100]
],dtype=np.float32)

Img_transformed=cv2.warpaffine (Img,m, (400,600))
cv2.imshow ("img_transformed", img_transformed)

Cv2.waitkey (0)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.