OpenCV基础

博客主要介绍了OpenCV相关操作,包括加载、修改和保存图像,还涉及矩阵的掩膜操作,如获取图像像素指针、解释掩膜操作等。此外,详细阐述了Mat对象,包含其与IplImage对象的关系、使用方法以及如何用Mat定义数组。

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

 OpenCV加载-修改-保存图像

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

using namespace std;
using namespace cv;

int main() {
	Mat src = imread("D:/OpencvProjects/testOpenCV/lena.jpg");
	if (src.empty()) {
		cout << "Image can not be loaded correctly!" << endl;
		return -1;
	}
	
	namedWindow("Raw_image", CV_WINDOW_AUTOSIZE);
	imshow("Raw_image", src);
	

	namedWindow("Gray_image", CV_WINDOW_AUTOSIZE);
	Mat gray;
	cvtColor(src, gray, CV_BGR2HLS);
	imshow("Gray_image", gray);

	imwrite("HLS.png", gray);
	waitKey(0);
	return 0;
}

矩阵的掩膜操作

  1. 获取图像像素指针
  2. 掩膜操作解释
#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>

using namespace cv;
using namespace std;

int main() {
	Mat src, dst;
	src = imread("D:/images/lena.jpg", 1);
	if (!src.data) {
		cout << "Image can not be loaded correctly!" << endl;
		return -1;
	}
	namedWindow("input_image", CV_WINDOW_AUTOSIZE);
	imshow("input_image", src);

	dst = Mat::zeros(src.size(), src.type());
	int cols = (src.cols - 1) * src.channels();
	int offsetx = src.channels();
	int rows = src.rows;
	
	for(int row = 1; row < (rows - 1); row++){
		const uchar* previous = src.ptr<uchar>(row-1);
		const uchar* current = src.ptr<uchar>(row);
		const uchar* next = src.ptr<uchar>(row+1);
		uchar* output = dst.ptr<uchar>(row);
		for (int col = offsetx; col < cols; col++) {
			output[col] = saturate_cast<uchar>(5*current[col]-(current[col-offsetx] + current[col+offsetx] + previous[col] + next[col]));
		}
	}
    /*
    Mat kernel = (Mat_<char>(3,3)<<0,-1,0,-1,5,-1,0,-1,0);
	filter2D(src, dst, src.depth(), kernel);
    */

	namedWindow("Contrast_image", CV_WINDOW_AUTOSIZE);
	imshow("Constrast_image", dst);

	waitKey(0);
	return 0;
}

Mat对象

  1. Mat对象与IplImage对象
  2. Mat对象使用
  3. Mat定义数组
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值