Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

The principle of Grayscale World algorithm and example Analysis of C++ implementation

Shulou Source: shulou.com Published: 2022-06-01 23:38:13 10月03日 Update

This article introduces the principle of grayscale world algorithm and the example analysis of C++ implementation, the content is very detailed, interested friends can refer to, I hope it can be helpful to you.

Pre-content

The human visual system has color constancy, which can obtain the invariant characteristics of the surface color of the object from the changing lighting environment and imaging conditions, but the imaging equipment does not have such an adjustment function. Different lighting environments will lead to a certain degree of deviation between the captured image color and the real color, so it is necessary to choose an appropriate color balance algorithm to eliminate the influence of lighting environment on color display.

Principle of grayscale world algorithm

The grayscale world algorithm is based on the grayscale world hypothesis, which assumes that for an image with a large number of color changes, the average value of RGB3 components tends to the same gray value Gray. In the physical sense, the grayscale world algorithm assumes that the average reflection of natural scenery to light is a fixed value as a whole, which is approximately "gray". The color balance algorithm forces this assumption to be applied to the image to be processed, which can eliminate the influence of ambient light from the image and obtain the original scene image.

Algorithm step

Advantages and disadvantages of algorithm

This algorithm is simple and fast, but when the color of the image scene is not rich, especially when there are a large number of monochromatic objects, the algorithm will fail.

Source code implementation

Mat GrayWorld (Mat src) {vector bgr; cv::split (src, bgr); double B = 0; double G = 0; double R = 0; int row = src.rows; int col = src.cols; Mat dst (row, col, CV_8UC3); for (int I = 0; I

< row; i++) { for (int j = 0; j < col; j++) { B += 1.0 * src.at(i, j)[0]; G += 1.0 * src.at(i, j)[1]; R += 1.0 * src.at(i, j)[2]; } } B /= (row * col); G /= (row * col); R /= (row * col); printf("%.5f %.5f %.5f\n", B, G, R); double GrayValue = (B + G + R) / 3; printf("%.5f\n", GrayValue); double kr = GrayValue / R; double kg = GrayValue / G; double kb = GrayValue / B; printf("%.5f %.5f %.5f\n", kb, kg, kr); for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { dst.at(i, j)[0] = (int)(kb * src.at(i, j)[0]) >

(int) (kb * src.at (I, j) [0]); dst.at (I, j) [1] = (int) (kg * src.at (I, j) [1]) > 255255: (int) (kg * src.at (I, j) [1]) Dst.at (I, j) [2] = (int) (kr * src.at (I, j) [2]) > 255255: (int) (kr * src.at (I, j) [2]);}} return dst;}

Effect picture

Conclusion

We can see that the grayscale world algorithm has the effect of white balance, and the execution speed of the algorithm is very fast.

This is the end of the example analysis on the principle of the grayscale world algorithm and the implementation of C++. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Algorithm Grayscale World Color Image Environment principle Illumination content examples Clippers Analysis scenes effects more objects jacks changes help influence Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Shulou Technology Shulou Tech Info macOS Microsoft