博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
边缘检测:sobel
阅读量:4100 次
发布时间:2019-05-25

本文共 1367 字,大约阅读时间需要 4 分钟。

sobel

原理比较简单,参照

流程上就是一张灰度图片分别进行两次卷积,然后将两次卷积后的图片合在一起

#include 
#include
#include
using namespace std;using namespace cv;void conv(char *G, Mat pic, Mat &res){
int i, j, i1, j1; int tmp; for (i = 1; i < pic.rows-1; i++) {
for (j = 1; j < pic.cols - 1; j++) {
tmp = 0; for (i1 = -1; i1 <= 1; i1++) {
for (j1 = -1; j1 <= 1; j1++) {
tmp += pic.at
(i + i1, j + j1)*G[(j1 + 1) * 3 + i1 + 1]; } } res.at
(i, j) = tmp; } }}void merge1(Mat resX, Mat resY, Mat &res){
int tmp; for (int i = 0; i < res.rows; i++) {
for (int j = 0; j < res.cols; j++) {
tmp = abs(resX.at
(i, j)) + abs(resY.at
(i, j)); //tmp = sqrt(resX.at
(i, j)*resX.at
(i, j) + resY.at
(i, j)*resY.at
(i, j)); if (tmp > 255) tmp = 255; res.at
(i,j) = tmp; } }}int main(){ char Gx[3 * 3] = { -1, 0, 1, -2, 0, 2, -1, 0, 1 }; char Gy[3 * 3] = { 1, 2, 1, 0, 0, 0, -1, -2, -1 }; Mat pic = imread("C:\\Users\\Thinkpad\\Desktop\\c++work\\pic\\lena.jpg", 0); Mat resX = Mat::zeros(pic.rows, pic.cols, CV_16SC1); Mat resY = Mat::zeros(pic.rows, pic.cols, CV_16SC1); conv(Gx, pic, resX); conv(Gy, pic, resY); Mat res = Mat::zeros(pic.rows, pic.cols, CV_8UC1); merge1(resX, resY, res); //imwrite("C:\\Users\\Thinkpad\\Desktop\\c++work\\pic\\lena1.jpg", res); imshow("1", res); waitKey(); return 0;}

转载地址:http://swksi.baihongyu.com/

你可能感兴趣的文章
乐视视频 App 图标改为“欠 122 亿”,网友:我在别家分红包,却在你家随份子!...
查看>>
乔布斯18岁求职信拍卖价22.24万美元,值吗?
查看>>
为何程序员总喜欢写技术博客,看完恍然大悟...
查看>>
假如计算机是中国人发明的,那代码应该这么写
查看>>
触目惊心:比特币到底消耗了多少能源?
查看>>
面试官:简历上敢写技术精通?那我就不客气了!
查看>>
如何判断一家互联网公司要倒闭了?
查看>>
想快速上手机器学习?来看下这个 GitHub 项目!
查看>>
GitHub 标星 3.6k,一本开源的深度学习中文教程!
查看>>
9 款你不能错过的 JSON 工具
查看>>
就在昨天,全球 42 亿 IPv4 地址宣告耗尽!
查看>>
200页!分享珍藏很久的Python学习知识手册(附链接)
查看>>
程序员之神
查看>>
4 岁小女孩给 Linux 内核贡献提交
查看>>
推荐几个私藏很久的技术公众号给大家
查看>>
王垠受邀面试阿里 P9,被 P10 面跪后网上怒发文,惨打 325 的 P10 赵海平回应了!...
查看>>
Python 趣味打怪:147 段简单代码助你从入门到大师
查看>>
卧槽!小姐姐用动画图解 Git 命令,这也太秀了吧?!
查看>>
厉害了!Python 编辑器界的神器 Jupyter ,推出官方可视化 Debug 工具!
查看>>
卧槽!Java 虚拟机竟然还有这些性能调优技巧...
查看>>