Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the coloring function of grayscale image with OpenCV custom color bar

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how OpenCV custom color bar to achieve gray map coloring function, Xiaobian thinks it is quite practical, so share it with you as a reference, I hope you can gain something after reading this article.

scene requirements

Qt has a convenient color bar interface when displaying 2D images, which allows the grayscale image to be colored based on its designed color bar. For example, if 1 is red, 0.55 is yellow, 0.45 is green, and 0 is blue, the grayscale image will be colored from blue to red (from small to large) after normalization. But sometimes this interface needs too much code to match, which brings some trouble to development, so I wrote a function GrayToColor_ColorBar based on its principle to replace this function.

Function principle: First, you need to convert the gray value map into an 8-channel (uchar) gray value map of 0-255, which can be realized by using the normalization function; then, considering the relationship between color and gray, for example, the lowest color is blue (0,0,255) corresponding to gray value 0, and the highest color is red (255,0,0) corresponding to gray value 255, you only need to find out the law of its change.

Below are the specific implementation functions and test code.

function code

/** * @brief GrayToColor_ColorBar Color with color bar grayscale (1: red, param1: yellow, param2: green, 0: blue)* @param phase Input gray image, channel 1 * @param param1 Color bar parameter1 * @param param2 Color bar parameter 2 * @return Colored image */cv::Mat GrayToColor_ColorBar(cv::Mat &phase, float param1, float param2){ CV_Assert(phase.channels() == 1); //Bar parameter 1 must be greater than Bar parameter 2 if (param2 >= param1) { return cv::Mat::zeros(10, 10, CV_8UC1); } cv::Mat temp, result, mask; //Renormalize the grayscale map to 0-255 cv::normalize(phase, temp, 255, 0, cv::NORM_MINMAX); temp.convertTo(temp, CV_8UC1); //Create a mask to isolate interference with nan values mask = cv::Mat::zeros(phase.size(), CV_8UC1); mask.setTo(255, phase == phase); //initialize three-channel color map cv::Mat color1, color2, color3; color1 = cv::Mat::zeros(temp.size(), temp.type()); color2 = cv::Mat::zeros(temp.size(), temp.type()); color3 = cv::Mat::zeros(temp.size(), temp.type()); int row = phase.rows; int col = phase.cols; //Color the grayscale map based on its grayscale levels, with the lowest grayscale value 0 being blue (255, 0,0), the highest grayscale value 255 being red (0,0,255), and the middle grayscale value 127 being green (0,255,0) //Don't be surprised why blue is (255,0,0), because OpenCV is BGR instead of RGB for (int i = 0; i

< row; ++i) { uchar *c1 = color1.ptr(i); uchar *c2 = color2.ptr(i); uchar *c3 = color3.ptr(i); uchar *r = temp.ptr(i); uchar *m = mask.ptr(i); for (int j = 0; j < col; ++j) { if (m[j] == 255) { if (r[j] >

(param1 * 255) && r[j]

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report