opencv C++ 图像任意mask做卷积

opencv C++ 图像任意mask做卷积


欢迎在评论区留下自己的意见呀!

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

using namespace std;
using namespace cv;

/***********************************************************************
 * 
 * Name of Function: 
 *		RandomConvolution()
 *
 * Parameter:
 *		src: the image of input
 *		dst: the image of output
 *		mask: the mask used to make Convolution
 *		coefficient: a parameter used to relize normalization
 *		Sx: the number of 0 need to fill in X coordinate
 *		Sy: the number of 0 need to fill in Y coordinate
 *
 * The Value of Return:
 *		void
 *
 * State: this function can make us to make Convolution with any mask 
 *		
***********************************************************************/

// only used to process gray image
void RandomConvolution(Mat &src, Mat &dst, int **mask, int coefficient, int Sx, int Sy);


int main()
{

	cv::Mat src, grayImg, output;
	
	//mask used to make Convolution
	int mask[3][3] = { {1,1,1},{1,1,1},{1,1,1} };
	int *mask_used[3] = { mask[0],mask[1],mask[2] };
	
	//读取图片
	src = imread("D:\\ProjectVs\\NewProject\\NewProject\\image.jpg");

	if (src.empty())
	{
		std::cout << "Failed to open file!!!" << std::endl;
	}

	cvtColor(src, grayImg, COLOR_BGR2GRAY);
	
	// dispaly some information about the image
	std::cout << "The width of src: " << src.cols << std::endl;
	std::cout << "The height of src: " << src.rows << std::endl;
	std::cout << "The depth of src: " << src.depth() << std::endl;
	std::cout << "The channels of src: " << src.channels() << std::endl;
	cv::imshow("Orginal Image", src);

	RandomConvolution(grayImg, output, mask_used, 9, 1, 1);// only used to process gray image															  
	cv::imshow("OUTPUT", output);
	cv::imwrite("result.jpg", output);//save the output

	waitKey(0);
	return 0;

}

void RandomConvolution(Mat &src, Mat &dst, int **mask, int coefficient, int Sx, int Sy)
{
	cv::Mat preImg;

 	//make a mat dst to accept the pixel and gray after making Convolution
	dst = Mat(src.rows, src.cols, CV_8U, Scalar(0, 0, 0));

	//make a nee image which has filled the edges
	copyMakeBorder(src, preImg, Sx, Sx, Sy, Sy, BORDER_DEFAULT);

	for (int i = Sx; i < src.rows + Sx; i++)
	{
		for (int j = Sy; j < src.cols + Sy; j++)
		{
			int val = 0;
			for (int m = i - Sx; m < i + Sx + 1; m++)
			{
				for (int n = j - Sy; n < j + Sy + 1; n++)
				{
					val += preImg.at<uchar>(m, n)*mask[m - i + Sx][n - j + Sy];
				}
			}
			dst.at<uchar>(i - Sx, j - Sy) = val / coefficient;
		}
	}
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OpenCV是一个开源计算机视觉库,提供了许多用于图像处理的函数和工具。下面是使用OpenCV C++实现图像处理基本操作的步骤: 1. 首先,需要在C++程序中包含OpenCV的头文件: ```c++ #include <opencv2/core/core.hpp> #include <opencv2/imgcodecs.hpp> #include <opencv2/highgui.hpp> #include <opencv2/imgproc.hpp> #include <iostream> ``` 2. 读取图像文件并显示图像: ```c++ cv::Mat image = cv::imread("example.jpg"); // 读取图像文件 cv::imshow("Image", image); // 显示图像 cv::waitKey(0); // 等待用户输入任意键继续 ``` 3. 转换图像的颜色空间: ```c++ cv::Mat gray_image; cv::cvtColor(image, gray_image, cv::COLOR_BGR2GRAY); // 将图像从BGR色彩空间转换为灰度色彩空间 cv::imshow("Gray Image", gray_image); cv::waitKey(0); ``` 4. 图像的平滑滤波: ```c++ cv::Mat blur_image; cv::GaussianBlur(image, blur_image, cv::Size(5, 5), 0); // 高斯滤波 cv::imshow("Blur Image", blur_image); cv::waitKey(0); ``` 5. 图像的边缘检测: ```c++ cv::Mat canny_image; cv::Canny(image, canny_image, 100, 200, 3); // Canny边缘检测 cv::imshow("Canny Image", canny_image); cv::waitKey(0); ``` 6. 识别图像中的轮廓: ```c++ cv::Mat contour_image = image.clone(); // 克隆原始图像,以便在上面绘制轮廓 cv::cvtColor(image, gray_image, cv::COLOR_BGR2GRAY); // 首先将图像从BGR色彩空间转换为灰度色彩空间 cv::threshold(gray_image, gray_image, 100, 255, cv::THRESH_BINARY); // 二值化图像 std::vector<std::vector<cv::Point>> contours; // 存储轮廓点的向量 std::vector<cv::Vec4i> hierarchy; // 存储轮廓层次结构的向量 cv::findContours(gray_image, contours, hierarchy, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE); // 查找轮廓 for (int i = 0; i < contours.size(); i++) // 遍历所有轮廓并绘制 { cv::Scalar color = cv::Scalar(0, 0, 255); // 定义轮廓颜色(红色) cv::drawContours(contour_image, contours, i, color, 2, cv::LINE_8, hierarchy, 0); // 在图像上绘制每个轮廓 } cv::imshow("Contour Image", contour_image); cv::waitKey(0); ``` 引用: opencv(c++)实现图像处理基本操作。常见头文件 #include<opencv2/core/core.hpp> #include<opencv2/imgcodecs.hpp> #include<opencv2/highgui.hpp> #include<opencv2/imgproc.hpp> #include<iostream> [^1]。引用:Opencv c++图像处理)。openCV 中 cv::Rect 矩形类用法_sinat_38102206的博客-CSDN博客_cv rect [^2]。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值