如今智能手机里都有个“魔法换天”的特效,现看下如何实现的呢?
原始图
代码语言:javascript复制enum MyShape{MyCIRCLE=0,MyRECTANGLE,MyELLIPSE};
struct ParamColorMap {
int iColormap;
Mat img;
};
String winName="False color";
static const String ColorMaps[] = { "Autumn", "Bone", "Jet", "Winter", "Rainbow", "Ocean", "Summer", "Spring",
"Cool", "HSV", "Pink", "Hot", "Parula", "Magma", "Inferno", "Plasma", "Viridis",
"Cividis", "Twilight", "Twilight Shifted", "Turbo", "User defined (random)" };
ParamColorMap p;
Mat img;
if (argc > 1)
img = imread((argv[1]), IMREAD_GRAYSCALE);
p.img=img;
p.iColormap=0;
灰度图
代码语言:javascript复制 imshow("Gray image",img);
namedWindow(winName);
魔法换天
代码语言:javascript复制static void TrackColorMap(int x, void *r)
{
ParamColorMap *p = (ParamColorMap*)r;
Mat dst;
p->iColormap= x;
if (x == COLORMAP_COOL 1)
{
Mat lutRND(256, 1, CV_8UC3);
randu(lutRND, Scalar(0, 0, 0), Scalar(255, 255, 255));
applyColorMap(p->img, dst, lutRND);
}
else
applyColorMap(p->img,dst,p->iColormap);
putText(dst, "Colormap : " ColorMaps[p->iColormap], Point(10, 20), FONT_HERSHEY_SIMPLEX, 0.8, Scalar(255, 255, 255),2);
imshow(winName, dst);
}
秋天
代码语言:javascript复制 createTrackbar("colormap", winName,&p.iColormap,1,TrackColorMap,(void*)&p);
setTrackbarMin("colormap", winName, COLORMAP_AUTUMN);
setTrackbarMax("colormap", winName, COLORMAP_COOL 1);
setTrackbarPos("colormap", winName, -1);
TrackColorMap(0, (void*)&p);
cout << "Press a key to exit" << endl;
waitKey(0);
春天
冬天