opencv-c++自学day2

单通道(灰度)/三通道(RGB)图像应用(侵删)

  1. 单通道(灰度)

原图1-1 原图
灰度图
1-2 灰度图像
代码:

#include<opencv2/opencv.hpp>
#include<iostream>

using namespace std;
using namespace cv;

int main(int argc, char** argv) {
	Mat src, gray_src;
	src = imread("C:/Users/Administrator/Pictures/20160711084909.jpg");
	if (!src.data)
	{
		printf("could not lead image...\n");
		return -1;
	}
	namedWindow("input image", CV_WINDOW_AUTOSIZE);
	imshow("input image", src);

cvtColor(src, gray_src, CV_BGR2GRAY);
namedWindow("output image", CV_WINDOW_AUTOSIZE);
imshow("output image", gray_src);

反转:

int height = gray_src.rows;
int width = gray_src.cols;

for (int row = 0; row<height;row++)
	for (int col = 0; col < width; col++)
	{
		int gray = gray_src.at<uchar>(row, col);
		gray_src.at<uchar>(row, col) = 255 - gray;
	}

imshow("output2", gray_src);

反转
1-3 反正灰度图

需要注意此方法必须要求图片是单通道(即转成灰度图像)才能实现反转。

  1. 三通道(BGR)图片
Mat dst;
	dst.create(src.size(), src.type());
	height = src.rows;
	width = src.cols;
	int nc = src.channels();
	for (int row = 0; row<height; row++)
		for (int col = 0; col < width; col++)
		{
			if (nc == 1) {
				int gray = gray_src.at<uchar>(row, col);
				dst.at<uchar>(row, col) = 255 - gray;
			}
			else if (nc == 3)
			{
				int b = dst.at<Vec3b>(row, col)[0];
				int g = dst.at<Vec3b>(row, col)[1];
				int r = dst.at<Vec3b>(row, col)[2];
				dst.at<Vec3b>(row, col)[0] = 255 - b;
				dst.at<Vec3b>(row, col)[1] = 255 - g;
				dst.at<Vec3b>(row, col)[2] = 255 - r;
			}
			
		}
	printf("nc = %d", nc);
	//bitwise_not(src, dst);  //位操作直接反转
	imshow("output3", dst);

暂时因为未知原因未实现,但是使用opencv的库函数bitwise_not()可以实现反转,如下图。
原图反转
1-4 原图像反转

图像混合

API:理解为函数/接口
线性混合:理解为两张图片叠加
公式
f0理解为第一张图像,f1理解为第二张图像,gx理解为合成后的图像
在这里插入图片描述
参数
gamma为校验值,如果两个图片混合后太暗了,gamma可以调节亮度。

代码:

#include<opencv2/opencv.hpp>
#include<iostream>

using namespace std;
using namespace cv;

int main(int agrc, char** argv) {
	Mat src1, src2, dst;
	src1 = imread("C:/Users/Administrator/Pictures/mmexport1566896832907.jpg");
	src2 = imread("C:/Users/Administrator/Pictures/20190827161326_ps.jpg");
	if (src1.empty())
	{
		cout << "could not load image src1..." << endl;
		return -1;
	}
	if (src2.empty())
	{
		cout << "could not load image src2..." << endl;
		return -1;
	}
	double alpha = 0.5;
	if (src1.rows == src2.rows && src1.cols == src2.cols && src1.type() == src2.type())
	{
		addWeighted(src1, alpha, src2, (1.0 - alpha), 0.0, dst);
		namedWindow("input1", CV_WINDOW_AUTOSIZE);
		imshow("input1", src1);
		namedWindow("input2", CV_WINDOW_AUTOSIZE);
		imshow("input2", src2);
		namedWindow("output", CV_WINDOW_AUTOSIZE);
		imshow("output", dst);
	}
	else printf("could not blend images, the size of images is not same...\n");
	waitKey(0);
	return 0;
}

效果图
2-1 效果图
关于
if (src1.rows == src2.rows && src1.cols == src2.cols && src1.type() == src2.type())
要求两张图必须像素一致且都是同一个格式。

附PS调图片像素方法:
ps调图片像素
2-2 PS调图片像素方法
另外还有两个API:
add(src1, src2, dst, Mat());
multiply(src1, src2, dst, 1.0);
都是直接的叠加,很不美观。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值