This is the 22nd day of my participation in the August Wen Challenge.More challenges in August

Python OpenCV 365 day learning plan, enter the graphics realm with eraser.

Foundation of basic knowledge

All the fuzzies I have learned in the previous blogs belong to convolution operations, and they all have one thing in common: after the blurring, the edge information of the image no longer exists.

The edge retention filtering algorithm (EPF) learned in this blog is a kind of fuzzy image that can be realized through convolution processing without causing damage to the image edge, and the image after the convolution operation can completely save the whole image edge.

Specific implementation is divided into: Gaussian bilateral filtering, mean transfer filtering

Principle is not in detail, belong to mathematical knowledge, first use, such as the need, in the inverse mathematical basic knowledge.

The first thing the eraser feels when he learns this is, “Give it up!” He doesn’t know where the content is used!

Function prototype introduction

Gaussian bilateral filtering

The prototype of bilateral filtering function is as follows:

dst = cv2.bilateralFilter(src, d, sigmaColor, sigmaSpace[, dst[, borderType]])
Copy the code

Parameter Description:

  • SRC: input image;
  • D: diameter range of each pixel neighborhood in the filtering process. If it is not positive, the function computes the value from sigmaSpace;
  • SigmaColor: The sigma value of the color space filter. The larger the parameter value is, the wider the colors in the pixel neighborhood will be mixed together, resulting in a larger semi-isochromatic area.
  • SigmaSpace: The sigma value of the filter in the coordinate space. If this value is large, it means that the more distant pixels will influence each other so that similar colors in a larger area are sufficient to obtain the same color. When d>0, d specifies the neighborhood size and is independent of sigmaSpace, otherwise d is proportional to sigmaSpace.
  • DST: An output image of the same size and format as the source image;

Generally make sigmaSpace larger and sigmaColor smaller to render better.

Advantages and disadvantages: Bilateral filtering can save image edge details well and filter out low-frequency noise, but bilateral filtering efficiency is not very high and takes longer time than other filters.

The test code is as follows, first master the basic use of the function again.

import cv2 as cv
import numpy as np

# Bilateral filtering
def bilater(image) :
    # the third parameter is bigger (color), the fourth parameter is smaller (space)
    dst = cv.bilateralFilter(image, 0.100.15)
    cv.namedWindow("dst")
    cv.imshow("dst", dst)

src = cv.imread("./123.jpg")
cv.namedWindow('src')
cv.imshow('src', src)

bilater(src)

cv.waitKey(0)
cv.destroyAllWindows()
Copy the code

Running effect, a little beauty skin effect, but the effect is not very good, it is estimated that the parameters of the problem.

Mean shift filtering

Mean transfer algorithm is a general clustering algorithm, and its basic principle is:

For a given number of samples, one of the samples is selected, and a circular region is defined with the sample as the center point. The centroid of the sample in the circular region, namely the point with the maximum density, is obtained, and the above iterative process is continued with this point as the center until the final convergence. (I don’t understand it very well, just have a general idea)

The function prototype is as follows:

dst = cv2.pyrMeanShiftFiltering(src, sp, sr[, dst[, maxLevel[, termcrit]]])
Copy the code

Parameter Description:

  • SRC: Input image, 8-bit 3-channel image;
  • Sp: radius of physical space of migration;
  • Sr: size of migrating color space radius;
  • DST: An output image of the same size and format as the source image, an optional parameter.
  • MaxLevel: Optional parameter, the maximum number of layers of the pyramid;
  • Termcrit: Optional parameter. The termination condition of the migration iteration can be set to the number of iterations satisfying termination, the iteration target and center point deviation satisfying termination, or a combination of the two.

Among all parameters, sp and sr are mandatory. The larger the value of sp and SR is, the more obvious the smoothing effect of image color will be, and the more time the function will take.

The test code is as follows:

import cv2 as cv
import numpy as np

# Bilateral filtering
def bilater(image) :
    # the third parameter is bigger (color), the fourth parameter is smaller (space)
    dst = cv.bilateralFilter(image, 0.100.15)
    cv.namedWindow("dst")
    cv.imshow("dst", dst)

# Mean shift filtering
def pyrmeanshift(src) :
    dst = cv.pyrMeanShiftFiltering(src, 10.50)
    cv.imshow("dst", dst)

src = cv.imread("./123.jpg")
cv.namedWindow('src')
cv.imshow('src', src)

pyrmeanshift(src)

cv.waitKey(0)
cv.destroyAllWindows()
Copy the code

Run effect, kind of like oil painting effect.

Eraser section

A friend recently sent a message to Eraser asking: “Eraser, can we learn OpenCV by calling functions without learning principles?”

“Rest assured, no problem, 365 days to go.”

Hang in there, Eraser. This form of learning is called “God’s Perspective learning.”

First use the simplest way, the knowledge point in OpenCV have been over, master the global concept, in detail inside each point

If every piece of knowledge is boring, every day is difficult, you will be hard to stick to it.

An hour has passed. Have you mastered the Python OpenCV related knowledge points?

As a beginner, there are many places to learn not in-depth, I hope you stick to it with me.

reading


Today is the 63/100 day of continuous writing. If you have ideas or techniques you’d like to share, feel free to leave them in the comments section.


Blogger ID: Dream eraser, hope you like, comment, favorites.