OpenCV 4.11.0
开源计算机视觉库
正在加载…
正在搜索…
未找到匹配项
samples/cpp/falsecolor.cpp

使用 applyColorMap 函数的示例

#include <iostream>
using namespace cv;
using namespace std;
enum MyShape{MyCIRCLE=0,MyRECTANGLE,MyELLIPSE};
struct ParamColorMap {
int iColormap;
Mat img;
};
String winName="伪彩色";
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", "Deep Green", "用户自定义(随机)" };
static void TrackColorMap(int x, void *r)
{
ParamColorMap *p = (ParamColorMap*)r;
Mat dst;
p->iColormap= x;
if (x == COLORMAP_DEEPGREEN + 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, "颜色映射 : "+ColorMaps[p->iColormap], Point(10, 20), FONT_HERSHEY_SIMPLEX, 0.8, Scalar(255, 255, 255),2);
imshow(winName, dst);
}
static Mat DrawMyImage(int thickness,int nbShape)
{
Mat img=Mat::zeros(500,256*thickness+100,CV_8UC1);
int offsetx = 50, offsety = 25;
int lineLength = 50;
for (int i=0;i<256;i++)
line(img,Point(thickness*i+ offsetx, offsety),Point(thickness*i+ offsetx, offsety+ lineLength),Scalar(i), thickness);
RNG r;
Point center;
int radius;
int width,height;
int angle;
Rect rc;
for (int i=1;i<=nbShape;i++)
{
int typeShape = r.uniform(MyCIRCLE, MyELLIPSE+1);
switch (typeShape) {
case MyCIRCLE
center = Point(r.uniform(offsetx,img.cols- offsetx), r.uniform(offsety + lineLength, img.rows - offsety));
radius = r.uniform(1, min(offsetx, offsety));
circle(img,center,radius,Scalar(i),-1);
break;
case MyRECTANGLE
center = Point(r.uniform(offsetx, img.cols - offsetx), r.uniform(offsety + lineLength, img.rows - offsety));
width = r.uniform(1, min(offsetx, offsety));
height = r.uniform(1, min(offsetx, offsety));
rc = Rect(center-Point(width ,height )/2, center + Point(width , height )/2);
rectangle(img,rc, Scalar(i), -1);
break;
case MyELLIPSE
center = Point(r.uniform(offsetx, img.cols - offsetx), r.uniform(offsety + lineLength, img.rows - offsety));
width = r.uniform(1, min(offsetx, offsety));
height = r.uniform(1, min(offsetx, offsety));
angle = r.uniform(0, 180);
ellipse(img, center,Size(width/2,height/2),angle,0,360, Scalar(i), -1);
break;
}
}
return img;
}
int main(int argc, char** argv)
{
cout << "本程序演示了 applyColorMap 函数的使用方法。\n\n";
ParamColorMap p;
Mat img;
if (argc > 1)
img = imread(samples::findFile(argv[1]), IMREAD_GRAYSCALE);
else
img = DrawMyImage(2,256);
p.img=img;
p.iColormap=0;
imshow("灰度图像",img);
namedWindow(winName);
createTrackbar("colormap", winName, NULL, COLORMAP_DEEPGREEN + 1, TrackColorMap, (void*)&p);
setTrackbarMin("colormap", winName, COLORMAP_AUTUMN);
setTrackbarMax("colormap", winName, COLORMAP_DEEPGREEN + 1);
setTrackbarPos("colormap", winName, COLORMAP_AUTUMN);
TrackColorMap(0, (void*)&p);
cout << "Press a key to exit" << endl;
waitKey(0);
return 0;
}
n维稠密数组类
定义 mat.hpp:829
int cols
定义 mat.hpp:2155
int rows
行数和列数,当矩阵维度超过2维时为(-1, -1)
定义 mat.hpp:2155
随机数生成器。
定义 core.hpp:2874
int uniform(int a, int b)
返回[a,b)范围内均匀分布的整数随机数
二维矩形的模板类。
定义 types.hpp:444
用于指定图像或矩形大小的模板类。
定义 types.hpp:335
void randu(InputOutputArray dst, InputArray low, InputArray high)
生成单个均匀分布的随机数或随机数数组。
std::string String
定义 cvstd.hpp:151
#define CV_8UC1
定义 interface.h:88
#define CV_8UC3
定义 interface.h:90
@ circle
定义 gr_skig.hpp:62
void imshow(const String &winname, InputArray mat)
在指定的窗口中显示图像。
int waitKey(int delay=0)
等待按键按下。
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
创建窗口。
void setTrackbarPos(const String &trackbarname, const String &winname, int pos)
设置滑块位置。
void setTrackbarMax(const String &trackbarname, const String &winname, int maxval)
设置滑块最大位置。
void setTrackbarMin(const String &trackbarname, const String &winname, int minval)
设置滑块最小位置。
int createTrackbar(const String &trackbarname, const String &winname, int *value, int count, TrackbarCallback onChange=0, void *userdata=0)
创建滑块并将其附加到指定的窗口。
CV_EXPORTS_W Mat imread(const String &filename, int flags=IMREAD_COLOR_BGR)
从文件中加载图像。
void applyColorMap(InputArray src, OutputArray dst, int colormap)
在给定图像上应用 GNU Octave/MATLAB 等效的色图。
void rectangle(InputOutputArray img, Point pt1, Point pt2, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
绘制简单的、粗的或填充的右上角矩形。
void ellipse(InputOutputArray img, Point center, Size axes, double angle, double startAngle, double endAngle, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
绘制简单的或粗的椭圆弧或填充椭圆扇区。
void putText(InputOutputArray img, const String &text, Point org, int fontFace, double fontScale, Scalar color, int thickness=1, int lineType=LINE_8, bool bottomLeftOrigin=false)
绘制文本字符串。
void line(InputOutputArray img, Point pt1, Point pt2, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
绘制连接两点的线段。
int main(int argc, char *argv[])
定义 highgui_qt.cpp:3
定义 core.hpp:107
STL 命名空间。