理论补充:理论知识
!!!!回顾时一定要结合上面理论知识的链接
源程序:
#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;
}
运行结果: