欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

opencv---图像的掩模操作

程序员文章站 2024-03-25 10:32:34
...
#include "stdafx.h"
#include <opencv.hpp>
using namespace cv;


int _tmain(int argc, _TCHAR* argv[])
{
	Mat src = imread("1.jpg", IMREAD_UNCHANGED);
	if (src.empty())
	{
		printf("Load src Image Error.");

		return -1;
	}

	namedWindow("src image", CV_WINDOW_AUTOSIZE);
	imshow("src image", src);

	CV_Assert(src.depth() == CV_8U);

	Mat dst;
	dst = Mat::zeros(src.size(), src.type());
	int cols = (src.cols - 1) * src.channels(); //原图像列数 - 1 * channels
	int rows = src.rows;                        //原图像行数
	int offset = src.channels();
	
	for (int row = 1; row < rows - 1; row++)
	{
		const uchar* pCur = src.ptr<uchar>(row);      //指向当前行指针
		const uchar* pPre = src.ptr<uchar>(row - 1);  //指向上一行指针
		const uchar* pNext = src.ptr<uchar>(row + 1); //指向下一行指针
		uchar* pOutput = dst.ptr<uchar>(row);         //指向目标图像的当前行指针

		for (int col = offset; col < cols; col++)
		{
			pOutput[col] = saturate_cast<uchar>(pCur[col] * 5 - (pCur[col - offset] + pCur[col + offset] + pPre[col] + pNext[col]));
		}
	}

	/* opencv中的掩模函数
	Mat kernel = (Mat_<char>(3,3) << 0,-1,0,-1,5, 0,-1,0,-1);
	filter2D(src,dst,src.depth(),kernel);
	*/

	namedWindow("dst image",CV_WINDOW_AUTOSIZE);
	imshow("dst image",dst);


	waitKey(0);

	return 0;
}

上例的掩模应用:提高图像的对比度