图像减运算
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
void main()
{
Mat img1 = imread("C://Users//LH//Desktop//大论文//4-2(b).jpg");
Mat img2 = imread("C://Users//LH//Desktop//大论文//4-1.jpg");
Mat dst;//存储结果
imshow("img1", img1);
imshow("img2", img2);
cout << "img1 " << int(img1.at<Vec3b>(10, 10)[0]) << endl;//img1在坐标(10,10)的蓝色通道的值,强制转成int
cout << "img2 " << int(img2.at<Vec3b>(10, 10)[0]) << endl;
//dst=img1-img2;//这两个减法效果相同 若dst<0,则dst=0
//subtract(img1,img2,dst);//注意:要求被处理图片尺寸一致
absdiff(img1, img2, dst);//若dst<0,则dst=|dst|>=0 用于检测两幅相似图像的不同点,效果比上面的两种减法好
cout << "dst " << int(dst.at<Vec3b>(10, 10)[0]) << endl;
imshow("dst", dst);
waitKey(0);
}
滤波
#include<opencv2/opencv.hpp>
using namespace cv;
void main()
{
Mat src = imread("C://Users//LH//Desktop//大论文//4-1.jpg");
Mat dst;
//boxFilter(src,dst,-1,Size(3,3),Point(-1,-1),true);//方框滤波器
//blur(src,dst,Size(5,5));//均值滤波
//GaussianBlur(src,dst,Size(5,5),1);//高斯滤波
//medianBlur(src,dst,5);//中值滤波
bilateralFilter(src, dst, 5, 10.0, 2.0);//双边滤波
imshow("src", src);
imshow("双边滤波", dst);
waitKey(0);
}
调整图像亮度和对比度
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace std;
using namespace cv;
double alpha; /**< 控制对比度 */
int beta; /**< 控制亮度 */
int main(int argc, char** argv)
{
/// 读入用户提供的图像
Mat image = imread("C://Users//LH//Desktop//大论文//4-1.jpg");
Mat new_image = Mat::zeros(image.size(), image.type());
/// 初始化
cout << " Basic Linear Transforms " << endl;
cout << "-------------------------" << endl;
cout << "* Enter the alpha value [1.0-3.0]: ";
cin >> alpha;
cout << "* Enter the beta value [0-100]: ";
cin >> beta;
/// 执行运算 new_image(i,j) = alpha*image(i,j) + beta
for (int y = 0; y < image.rows; y++)
{
for (int x = 0; x < image.cols; x++)
{
for (int c = 0; c < 3; c++)
{
new_image.at<Vec3b>(y, x)[c] = saturate_cast<uchar>(alpha*(image.at<Vec3b>(y, x)[c]) + beta);
}
}
}
/// 创建窗口
namedWindow("Original Image", WINDOW_AUTOSIZE);
namedWindow("New Image", WINDOW_AUTOSIZE);
/// 显示图像
imshow("Original Image", image);
imshow("New Image", new_image);
/// 等待用户按键
waitKey();
return 0;
}
亮度和对比度
#include<opencv2/opencv.hpp>
using namespace cv;
#define WIN_NAME "输出图像"
Mat src, dst;
int contrast = 20, bright = 20;
void onChange(int, void*){
for (int i = 0; i < src.rows; i++)
{
for (int j = 0; j < src.cols; j++)
{
//saturate_cast<uchar> 溢出保护:if(data<0) data=0; if(data>255) data=255;
dst.at<Vec3b>(i, j)[0] = saturate_cast<uchar>(src.at<Vec3b>(i, j)[0] * contrast*0.01 + bright);
dst.at<Vec3b>(i, j)[1] = saturate_cast<uchar>(src.at<Vec3b>(i, j)[1] * contrast*0.01 + bright);
dst.at<Vec3b>(i, j)[2] = saturate_cast<uchar>(src.at<Vec3b>(i, j)[2] * contrast*0.01 + bright);
}
}
imshow("原图", src);
imshow(WIN_NAME, dst);
}
void main(){
src = imread("C://Users//LH//Desktop//大论文//4-10.jpg");
dst = Mat::zeros(src.size(), src.type());
//Mat::zeros();//将矩阵元素置为0
//Mat::ones();//置1
namedWindow(WIN_NAME, CV_WINDOW_AUTOSIZE);
createTrackbar("对比度", WIN_NAME, &contrast, 300, onChange, 0);
createTrackbar("亮 度", WIN_NAME, &bright, 200, onChange, 0);
onChange(contrast, 0);//回调函数初始化
onChange(bright, 0);
waitKey(0);
}