Opencv-基础入门六

滚动条操作演示-调整图像亮度-TrackBar

#include<quickopencv.h>
/// <summary>
/// 设置颜色空间
/// </summary>
/// <param name="image"></param>
void QuickDemo::colorSpace_Demo(Mat& image) {
	Mat gray, hsv;
	//H 0-180    S,V0-255
	cvtColor(image, hsv, COLOR_BGR2HSV);
	cvtColor(image, gray, COLOR_BGR2GRAY);
	imshow("HSV", hsv);
	imshow("灰度", gray);
	imwrite("F:/vcworkspaces/testSaveImage/hsv.png", hsv);
	imwrite("F:/vcworkspaces/testSaveImage/gray.png", gray);
}
/// <summary>
/// Mat图像对象创建与赋值
/// </summary>
void QuickDemo::mat_creation_demo() {
	//Mat m1, m2;
	//m1 = image.clone();
	//image.copyTo(m2);

	//创建空白图像
	//【1】CV_8UC1---则可以创建----8位无符号的单通道---灰度图片------grayImg
	//define CV_8UC1 CV_MAKETYPE(CV_8U,1)
	//define CV_8UC2 CV_MAKETYPE(CV_8U,2)
	//【2】CV_8UC3---则可以创建----8位无符号的三通道---RGB彩色图像---colorImg 
	//define CV_8UC3 CV_MAKETYPE(CV_8U,3)
	//【3】CV_8UC4--则可以创建-----8位无符号的四通道---带透明色的RGB图像 
	//define CV_8UC4 CV_MAKETYPE(CV_8U,4)

	//Mat m3 = Mat::zeros(Size(8, 8), CV_8UC1);
	
	//但是使用ones时的CV_8UC3只会第一个数据为1
	Mat m3 = Mat::ones(Size(512, 512), CV_8UC3);
	m3 = Scalar(255, 0, 0);//B G R
	//获取图像宽度,因此遍历像素时必须考虑通道数/
	std::cout << "width:" << m3.cols << "height:" << m3.rows << "channels:" << m3.channels() << std::endl;
	//std::cout << m3 << std::endl;
	
	//Mat m4 = m3;
	Mat m4 = m3.clone();
	m4= Scalar(0, 255, 255);//B G R
	imshow("图像3", m3);
	imshow("图像4", m4);
}
/// <summary>
/// 图像像素的读写操作
/// </summary>
/// <param name="image"></param>
void QuickDemo::pixel_visit_demo(Mat& image) {
	//宽
	int  w = image.cols;
	//高
	int h = image.rows;
	//通道数
	int dims = image.channels();
	/*
	for (int row = 0; row < h; row++)
	{
		for (int  col = 0; col < w; col++)
		{
			//灰度图像,单通道
			if (dims == 1) {
				int pv = image.at<uchar>(row, col);//(行/y/高,列/x/宽)
				image.at<uchar>(row, col) = 255 - pv;
			}
			//彩色图像,三通道
			if (dims == 3) {
				Vec3b bgr = image.at<Vec3b>(row, col);//(行/y/高,列/x/宽)
				image.at<Vec3b>(row, col)[0] = 255 - bgr[0];
				image.at<Vec3b>(row, col)[1] = 255 - bgr[1];
				image.at<Vec3b>(row, col)[2] = 255 - bgr[2];
			}
		}
	}
	*/
	//指针操作
	for (int row = 0; row < h; row++)
	{
		uchar* current_row = image.ptr<uchar>(row);
		for (int col = 0; col < w; col++)
		{
			
			//灰度图像,单通道
			if (dims == 1) {
				int pv = *current_row;//(行/y/高,列/x/宽)
				*current_row++ = 255 - pv;
			}
			//彩色图像,三通道
			if (dims == 3) {
				*current_row++ = 255 - *current_row;
				*current_row++ = 255 - *current_row;
				*current_row++ = 255 - *current_row;
			}
		}
	}
	imshow("像素读写显示", image);
}
void  QuickDemo::operators_demo(Mat& image) {
	/*Mat dst=Mat::zeros(image.size(), image.type());;
	dst = image + Scalar(150, 150, 150);
	imshow("加法操作", dst);

	dst = image - Scalar(150, 150, 150);
	imshow("减法操作", dst);*/

	/*
	Mat dst = Mat::zeros(image.size(), image.type());;
	Mat m = Mat::zeros(image.size(), image.type());
	m= Scalar(112,112, 112);*/
	//multiply(image,m,dst);
	//imshow("乘法操作", dst);

	宽
	//int  w = image.cols;
	高
	//int h = image.rows;
	通道数
	//int dims = image.channels();

	加法
	//for (int row = 0; row < h; row++)
	//{
	//	for (int col = 0; col < w; col++)
	//	{
	//		//彩色图像,三通道
	//		if (dims == 3) {
	//			Vec3b p1 = image.at<Vec3b>(row, col);//(行/y/高,列/x/宽)
	//			Vec3b p2 = m.at<Vec3b>(row, col);//(行/y/高,列/x/宽)
	//			dst.at<Vec3b>(row, col)[0] = saturate_cast<uchar>(p1[0] + p2[0]);
	//			dst.at<Vec3b>(row, col)[1] = saturate_cast<uchar>(p1[1] + p2[1]);
	//			dst.at<Vec3b>(row, col)[2] = saturate_cast<uchar>(p1[2] + p2[2]);
	//		}
	//	}
	//}
	 
	//但是咱们会使用opencv存在的API,原因是计算运行快速
	Mat dst = Mat::zeros(image.size(), image.type());;
	Mat m = Mat::zeros(image.size(), image.type());
	m = Scalar(2, 2, 2);
	add(image, m, dst);
	imshow("加法操作", dst);
	subtract(image, m, dst);
	imshow("减法操作", dst);
	multiply(image,m,dst);
	imshow("乘法操作", dst);
	divide(image, m, dst);
	imshow("除法操作", dst);
}

Mat src, dst, m;
int lightness = 50;
static void on_track(int, void*) {
	m = Scalar(lightness, lightness, lightness);
	/*add(src, m, dst);
	imshow("亮度调整", dst);*/
	subtract(src, m, dst);
	imshow("亮度调整", dst);
	//multiply(src, m,dst);
	//imshow("亮度调整", dst);
	//divide(src, m, dst);
	//imshow("亮度调整", dst);
}
void  QuickDemo::tracking_bar_demo(Mat& image) {
	namedWindow("亮度调整", WINDOW_AUTOSIZE);
	 dst = Mat::zeros(image.size(), image.type());;
	 m = Mat::zeros(image.size(), image.type());
	int max_value = 100;
	src = image;
	createTrackbar("value", "亮度调整", &lightness, max_value, on_track);
	on_track(50, 0);
}

 

#pragma once
#include<opencv2/opencv.hpp>
using namespace cv;
class QuickDemo {
public:
	void colorSpace_Demo(Mat& image);
	void mat_creation_demo();
	void pixel_visit_demo(Mat& image);
	void operators_demo(Mat& image);
	void tracking_bar_demo(Mat& image);
};
#include<opencv2/opencv.hpp>
#include<quickopencv.h>
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
	//灰色
	//Mat scr = imread("F:/vcworkspaces/opencv_tutorial_data-master/images/lena.png",IMREAD_GRAYSCALE);
	//任意深度
	Mat scr = imread("F:/vcworkspaces/opencv_tutorial_data-master/images/lena.png");//三通道顺序B G R
	//调整窗口大小
	namedWindow("输入窗口", WINDOW_FREERATIO);
	if (scr.empty())
	{
		printf("Not image");
		return -1;
	}
	//仅支持8位
	imshow("输入窗口", scr);
	QuickDemo qd;
	//qd.mat_creation_demo();
	//qd.pixel_visit_demo(scr);
	//qd.operators_demo(scr);
	qd.tracking_bar_demo(scr);

	//人眼帧率至少300帧
	waitKey(0);
	//销毁单个特定窗口
	destroyAllWindows;
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值