Implementation Principle
PS diffusion effect can produce a similar effect of hairy glass texture, so that the picture is somewhat hairy. Its implementation can be achieved by manipulating the pixel three-channel numerical value, define a random number device, the value of any point in the image is assigned to the value of the point as the center of a certain size of the window (such as 3 * 3,5 * 5) within a random point of value.
Function Code
// Diffusion effect cv::Mat Diffusion(cv::Mat src,int size) { int row = ; int col = ; RNG rng; cv::Mat result = (); for (int i = size; i < row- size; ++i) { uchar* t = <uchar>(i); for (int j = size; j < col- size; ++j) { int tmp = (0, 2*size+1); t[3 * j] = <uchar>(i - size + tmp / (2 * size + 1), 3 * (j - size + tmp % (2 * size + 1))); t[3 * j + 1] = <uchar>(i - size + tmp / (2 * size + 1), 3 * (j - size + tmp % (2 * size + 1)) + 1); t[3 * j + 2] = <uchar>(i - size + tmp / (2 * size + 1), 3 * (j - size + tmp % (2 * size + 1)) + 2); } } return result; }
C++ Test Code
#include <iostream> #include <string> #include <opencv2/> using namespace std; using namespace cv; cv::Mat Diffusion(cv::Mat src, int size); int main() { cv::Mat src = imread(""); 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 = ; int col = ; RNG rng; cv::Mat result = (); for (int i = size; i < row- size; ++i) { uchar* t = <uchar>(i); for (int j = size; j < col- size; ++j) { int tmp = (0, 2*size+1); t[3 * j] = <uchar>(i - size + tmp / (2 * size + 1), 3 * (j - size + tmp % (2 * size + 1))); t[3 * j + 1] = <uchar>(i - size + tmp / (2 * size + 1), 3 * (j - size + tmp % (2 * size + 1)) + 1); t[3 * j + 2] = <uchar>(i - size + tmp / (2 * size + 1), 3 * (j - size + tmp % (2 * size + 1)) + 2); } } return result; }
test effect
Figure 1 Original diagram
Figure 2 5*5 diffusion
Figure 3 11*11 diffusion
Adjusting the SIZE appropriately can change the intensity of the diffusion, which in turn brings out a different texture.
If there is any function that can be improved and perfected, you are very welcome to point it out, and why not make progress together?
To this article on the OpenCV-PS diffusion hairy glass effect is introduced to this article, more related OpenCV hairy glass content, please search for my previous articles or continue to browse the following related articles I hope you will support me more in the future!