How to realize the effect of PS Diffusion ground Glass by OpenCV
This article is about how OpenCV achieves the ground glass effect of PS diffusion. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Realization principle
The diffusion effect of PS can produce a texture similar to ground glass, making the picture feel hairy. It can be realized by manipulating the three-channel values of pixels, defining a random numerator, assigning the value of any point in the image to the value of a random point in a window of a certain size (such as 3-3-5-5).
Function code
/ / Diffusion effect cv::Mat Diffusion (cv::Mat src,int size) {int row = src.rows; int col = src.cols; RNG rng; cv::Mat result = src.clone (); for (int I = size; I < row- size; + + I) {uchar* t = result.ptr (I); for (int j = size; j < col- size) + + j) {int tmp = rng.uniform (0, 2*size+1); t [3 * j] = src.at (I-size+ tmp / (2*size+1), 3 * (j-size+ tmp% (2*size+1) T [3 * j + 1] = src.at (I-size + tmp / (2 * size + 1), 3 * (j-size + tmp% (2 * size + 1) + 1); t [3 * j + 2] = src.at (I-size + tmp / (2 * size + 1), 3 * (j-size + tmp% (2 * size + 1)) + 2) }} return result;}
C++ test code
# include # include # include using namespace std;using namespace cv; cv::Mat Diffusion (cv::Mat src, int size); int main () {cv::Mat src = imread ("test.jpg"); int size = 5; cv::Mat result = Diffusion (src,size); cv::imshow ("original", src); cv::imshow ("result", result); waitKey (0); return 0 } / / Diffusion effect cv::Mat Diffusion (cv::Mat src,int size) {int row = src.rows; int col = src.cols; RNG rng; cv::Mat result = src.clone (); for (int I = size; I < row- size; + + I) {uchar* t = result.ptr (I); for (int j = size J < col- size; + + j) {int tmp = rng.uniform (0, 2*size+1); t [3 * j] = src.at (I-size+ tmp / (2*size+1), 3 * (j-size+ tmp% (2*size+1) T [3 * j + 1] = src.at (I-size + tmp / (2 * size + 1), 3 * (j-size + tmp% (2 * size + 1) + 1); t [3 * j + 2] = src.at (I-size + tmp / (2 * size + 1), 3 * (j-size + tmp% (2 * size + 1)) + 2) }} return result;}
Test effect
Fig. 1 original picture
Fig. 2 5 Diffusion
Fig. 3 11-11 diffusion
Proper adjustment of size can change the intensity of diffusion and bring different texture.
Thank you for reading! This is the end of this article on "how to achieve PS diffusion ground glass effect in OpenCV". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!