OpenCV1.基本操作and掩膜例子

OpenCV基础操作

加载图像

Mat input = imread("路径");
Mat input = imread("路径", IMREAD_UNCHANGED)(<0)加载原图
Mat input = imread("路径", IMREAD_GRAYSCALE)(0)把原图作为灰度图像加载
Mat input = imread("路径", IMREAD_COLOR)(>0)把原图作为RGB图像加载

显示图像

namedWindow("窗口名", WINDOW_AUTOSIZE);//创建一个窗口,自动创建与释放
WINDOW_AOTUSIZE  //自动根据图像大小显示窗口大小,不能人为改变
WINDOW_NORMAL   //允许修改窗口大小
imshow("窗口名", Mat对象)

修改图像

cvtColor把图像从色彩空间转换的另一个色彩空间。

cvtColor(input_image, output_image, COLOR_BGR2GRAY);
//        输入图像       输出图像     源和目标色彩空间

保存图像

imwrite(“指定目录路径”, 需输出的图像);

只有8位、16位的PNG、JPG、Tiff文件而且是单通道或者是三通道的BGR图像才可以通过这种方式保存。

获取图像像素指针

Mat.ptr<uchar>(int i = 0);//获取像素矩阵的指针,索引i表示第几行,从0开始计数。
const uchar* current = myImage.ptr<uchar>(row);//获取当前行指针
p(row, col) = current[col]; //获取当前像素点

像素范围处理saturate_cast,确保RGB值得范围在0~255之间。

saturate_cast(-100), 返回0。

saturate_cast(288), 返回255。

saturate_cast(100), 返回100。

掩膜操作实现图像对比度调整

掩膜操作公式如图:
在这里插入图片描述

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

using namespace std;
using namespace cv;

int main()
{
	Mat myImage = imread("F:/Opencvlearn/t.jpg");
	if (!myImage.data)
	{
		cout << "could not load image..." << endl;
	}
	namedWindow("myImage", WINDOW_AUTOSIZE);
	imshow("myImage", myImage);

	Mat outImage;
	int height = myImage.rows;
	int width = (myImage.cols - 1) * myImage.channels();
	int offsetx = myImage.channels();

	//myImage.copyTo(outImage);  // 将myImage复制给outImage
	//outImage = Mat::zeros(myImage.size(), myImage.type());  //置为0,全黑图像
	//for (int row = 1; row < height - 1; row++)
	//{
	//	const uchar* previous = myImage.ptr<uchar>(row - 1);
	//	const uchar* current = myImage.ptr<uchar>(row);
	//	const uchar* next = myImage.ptr<uchar>(row + 1);
	//	uchar* output = outImage.ptr<uchar>(row);  //获取当前行的指针
	//	for(int col = offsetx; col < width; col++)
	//	{
	//		output[col] = saturate_cast<uchar>(5 * current[col] - previous[col] - next[col] - current[col - offsetx] - current[col + offsetx]);
	//	}  //掩膜操作,重新计算当前像素点的值
	//}
	

	//函数调用filter2D功能实现
	Mat kernel = (Mat_<char>(3, 3) << 0, -1, 0, 0, 5, 0, 0, -1, 0); //定义掩膜
	filter2D(myImage, outImage, myImage.depth(), kernel); //myImage.depth()表示位图深度
	namedWindow("outImage", WINDOW_AUTOSIZE);
	imshow("outImage", outImage);
	waitKey(0);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值