颜色滤镜效果
实现原理:其实是利用伪彩色applyColorMap函数,对灰度图进行添加颜色滤镜。
OpenCV的定义12种colormap(色度图),可以应用于灰度图像,使用函数applycolormap产生伪彩色图像。
enum
{
COLORMAP_AUTUMN = 0,
COLORMAP_BONE = 1,
COLORMAP_JET = 2,
COLORMAP_WINTER = 3,
COLORMAP_RAINBOW = 4,
COLORMAP_OCEAN = 5,
COLORMAP_SUMMER = 6,
COLORMAP_SPRING = 7,
COLORMAP_COOL = 8,
COLORMAP_HSV = 9,
COLORMAP_PINK = 10,
COLORMAP_HOT = 11
}
十二中颜色图如下:
C++接口:
using namespace cv;
Mat im_gray = imread("pluto.jpg", IMREAD_GRAYSCALE);
Mat im_color;
applyColorMap(im_gray, im_color, COLORMAP_JET);
Python接口:
import cv2
im_gray = cv2.imread("pluto.jpg", cv2.IMREAD_GRAYSCALE)
im_color = cv2.applyColorMap(im_gray, cv2.COLORMAP_JET)
颜色滤镜C++代码实现:
#include <math.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include<opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
int main()
{
Mat src0= imread("C://timg.jpg");
Mat src;
resize(src0, src, cv::Size(300, 300));
int width = src.cols;
int heigh = src.rows;
Mat gray;
Mat imgColor[12];
Mat display(heigh * 3, width * 4, CV_8UC3);
cvtColor(src, gray, CV_BGR2GRAY);
for (int i = 0; i<12; i++)
{
applyColorMap(gray, imgColor[i], i);
int x = i % 4;
int y = i / 4;
Mat displayROI = display(Rect(x*width, y*heigh, width, heigh));
resize(imgColor[i], displayROI, displayROI.size());
}
imshow("colorImg", display);
waitKey();
}
另外,可以利用色图映射公式来进行颜色滤镜效果实现:
映射公式:
b, g, r = src_img[i][j]
x = int(g / 4 + int(b / 32) * 64)
y = int(r / 4 + int(b % 32) * 64)
参考博客:
https://blog.csdn.net/u013381011/article/details/78341861
https://blog.csdn.net/yangtrees/article/details/9096435
https://baijiahao.baidu.com/s?id=1625787844890494432&wfr=spider&for=pc