當前位置:網站首頁>opencv掩膜操作

opencv掩膜操作

2022-01-28 00:38:19 散散汪

超詳細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; 

}

版權聲明
本文為[散散汪]所創,轉載請帶上原文鏈接,感謝
https://cht.chowdera.com/2022/01/202201280038189476.html

隨機推薦