opencv学习记录02:矩阵的掩膜操作

1、获取图像像素指针

1、CV_Assert(myImage.depth() == CV_8U);
2、Mat.ptr(int i=0) 获取像素矩阵的指针,索引i表示第几行,从0开始行数。
3、获得当前行指针const uchar* current= myImage.ptr(row );
4、获取当前像素点P(row, col)的像素值 p(row, col) =current[col]
像素范围处理saturate_cast
saturate_cast(-100),返回 0。
saturate_cast(288),返回255
saturate_cast(100),返回100
这个函数的功能是确保RGB值得范围在0~255之间

2、掩膜操作解释在这里插入图片描述

3、filter2D函数在这里插入图片描述

注:图片From 51CTO学院!!侵权删

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

using namespace cv;

int main() {
	Mat src,dst;
	src = imread("F:\\opencv_work\\03矩阵的掩膜操作\\Project1\\27.jpg");
	//	if (src.empty())    
	//{
	//	cout << "can not find image" << endl;
	//	return -1;
	//}
	namedWindow("总舵主的自画像", WINDOW_AUTOSIZE);
	imshow("总舵主的自画像",src);

	//double t1 = getTickCount();

	//int cols = (src.cols - 1) * src.channels();         //行
	//int offsetx = src.channels();                    //RGB三通道  计算col
	//int rows = src.rows;

	//dst = Mat::zeros(src.size(), src.type());
	//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]));
	//	}
	//}
	//double timeconsume1 = (getTickCount() - t1) / getTickFrequency();
	//printf("timeconsume1 %.2f\n", timeconsume1);

	double t2 = getTickCount();
	Mat kernel = (Mat_<char>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
	filter2D(src, dst, src.depth(), kernel);
	double timeconsume2 = (getTickCount() - t2) / getTickFrequency();
	printf("timeconsume2 %.2f\n", timeconsume2);

	namedWindow("舵主的对比度图像", WINDOW_AUTOSIZE);
	imshow("舵主的对比度图像", dst);

	waitKey(0);
	return(0);
}

程序运行结果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Cai_Xu_Kun/article/details/107569294
今日推荐