These are equivalent to my study notes, so there is no strong structural and full introduction, please forgive me.
1 reading and writing images
Here is a brief example of loading images, printing dimensions, converting formats, and saving images as. png:
# -*-coding:utf-8-*- import Cv2import NumPy as NP
# read-in image im = Cv2.imread ( " .. /data/empire.jpg " ) # print image size h, w = Im.shape[:2 print H, W # The image in the original JPG format is a PNG format image Cv2.imwrite ( " . /images/ch10/ch10_p210_reading-and-writing-images.png , IM)
# Note: Imread default read is the RGB format, so even if the original image is a grayscale image, read out is still three channels, so after Imread can add parameters
# Note: Here is the relative path: \ and/is no difference, ' and ' is no difference ... /means return to the previous level directory,./indicates the same level directory as the source file.
# Note: the function imread () returns the image as a standard NumPy array.
1.1 Related NotesCv2.imread
Python: cv2. Imread (filename[, flags])
Cv2.imwrite
Python: cv2. Imwrite (filename, img[, params])
Parameters: |
- filename –name of the file.
- image –image to be saved.
- params –
Format-specific Save parameters encoded as PAIRS paramid_1, paramvalue_1, paramid_2, paramValue_2, . The following parameters is currently supported:
- For JPEG, it can be a quality ( cv_imwrite_jpeg_quality ) from 0 to (the higher is the better). Default value is 95.
- For PNG, it can being the compression level ( cv_imwrite_png_compression ) from 0 to 9. A higher value means a smaller size and longer compression time. Default value is 3.
- For PPM, PGM, or PBM, it can be a binary format flag ( cv_imwrite_pxm_binary ), 0 or 1. Default value is 1.
|
2 image RGB, HSV channel separation
# convert BGR to r,g,bb,g,r = cv2.split (IM)# Convert BGR to HSVimage_hue_saturation_ Value = Cv2.cvtcolor (IM, cv2. COLOR_BGR2HSV) h,s,v=cv2.split (image_hue_saturation_value)# Convert BGR to Gray Image_gray = Cv2.cvtcolor (IM, cv2. Color_bgr2gray)
Note: RGB channels are indexed in B G R which are different from Matlab.
# Note: Any channels could is split using cv2.split, pay attention to the sequence of channels
2.1 Related Notes
Python:   cv2. split ( m[, Mv) →mv
Parameters: |
- src –input multi-channel array.
- mv –output array or vector of arrays; In the first variant of the function the number of arrays must match src.channels (); The arrays themselves is reallocated, if needed.
|
Python: cv2. Cvtcolor (src, code[, DST[, DSTCN]]) →dst
Parameters: |
- SRC –input image:8-bit unsigned, 16-bit unsigned ( CV_16UC ... ), or single-precision floating-point.
- DST –output image of the same size and depth AS src .
- code –color Space Conversion code (see the description below ).
- DSTCN –number of channels in the destination image; if the parameter is 0, the number of the channels is derived automatically from src and code .
|
3 operation of the image matrix (point multiplication, copy, intercept, 1 to n-dimensional matrix)
# Mask Seed 3D Matrix
Seed_mask_single_channel_list = Np.array ([[[[1,0,0],[0,0,0],[0,0,0]],[[0,1,0],[0,0,0],[0,0,0]],[[0,0,1],[0,0,0],[ 0,0,0]],
[[0,0,0],[1,0,0],[0,0,0]],[[0,0,0],[0,1,0],[0,0,0]],[[0,0,0],[0,0,1],[0,0,0]],
[[0,0,0],[0,0,0],[1,0,0]],[[0,0,0],[0,0,0],[0,1,0]],[[0,0,0],[0,0,0],[0,0,1]])
# Cut Image Image_new_sample = image_source[:200,:200#mask_singel_channel = Np.tile (seed_mask_ Single_channel_list[1], (70,70)) [: 200,:200= mask_singel_channel * Image_new_sample #表示点乘
# Note: The operation of the matrix is performed using the NumPy class library.
3.1 Related Notes
NumPy. Array (object, dtype=none, copy=true, order=none, subok= False, ndmin=0)
Parameters: |
Object : Array_like
An array, any object exposing the array interface, a object whose __array__ method returns an array, or any (nested) sequ ence.
dtype : data-type, optional
The desired data-type for the array. If not given, then the type is determined as the minimum type required to the objects in the sequence. This argument can is used to ' upcast ' the array. For downcasting, use the. Astype (t) method.
copy : bool, optional
If true (default), then the object is copied. Otherwise, a copy would only be made if __array__ returns a copy, if obj was a nested sequence, or if a copy is needed to SA Tisfy any of the other requirements (Dtype, order, etc).
order : {' C ', ' F ', ' A '}, optional
Specify the order of the array. If order is ' C ' (default) and then the array would be in c-contiguous order (Last-index varies the fastest). If order is ' F ', then the returned array would be in fortran-contiguous order (First-index varies the fastest). If order is ' A ' and then the returned array could be in any order (either C, fortran-contiguous, or even discontiguous).
Subok : bool, optional
If True, then sub-classes'll be Passed-through, otherwise the returned array would be forced to be a base-class array (de Fault).
ndmin : int, optional
Specifies the minimum number of dimensions that the resulting array should has. Ones'll is pre-pended to the shape as needed to meet this requirement.
|
Returns: |
out: Ndarray
An array object satisfying the specified requirements.
|
e.g. the outermost layer is always [], so if it is a 1-dimensional one [],2 dimension is 2, n-Dimension is n
>>> Np.array ([1, 2, 3]) array([1, 2, 3])>>> Np.array ([[1, 2], [3, 4]]) array ([[[
1, 2], [3, 4]])>>> Np.array ([1, 2, 3], ndmin=2) Array ([[1, 2, 3]])
numpy. tile ( A , reps )
Parameters: |
A : array_like
The input array.
Reps : array_like
The number of repetitions of A along each axis.
|
Returns: |
C : Ndarray
The tiled output array
|
e.g.
>>> B = Np.array ([[1, 2], [3, 4]])>>> np.tile (b, 2) Array ([[1, 2, 1, 2], [3, 4, 3, 4]])>>> np.tile (b, (2, 1)) array ([[1, 2], [3, 4], [1, 2< c10>], [3, 4]])
Python + OpenCV2 Series: 2-picture manipulation