OpenCV图像处理

 图像减运算

#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);

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值