Opencv之图像边缘处理(copyMakeBorder和borderInterpolate)

理论补充:理论知识
!!!!回顾时一定要结合上面理论知识的链接

源程序:
#include “stdafx.h”

//本节讲述 图像处理之 图像边缘处理;
#include <opencv2/opencv.hpp>
#include
#include <math.h>

using namespace std;
using namespace cv;

Mat src, dst1, dst2, dst3, dst4, asd, gray_src;
int threshold_value = 127;
int threshold_max = 255;

//const char*
void shreshold_demo(int, void*);
const char*output_title = “自定义算子”;
char input_title[] = “原图”;

int main(int argc, char**argv)
{
src = imread(“C:/Users/Rubison.DELL/Desktop\杂物/壁纸/小白2.jpg”); //存放自己图像的路径
if (!src.data)
{
printf(“could not load image…\r\n”);
return -1;
}

namedWindow(input_title, CV_WINDOW_AUTOSIZE);
imshow(input_title, src);

int top = (int)(0.05*src.rows);
int bottom = (int)(0.05*src.rows);
int left = (int)(0.05*src.cols);
int right = (int)(0.05*src.cols);

RNG rng(12345);
int borderType = BORDER_DEFAULT;

int c = 0;
while (true)
{
	c = waitKey(500);  
	if ((char)c == 27)   //ESC
	{
		break;
	}
	if ((char)c == 'r')
	{
		borderType = BORDER_REPLICATE;
	}
	else if ((char)c == 'v')
	{
		borderType = BORDER_WRAP;
	}
	else if ((char)c == 'c')
	{
		borderType = BORDER_CONSTANT;
	}
//	else{ borderType = BORDER_DEFAULT; }
	Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
	copyMakeBorder(src, dst1, top, bottom, left, right, borderType,color);
	imshow("边缘处理", dst1);
}


waitKey(0);
destroyAllWindows();
return 0;

}

运行结果:在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
补充2020.9.7

程序补充:
相关链接:sobel算子相关知识

#include "stdafx.h"

//本节讲述 图像处理之 图像边缘处理;
#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>

using namespace std;
using namespace cv;

Mat src, dst1, dst2, dst3, dst4, asd, gray_src;
int threshold_value = 127;
int threshold_max = 255;

//const char*
void shreshold_demo(int, void*);
const char*output_title = "自定义算子";
char input_title[] = "原图";

int main(int argc, char**argv)
{
	src = imread("C:/Users/Rubison.DELL/Desktop\\杂物/壁纸/小白2.jpg");  //存放自己图像的路径 
	if (!src.data)
	{
		printf("could not load image...\r\n");
		return -1;
	}

	namedWindow(input_title, CV_WINDOW_AUTOSIZE);
	imshow(input_title, src);
	GaussianBlur(src,dst1,Size(3,3),0,0);
	cvtColor(dst1, gray_src,CV_BGR2GRAY);
	//imshow("灰度",gray_src);

	Mat xgrad, ygrad, MRy/*,xygrad*/, xygrad2;
	Sobel(gray_src, xgrad, CV_16S, 1, 0,3);   //注意
	Sobel(gray_src, ygrad, CV_16S, 0, 1, 3);
	//Sobel(gray_src, MRy, -1, 0, 1, 3);     //默认深度时,会截断
	convertScaleAbs(xgrad, xgrad);  //由于会出现负值的情况,因此使用convertScalerAbs() 转换为绝对值的形式
	convertScaleAbs(ygrad, ygrad);
	imshow("xgrad", xgrad);
	imshow("ygrad", ygrad);
	//imshow("MRy", MRy);

	addWeighted(xgrad, 0.5, ygrad, 0.5, 0, xygrad2);
	imshow("xy手动相加梯度叠加",xygrad2);



	//两幅图像素简单相加==addWeighted()  且手动像素相加边界更醒目 注意!! sobel算子的改进函数Scharr()函数;
	Mat xygrad =Mat(xgrad.size(), xgrad.type());
	int width = xgrad.cols;
	int height = ygrad.rows;
	for (int row = 0; row < height; row++)
	{
		for (int col = 0; col < width; col++)
		{
			int xg = xgrad.at<uchar>(row, col);
			int yg = ygrad.at<uchar>(row, col);
			int xy = xg + yg;
			xygrad.at<uchar>(row, col) = saturate_cast<uchar>(xy);
		}
	}
	imshow("xy梯度叠加", xygrad);

	waitKey(0);
	destroyAllWindows();
	return 0;

}

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

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

w5875895

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值