Get the App
SLTechnology News&Howtos  ›  Development  › 

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

Shulou Source: shulou.com Published: 2022-06-02 11:48:58 10月03日 Update

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]

Tags: Grayscale color blue channel function parameter red function code image effect effect green highest article test two principle hierarchy interface Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Linux Shulou Information Shulou Technology MySQL