直接用split函数,OpenCV可以与STL的vector联动,于是可以将分离出来的各个通道的图像存到一个vector
代码:
#include
#include
#include
#include
#include
using namespace std;
using namespace cv;
std::vector tmp;
int main()
{
Mat img=imread("../apple.jpg");
split(img,tmp);
imshow("B",tmp.at(0));
imshow("G",tmp.at(1));
imshow("R",tmp.at(2));
waitKey(0);
return 0;
}
效果:
二值化,就是把大于thresh的值全部设为最大值,小于thresh的值全部设为最小值
利用threshold函数可以完成图像二值化
但是手动调参会比较麻烦,我们可以设置一个trackbar(滚动条),拖动它就可以直接改变thresh
并且实时观察图像效果
代码:
#include
using namespace cv;
Mat img,dst;
static void func(int,void *)
{
int thresh=getTrackbarPos("thresh","test");
threshold(img,dst,thresh,255,THRESH_BINARY);
imshow("test",dst);
}
int main()
{
img=imread("../ela_modified.jpg");
int t=150;
namedWindow("test");
cvtColor(img,img,COLOR_BGR2GRAY);
createTrackbar("thresh","test",&t,255,func);
func(t,0);
waitKey(0);
return 0;
}
效果:
各种噪声与滤波方式详见:https://blog.csdn.net/weixin_40446557/article/details/81451651
下面对椒盐噪声进行三种滤波(均值滤波,高斯滤波,中值滤波)
注意每种滤波方式的函数格式不一样
代码:
#include
#include
#include
#include
using namespace std;
using namespace cv;
vector channels;
int main()
{
Mat img=imread("../zaosheng.jpeg"),ans1,ans2,ans3;
blur(img,ans1,Size(3,3));
GaussianBlur(img,ans2,Size(3,3),3.0,3.0);
medianBlur(img,ans3,3);
imshow("junzhi",ans1);
imshow("gaosi",ans2);
imshow("zhongzhi",ans3);
waitKey(0);
return 0;
}
效果: