Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the effect of PS Diffusion ground Glass by OpenCV

Shulou Source: shulou.com Published: 2022-06-03 19:41:42 10月03日 Update

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!

Tags: Effect ground glass numerical value code content more article texture test good practical pixel function function original drawing principle image size intensity feeling Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Tech Info Shulou Information Shulou Technology OPPO Reno