Python OpenCV Getting Started basic image operations (6) __python

Source: Internet
Author: User
Tags sca

Content from Opencv-python tutorials own translation finishing

Target:
Gets the pixel value and modifies
Get Image Properties
Set Image Area
Segmenting and stitching Images

The above content is important to the NumPy library operation in Python, and it is important to use the NumPy library skillfully

gets the pixel value and modifies
The image value can be obtained by the coordinates of the rows and columns, and a BGR value is returned for a BGR image, and its gray value is returned for a grayscale image.

Import cv2
import numpy as NP

img=cv2.imread (' 2.jpg ')
px = img[50,50]
print (px)
px = img[50,50,0]# Get Blue value
print (px)
px = img[50,50,1] #获取green值
print (px)
px = img[50,50,2] #获取red值
print (px)

img[50,50] = [255,255,255]
print (img[50,50])

In the process of processing images, pixel-by-point operations are time-consuming and use matrix operations as much as possible.

The above method is usually used to select an area of the matrix, for pixel processing in the image, using the Array.item () and the Array.itemset () function is better, but the return value

Import cv2
import numpy as NP

img=cv2.imread (' 2.jpg ')
print (img[10,10)
Sca=img.item (10,10,0) # Get BGR Value
print (SCA)
Sca=img.item (10,10,1)
print (SCA)
Sca=img.item (10,10,2)
print (SCA)

Img.itemset (10,10,2) #给像素赋值

Get Image Properties
Image properties include number of rows, columns, channels, image data types, number of pixels, and so on

Img.shape can return basic information about an image

Import cv2
import numpy as NP

img=cv2.imread (' 2.jpg ')
print (Img.shape)

Number of rows, columns, number of channels, respectively
If the grayscale image returns only the number of rows and columns

Img.size returns the number of pixels of an image

Img.dtype returns the data type of the image
Because of the often inconsistent data in Python, the Dtype function is useful

Image Roi
Sometimes you need to find the location of a particular area in the picture, such as looking for the eye in the picture, first find the face, and then look for the eyes.

ROI is obtained using the numpy subscript, where the ball is selected and the ball is copied to another image


Copy a good result

Import cv2
import numpy as NP

img=cv2.imread (' 2.jpg ')
ball = img[280:340, 330:390]
img[273:333, 100:160] = Ball

segmenting and merging image channels

The BGR channel of a picture can be segmented into a single channel, and for each single channel it can be merged together to make up the BGR

Segmentation
B,g,r = Cv2.split (IMG)
Or use a slice in Python
b = img[:,:,0] #获取蓝色部分

Note that split is more time-consuming

add boundaries to an image (padding)
If you want to add a boundary to a picture, like a frame, you can use the Cv2.copymakeborder () function
But this feature has more value, such as convolution operations or 0 padding, and so on.
The parameters are as follows: src-enter picture top,bottom,left,right-boundary pixel number Bordertype-Add the boundary type, have the following selection, Cv2. Border_constant adding a constant value boundary with a color, you also need to add a parameter value; Cv2. Border_reflect boundary element mirroring, for example
Fedcba|abcdefgh|hgfedcb;cv2. Border_reflect_101 or CV2. Border_default, similar to the above, but with a slight change of gfedcb|abcdefgh|gfedcba;cv2. Border_replicate repeats the last element, such as AAAAAA|ABCDEFGH|HHHHHHH;CV2. Border_wrap (the author does not know how to express) for example CDEFGH|ABCDEFGH|ABCDEFG value boundary color, if the type of the border is cv2. Border_constant

Sample code

Import cv2
import matplotlib.pyplot as Plt
import numpy as np


BLUE = [255,0,0]

img1 = cv2.imread (' 2.jpg ')

replicate = Cv2.copymakeborder (img1,10,10,10,10,cv2. Border_replicate)
reflect = Cv2.copymakeborder (img1,10,10,10,10,cv2. Border_reflect)
reflect101 = Cv2.copymakeborder (img1,10,10,10,10,cv2. border_reflect_101)
wrap = Cv2.copymakeborder (img1,10,10,10,10,cv2. Border_wrap)
constant= cv2.copymakeborder (img1,10,10,10,10,cv2. Border_constant,value=blue)

Plt.subplot (231), Plt.imshow (IMG1, ' Gray '), Plt.title (' ORIGINAL ')
Plt.subplot (232), Plt.imshow (Replicate, ' gray '), Plt.title (' replicate ')
Plt.subplot (233), plt.imshow (reflect, ' Gray '), Plt.title (' reflect ')
Plt.subplot (234), Plt.imshow (reflect101, ' Gray '), Plt.title (' reflect_101 ')
Plt.subplot (235), plt.imshow (Wrap, ' gray '), Plt.title (' wrap ')
plt.subplot (236), Plt.imshow (Constant, ' Gray '), Plt.title (' CONSTANT ')

plt.show ()

Original:

The results are shown in the following illustration:

Matplotlib drawing, order is RGB, blue and red swap

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.