图像操作

本文介绍使用Visual Studio 2017结合OpenCV 4.0进行图像处理的方法,包括读取图片、显示图片、转换为灰度图像及反转颜色等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

vs2017%opencv4.0

#include <opencv2/core/core.hpp> 
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgcodecs.hpp> 
#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** args) {
	Mat src = imread("D:\\test.jpg", IMREAD_COLOR);
	if (src.empty()) {
		cout << "could not find the image resource..." << std::endl;
		return -1;
	}
	namedWindow("input", WINDOW_AUTOSIZE);
	imshow("input", src);

	Mat gray_src;
	cvtColor(src, gray_src, COLOR_BGR2GRAY);//转换颜色
	namedWindow("output1", WINDOW_AUTOSIZE);
	imshow("output1", 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;//改变像素
		}
	}
	namedWindow("output2", WINDOW_AUTOSIZE);
	imshow("output2", gray_src);


	/*上面是变成灰度图像处理,下面是直接处理
	Mat dst;
	dst.create(src.size(), src.type());
	int height = src.rows;
	int 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);
				gray_src.at<uchar>(row, col) = 255 - gray;
			}
			else if (nc == 3) {
				int b = src.at<Vec3b>(row, col)[0];
				int g = src.at<Vec3b>(row, col)[1];
				int r = src.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;
			}
		}
	}*/
	//bitwise_not(src, dst);//改变颜色的另外一种方法,直接变成相反颜色
	//imshow("output", dst);
	waitKey(0);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值