opencv掩膜操作

超详细opencv掩膜操作,看不懂算我输:

另外附上一篇博文,此博文当时算是解决我老大问题了!

掩膜图像对通道的理解

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

using namespace cv;

int main(int argc, char** argv)
{
	Mat src, dst,t;
	src = imread("D:/PT/yun.jpg");
	double scale = 0.5;
	Size dsize = Size(src.cols * scale, src.rows * scale);
	Mat src2 = Mat(dsize, CV_32S);
	resize(src, src2, dsize);
	CV_Assert(src2.depth() == CV_8U);// 仅接受uchar图像,
	//刚进入函数的时候,我们要确保输入图像是无符号字符类型的。为了做到这点,我们使用了 CV_Assert 函数。若该函数括号内的表达式为false,则会抛出一个错误。

	if (!src2.data)
	{
		printf("could not load image...\n");
		return -1;
	}
	namedWindow("Example1", WINDOW_AUTOSIZE);
	imshow("Example1", src2);

	int offsetx = src2.channels();//RGB三通道
	int cols = (src2.cols-1) * offsetx;//列(要忽略最后一列)
	int rows = src2.rows;//行
	dst = Mat::zeros(src2.size(), src2.type());//初始化
	for (int row = 1; row < (rows - 1); row++) {    //肯定从第二行开始啊,最后一行不要
		const uchar* previous = src2.ptr<uchar>(row - 1);
		const uchar * current = src2.ptr<uchar>(row);
		const uchar * next = src2.ptr<uchar>(row + 1);
		uchar * output = dst.ptr<uchar>(row);
		for (int col = offsetx; col < cols; col++)//0,1,2不要
		{
			output[col] = saturate_cast<uchar>(5 * current[col] - (current[col - offsetx] + current[col + offsetx] + previous[col] + next[col]));
		}
	}
	namedWindow("Example2", WINDOW_AUTOSIZE);
	imshow("Example2", dst);
	//double t = getTickCount();
	//Mat kernel = (Mat_<char>(3, 3) << 0, -1, 0,-1, 5, -1,0, -1, 0);
	//filter2D(src, dst, src.depth(), kernel);
	//double timeconsume =(getTickCount - t)/getTickFrequency();
	cout << "Build-in filter2D time passed in seconds:  " << timeconsume << endl;
	//printf("second", timeconsume);
	//namedWindow("Example2", WINDOW_AUTOSIZE);
	//imshow("Example2", dst);
	waitKey(0);
	return 0; 

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值