OpenCV寒假学习day03
1.逻辑操作&取反操作
代码
#include
#include
using namespace cv;
using namespace std;
int main(int argc, const char* argv[])
{
Mat src1 = Mat::zeros(Size(400, 400), CV_8UC3);
Rect rect(100, 100, 100, 100);
src1(rect) = Scalar(0, 0, 255);
imshow("input1", src1);
printf("create first image...\n");
Mat src2 = Mat::zeros(Size(400, 400), CV_8UC3);
rect.x = 150;
rect.y = 150;
src2(rect) = Scalar(0, 0, 255);
imshow("input2", src2);
printf("create second image...\n");
Mat dst1, dst2, dst3;
bitwise_and(src1, src2, dst1);
bitwise_xor(src1, src2, dst2);
bitwise_or(src1, src2, dst3);
imshow("dst1", dst1);
imshow("dst2", dst2);
imshow("dst3", dst3);
Mat src = imread("D:/vcprojects/images/test.png");
namedWindow("input", WINDOW_AUTOSIZE);
imshow("input", src);
Mat dst;
bitwise_not(src, dst);
imshow("dst", dst);
waitKey(0);
return 0;
}
运行结果
重要函数 |
功能 |
bitwise_and(src1,src2,dst) |
逐元素按位与操作 |
bitwise_xor(src1,src2,dst) |
逐元素按位“异或”操作 |
bitwise_or(src1,src2,dst) |
逐元素按位或操作 |
bitwise_not(src,dst) |
逐元素取反操作 |
Rect(x,y,width,height) |
创建矩形 |
2.OpenCV通道分离与合并
代码
#include
#include
using namespace cv;
using namespace std;
int main(int argc, const char* argv[])
{
Mat src = imread("D:/vcprojects/images/flower.png");
if (src.empty()) {
printf("could not load image...\n");
return -1;
}
namedWindow("input", WINDOW_AUTOSIZE);
imshow("input", src);
vector<Mat> mv;
Mat dst1, dst2, dst3;
split(src, mv);
mv[0] = Scalar(0);
merge(mv, dst1);
imshow("output1", dst1);
split(src, mv);
mv[1] = Scalar(0);
merge(mv, dst2);
imshow("output2", dst2);
split(src, mv);
mv[2] = Scalar(0);
merge(mv, dst3);
imshow("output3", dst3);
waitKey(0);
return 0;
}
运行结果
重要函数 |
功能 |
split(src,mv) |
将src的多通道矩阵分成多个单通道矩阵并保留到Mat对象指针 |
merge(mv,dst) |
split的逆操作,把mv中的数组矩阵合并到dst中的矩阵 |
3.颜色空间转换
代码
#include
#include
using namespace cv;
using namespace std;
int main(int argc, const char* argv[])
{
Mat src = imread("D:/vcprojects/images/dog.jpg");
if (src.empty()) {
printf("could not load image...\n");
return -1;
}
namedWindow("input", WINDOW_AUTOSIZE);
imshow("input", src);
Mat hsv;
cvtColor(src, hsv, COLOR_BGR2HSV);
imshow("hsv", hsv);
Mat yuv;
cvtColor(src, yuv, COLOR_BGR2YUV);
imshow("yuv", yuv);
Mat ycrcb;
cvtColor(src, ycrcb, COLOR_BGR2YCrCb);
imshow("ycrcb", ycrcb);
Mat src2 = imread("D:/vcprojects/images/greenback.png");
imshow("src2", src2);
cvtColor(src2, hsv, COLOR_BGR2HSV);
Mat mask;
inRange(hsv, Scalar(35, 43, 46), Scalar(77, 255, 255), mask);
imshow("mask", mask);
waitKey(0);
return 0;
}
运行结果
重要函数 |
|
cvtColor(src,dst,code) |
保留相同数据类型的同时从一个颜色空间转换到另一个颜色空间 |
inRange(src,upperb,lowerb,dst) |
src的每个元素在upperb和lowerb中对应的校验,如src中的元素为upperb和lowerb之间的,则dst对应的元素为255否则为0 |
4.问题
暂无
5.总结
这次学习了像素的逻辑操作,图像的通道分离与合并及颜色空间转换。接下来需要了解的是有关于颜色空间的知识和,通道分离与合并的实用意义。