目录
1.键盘响应
2.颜色表
3.图像像素的逻辑操作
注意需要给图像dst一个初始化
否则vs会报错
waitKey的返回值是ASCII码
当对着图片输出键盘的ASCII码时
会执行操作
#include
#include
#include
#include
using namespace cv;
using namespace std;
int main() {
string path = "resouces\\test.png";
Mat src = imread(path);
Mat dst;
dst = Mat(src.size(), src.type()); //创建一个与src相同大小的Mat对象dst
dst = Scalar(130, 130, 0); //颜色设置
imshow("初始图", src);
while (true) {
int c = waitKey(500);
if (c == 27) {
break;
}
if (c == 49) {
cout << "you enter number 1" << endl;
cvtColor(src, dst, CV_BGR2GRAY);
}
if (c == 50) {
cout << "you enter number 2" << endl;
cvtColor(src, dst, CV_BGR2HSV);
}
if (c == 51) {
cout << "you enter number 3" << endl;
cvtColor(src, dst, CV_BGR2RGB);
}
imshow("键盘响应", dst);
}
return 0;
}
相关函数:applyColorMap
关于index%19 可以保证index的值一直在0到18之间
注意:颜色表是整型数组
#include
#include
#include
#include
using namespace cv;
using namespace std;
int main() {
int colormap[] = {
COLORMAP_AUTUMN,
COLORMAP_BONE,
COLORMAP_CIVIDIS,
COLORMAP_COOL,
COLORMAP_HOT,
COLORMAP_HSV,
COLORMAP_INFERNO,
COLORMAP_JET,
COLORMAP_MAGMA,
COLORMAP_OCEAN,
COLORMAP_PARULA,
COLORMAP_PINK,
COLORMAP_PLASMA,
COLORMAP_RAINBOW,
COLORMAP_SPRING,
COLORMAP_SUMMER,
COLORMAP_TWILIGHT,
COLORMAP_TWILIGHT_SHIFTED,
COLORMAP_VIRIDIS,
COLORMAP_WINTER
};
string path = "resouces\\test.png";
Mat src = imread(path);
Mat dst;
int index = 0;
while (true) {
int c = waitKey(500);
if (c == 49) {
break;
}
applyColorMap(src, dst, colormap[index % 19]);
index++;
imshow("颜色显示", dst);
}
return 0;
}
#include
#include
#include
#include
using namespace cv;
using namespace std;
int main() {
Mat p1;
Mat p2;
Mat dst;
p1 = Mat(Size(256, 256), CV_8UC3, Scalar(0, 0, 0));
p2 = Mat(Size(256, 256), CV_8UC3, Scalar(0, 0, 0));
rectangle(p1, Point(100,100), Point(170,170),Scalar(255,255,0),FILLED);
rectangle(p2, Point(120, 120), Point(200,200), Scalar(0, 255, 255), FILLED);
bitwise_xor(p1, p2, dst);
imshow("p1", p1);
imshow("p2", p2);
imshow("xor", dst);
waitKey(0);
return 0;
}
与:
异或: