概念
Canny函数内部使用canny算法对输入图像进行边缘检测。
效果图对比
●源图像
●处理后图像
函数讲解
●函数原型
○c++
void Canny( InputArray image, OutputArray edges,
double threshold1, double threshold2,
int apertureSize = 3, bool L2gradient = false )
○Android
void Canny(Mat image, Mat edges, double threshold1, double threshold2, int apertureSize, boolean L2gradient)
●参数解释
○image:输入图像:8-bit
○edges:输出边缘图像:单通道,8-bit,size与输入图像一致
○threshold1:阈值1,低于阈值1的像素点会被认为不是边缘
○threshold2:阈值2,高于阈值2的像素点会被认为是边缘
○apertureSize:int类型的,表示y方向的差分阶数,1或0 。
○L2gradient:是否采用更精确的方式计算图像梯度,设置为true时更精确但是处理起来也会变慢 。
函数使用
●c++中
#include<opencv2/opencv.hpp>
using namespace cv;
int main() {
Mat src = imread("C:/Users/Administrator/Desktop/ic_test.jpg");//引入源图像
if (src.empty()) {
return -1;
}
imshow("src", src);//展示源图像
Mat dst;
Canny(src,dst,10,200);//边缘提取
imshow("dst", dst);//展示最终结果
waitKey(0);
return 0;
}
●Android中
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.ic_relief);
Mat src = new Mat();
Mat dst = new Mat();
Utils.bitmapToMat(bitmap,src);//将Bitmap对象转换为Mat对象
Imgproc.Canny(src,dst,20,200);//边缘提取
Utils.matToBitmap(dst,bitmap);//将Mat对象转换为Bitmap对象