21, image smoothing, median filtering
The neighboring pixels are arranged by size, and the values that are arranged in the middle of the set of pixels are used as the median filtered pixel values.
Medianblur function
Dst=cv2.medianblur (src,ksize) src, source file, Ksize, core size, must be more than 1 odd, such as 3,5,7, etc.
R=cv2.medianblur (o,3)
22, morphological transformation, image corrosion
Morphological transformation is mainly for the two-value image, the image is only 0 and 12 elements of the value, 0 is black, 1 is white. The so-called corrosion is aimed at its foreground color, that is, those pixel points with a value of 1 pixels, corrosion is white. Two input objects. Object 1: Two values the original image. Object 2: Convolution kernel, key array. The center point of the convolution core scans the original image one pixel at a time, and the pixels in the original image are scanned, and the value is only 1 if the value of the element corresponding to the convolution core is 1 o'clock, otherwise the value is 0.
function Erode
Dst=cv2.erode (SRC, kernel, iterations) DST, processing result src, source image kernel, convolutional kernel iterations, iteration count, by default, the number of iterations is 1 and can be repeated as needed
Kernel=np.ones ((5,5), np.uint8) ones means we're going to generate an array of 5 rows and 5 columns with 1 in it, np.uint8 representing the data type integral type
Import Cv2
Import NumPy as NP
O=cv2.imread ("Image\\erode.bmp", Cv2. imread_unchanged)
Kernel=np.ones ((5,5), np.uint8)
Erosion=cv2.erode (o,kernel,iterations=9)
Cv2.imshow ("original", O)
Cv2.imshow ("erosion", erosion)
23, morphological transformation, image expansion
The inverse operation of the corrosion operation, the image is corroded, the noise is removed, but the image is compressed. The corroded image can be expanded to remove noise and maintain the original shape. The center point of the convolution core scans the original image one pixel at a time, and the pixel in the original image is scanned, when the value of the element corresponding to the convolution core is as long as 1 o'clock, the value is 1, otherwise the value is 0.
function dilate
DST = cv2.dilate (src, kernel, iterations)
Kernel = Np.ones ((5,5), np.uint8) np.uint8 represents an unsigned 8-bit integer with a minimum value of 0 and a maximum of 8 powers of 2
dilation = cv2.dilate (O,kernel)
24, morphological transformation, open operation
The first corrosion and re-expansion is to remove the noise from the original image and keep the original image unchanged. Open operation (image) = expansion (corrosion (image))
function Morphologyex, morphological extension function, has encapsulated different operations, using different parameters to achieve the corresponding effect
opening = Cv2.morphologyex (IMG, Cv2. Morph_open, kernel) opening, open operation result img, source image cv2. Morph_open, open operation kernel convolution core (the same front of the corrosion expansion convolution core)
K = Np.ones ((5,5), np.uint8)
R = Cv2.morphologyex (O,cv2. MORPH_OPEN,K)
25, morphological transformation, image closed operation
The first expansion, after corrosion, it helps to close the foreground object inside the small hole, or the small black spots on the object. Closed operation (image) = corrosion (image), the larger the convolution core kernel array, the more obvious the effect.
function Morphologyex
closing = Cv2.morphologyex (IMG, Cv2. Morph_close, kernel) cv2. Morph_close, closed operation
K = Np.ones ((5,5), np.uint8)
R = Cv2.morphologyex (O,cv2. MORPH_CLOSE,K)
26, morphological transformation, image gradient
The original image, to get the original image of the expanded image and the original image of the corrosion image, with the expansion of the image-corrosion image, get contour image. Gradient (image) = expansion (image)-Corrosion (image)
function Morphologyex
result = Cv2.morphologyex (IMG, Cv2. Morph_gradient, kernel) cv2. Morph_gradient, gradient
K = Np.ones ((5,5), np.uint8)
R = Cv2.morphologyex (O,cv2. MORPH_CLOSE,K)
27, morphological conversion, image hat (top hat tophat)
Hat image = Original image-Open operation image, get noise image. Hat (image) = image-Open operation (image)
function Morphologyex
result = Cv2.morphologyex (IMG, Cv2. Morph_tophat, kernel) cv2. Morph_tophat, Hat
K = Np.ones ((5,5), np.uint8)
R = Cv2.morphologyex (O,cv2. MORPH_CLOSE,K)
28, morphological conversion, image black Hat
Black hat image = closed operation Image-the original image, resulting in a small hole inside the image, or a small black dot in the foreground color
function Morphologyex Cv2. Morph_blackhat, Black Hat
K = Np.ones ((5,5), np.uint8)
R = Cv2.morphologyex (O,cv2. MORPH_CLOSE,K)
29, image gradient, Sobel operator
Calculating gradients in different directions G = | Gx| + | gy|
-1 0 1 p1 p2 p3
Gx =-2 0 2 * P4 P5 P6
-1 0 1 P7 P8 p9
p5x = (P3-P1) + (P6-P4) + (P9-P7) The right side of this column minus the left column, the greater the difference, indicating that this is a boundary
-1-2-1 P1 p2 p3
Gy = 0 0 0 * P4 P5 P6
1 2 1 P7 P8 p9
p5y = (P7-P1) + (P8-P2) + (P9-P3) below this column minus the upper column, the greater the difference, it means that this is a boundary
The overall gradient value first computes the square and then computes the square root: g= (gx^2 +gy^2) Open square root in order to simplify the direct use of G = | Gx| + | gy| That directly computes the sum of the absolute values of the gradients in two directions.
P5sobel = |p5x|+|p5y|, this is for P5 point, in fact, every point is calculated in this way.
30. Image gradient, Sobel operator function and its use
DST = Cv2. Sobel (SRC, ddepth, dx, DY, [ksize]) DST, calculated results src, original image ddepth, processing result image depth, typically, you can set the value of this parameter to-1, so that the processing result is consistent with the original image Dx,x axis direction, calculates the X-direction boundary dy,y axis direction, calculates the Y-direction boundary ksize, the kernel size
Calculate Sobel result, calculate dx and dy to add separately
Calculates the gradient of the y-axis direction, dx=0,dy=1 the x-axis to the opposite.
White image pixel 255, black pixel is 0. The horizontal gradient on the left and right side of the difference is 0 is not a boundary, the difference is not 0 is the boundary, the boundary value is negative processing is 0 calculation, so the boundary is absolute.
DST = Cv2.convertscaleabs (src [, alpha[, Beta]]) #转回unit8
DST = cv2.addweighted (Src,alpha,src2,beta,gamma)
Cv2. cv_64f
Opencv+python (2)