In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces OpenCV in the image contrast example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Realization principle
Image contrast refers to the measurement of different luminance levels between the brightest white and the darkest black in an image, that is, the gray contrast of an image. The larger the range of difference is, the larger the contrast is, and the smaller the range of difference is, the smaller the contrast is. Set a base value thresh, when percent is greater than 0, you need to make the color contrast in the image more intense, that is, the farther the value is from thresh, the greater the change; when percent is equal to 1, the contrast is strong to the extreme, with only 255and 0; when percent is equal to 0, it remains the same; when percent is less than 0, the contrast decreases, that is, the value far away from thresh is closer; when percent is equal to-1, there is no contrast, all of it is the thresh value.
The implementation process of the contrast adjustment algorithm is as follows:
1. Set the adjustment parameter percent, with a value of-100 to 100, which is similar to that set in PS and normalized to-1 to 1.
two。 Single processing for all pixels of the image. When percent is greater than or equal to 0, the contrast is enhanced, and the adjusted RGB three-channel value is:
3. If the percent is less than 0, the contrast decreases, and the adjusted image RGB three-channel value is:
4. If percent equals 1, greater than thresh equals 255. less than equals 0.
So far, the brightness of the image has been adjusted, and the algorithm logic refers to xingyanxiao. The C++ implementation code is as follows.
Function code
/ / contrast cv::Mat Contrast (cv::Mat src, int percent) {float alpha = percent / 100.f; alpha = max (- 1.f, min (1.f, alpha)); cv::Mat temp = src.clone (); int row = src.rows; int col = src.cols; int thresh = 127; for (int I = 0; I
< row; ++i) { uchar *t = temp.ptr(i); uchar *s = src.ptr(i); for (int j = 0; j < col; ++j) { uchar b = s[3 * j]; uchar g = s[3 * j + 1]; uchar r = s[3 * j + 2]; int newb, newg, newr; if (alpha == 1) { t[3 * j + 2] = r >Thresh? 255: 0; t [3 * j + 1] = g > thresh? 255: 0; t [3 * j] = b > thresh? 255: 0; continue } else if (alpha > = 0) {newr = static_cast (thresh + (r-thresh) / (1-alpha)); newg = static_cast (thresh + (g-thresh) / (1-alpha)) Newb = static_cast (thresh + (b-thresh) / (1-alpha));} else {newr = static_cast (thresh + (r-thresh) * (1 + alpha)) Newg = static_cast (thresh + (g-thresh) * (1 + alpha)); newb = static_cast (thresh + (b-thresh) * (1 + alpha));} newr = max (0, min (255, newr)) Newg = max (0, min (255, newg)); newb = max (0, min (255, newb)); t [3 * j + 2] = static_cast (newr); t [3 * j + 1] = static_cast (newg) T [3 * j] = static_cast (newb);}} return temp;}
C++ test code
# include # include using namespace cv;using namespace std; cv::Mat Contrast (cv::Mat src, int percent); int main () {cv::Mat src = imread ("5.jpg"); cv::Mat result = Contrast (src, 50.f); imshow ("original", src); imshow ("result", result); waitKey (0); return 0 } / / contrast cv::Mat Contrast (cv::Mat src, int percent) {float alpha = percent / 100.f; alpha = max (- 1.f, min (1.f, alpha)); cv::Mat temp = src.clone (); int row = src.rows; int col = src.cols; int thresh = 127; for (int I = 0; I
< row; ++i) { uchar *t = temp.ptr(i); uchar *s = src.ptr(i); for (int j = 0; j < col; ++j) { uchar b = s[3 * j]; uchar g = s[3 * j + 1]; uchar r = s[3 * j + 2]; int newb, newg, newr; if (alpha == 1) { t[3 * j + 2] = r >Thresh? 255: 0; t [3 * j + 1] = g > thresh? 255: 0; t [3 * j] = b > thresh? 255: 0; continue } else if (alpha > = 0) {newr = static_cast (thresh + (r-thresh) / (1-alpha)); newg = static_cast (thresh + (g-thresh) / (1-alpha)) Newb = static_cast (thresh + (b-thresh) / (1-alpha));} else {newr = static_cast (thresh + (r-thresh) * (1 + alpha)) Newg = static_cast (thresh + (g-thresh) * (1 + alpha)); newb = static_cast (thresh + (b-thresh) * (1 + alpha));} newr = max (0, min (255, newr)) Newg = max (0, min (255, newg)); newb = max (0, min (255, newb)); t [3 * j + 2] = static_cast (newr); t [3 * j + 1] = static_cast (newg) T [3 * j] = static_cast (newb);}} return temp;} Test effect
Fig. 1 original picture
Figure 2 an effect diagram with a parameter of 50
Fig. 3 an effect diagram with a parameter of-50
The image contrast can be adjusted by adjusting the percent.
Thank you for reading this article carefully. I hope the article "sample Analysis of OpenCV in Image contrast" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.