Opencv convertScaleAbs函式 和灰度圖上進行透明彩色繪製
阿新 • • 發佈:2019-01-24
在將RealSense提取的深度圖片進行顯示時,由於是16點陣圖片,想將圖片轉化成為8點陣圖形進行顯示
Opencv中有一個函式convertScaleAbs可以實現這種功能
C++: void convertScaleAbs(InputArray src, OutputArray dst, double alpha=1, double beta=0)
Parameters:
src: input array
dst: output array
alpha: optional scale factor
beta: optional delta added to the scaled values
the governmental definition for the function is :
On each element of the input array, the function covertScaleAbs performs three operations sequentially: scaling, taking an absolute value, conversion to an unsigned 8-bit type:
接下來示例程式碼:
cv::Mat map = cv::imread("C:/Users/Lee_gc/Desktop/try.png", CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH);//read a 16bits depth pictures
double min;
double max;
cv::minMaxIdx(map, &min, &max);
cv::Mat adjMap();
cv::convertScaleAbs(map, adjMap, 255 / max);
imshow("8bitPic", adjMap);//the picture can be showed and converted to 8bits pic
waitKey();
為了提取有效的資訊並且顯示,需要將有效區域用其他顏色畫出來
使用opencv circle函式
首先將單通道灰度圖轉化成3通道的彩色影象
例項如下:
// read image
cv::Mat img_gray = imread(path,0);
// create 8bit color image. IMPORTANT: initialize image otherwise it will result in 32F
cv::Mat img_rgb(img_gray.size(), CV_8UC3);
// convert grayscale to color image
cv::cvtColor(img_gray, img_rgb, CV_GRAY2RGB);
circle(img_rgb, Point(100,100), 30, Scalar(100, 250, 100), -1);//Point圓心中心點,30,半斤,Scalar畫筆的引數,-1表示畫成實心狀態
imshow("test", img_rgb);
接下來我們如何能讓畫的圈為透明的,不遮擋重點顯示的區域呢
接下來我們要使用addWeighted函式來實現這個功能,通過實現兩張圖片進行疊加
Mat overlay;
rgb_img.copyTo(overlay);
circle(overlay, p, 50, Scalar(100, 250, 100), -1);
addWeighted(overlay, 0.3, img_rgb, 0.7, 0, img_rgb);
imshow("test", img_rgb);
waitKey();
最後實現的效果如下: