Android OpenCV 边缘检测 Canny 的使用
生活随笔
收集整理的這篇文章主要介紹了
Android OpenCV 边缘检测 Canny 的使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
先看下實現的效果圖
下面看下代碼使用
Canny(Mat image, Mat edges, double threshold1, double threshold2, int apertureSize, boolean L2gradient)
?@param image 8-bit input image.
* @param edges output edge map; single channels 8-bit image, which has the same size as image .
* @param threshold1 first threshold for the hysteresis procedure.
* @param threshold2 second threshold for the hysteresis procedure.
* @param apertureSize aperture size for the Sobel operator.
* @param L2gradient a flag, indicating whether a more accurate \(L_2\) norm
* \(=\sqrt{(dI/dx)^2 + (dI/dy)^2}\) should be used to calculate the image gradient magnitude (
* L2gradient=true ), or whether the default \(L_1\) norm \(=|dI/dx|+|dI/dy|\) is enough (
* L2gradient=false )
第???????一個參數image:輸入圖像,單通道8位圖像
第二個參數edges:輸出邊緣圖像,需要和原圖像有一樣尺寸和類型
第三個參數threshold1:第一個滯后性閾值
第四個參數threshold2:第二個滯后性閾值 這個數值越大輪廓越少,
第五個參數apertureSize:表示應用sobel算子的孔徑大小其有默認值3
第六個參數L2gradient:bool類型的L2gradient,一個計算圖像梯度幅值的標識閾值1和閾值2中較小的用于邊緣的連接,較大的用于控制強邊緣的初始段。高低閾值比在2:1和3:1之間時最佳的
?
public class CanneyActivity extends AppCompatActivity {private ImageView imageView;private Bitmap bitmap;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.canney_activity_layout);imageView = findViewById(R.id.img);bitmap = ((BitmapDrawable) getResources().getDrawable(R.mipmap.photo)).getBitmap();imageView.setImageBitmap(bitmap);findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Bitmap canneyBitmap = toCanneyByOpencv(bitmap);imageView.setImageBitmap(canneyBitmap);}});}private Bitmap toCanneyByOpencv(Bitmap bitmap) {Mat mat = new Mat();Utils.bitmapToMat(bitmap, mat);Mat gray= new Mat();// 轉灰度圖片
// Imgproc.cvtColor(mat, gray, Imgproc.COLOR_BGR2GRAY);// 使用3x3的內核來降噪
// Imgproc.blur(mat, gray, new Size(3,3));// 高斯降噪
// Imgproc.GaussianBlur(mat, gray, new Size(3,3),5,5);//3 描繪邊緣Imgproc.Canny(mat, gray, 50, 200, 3, false);Utils.matToBitmap(gray, bitmap);return bitmap;}@Overrideprotected void onResume() {super.onResume();OpenCVLoader.initDebug();}}
xml 代碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity"><ImageViewandroid:id="@+id/img"android:layout_width="wrap_content"android:layout_height="wrap_content"android:scaleType="fitXY" /><Buttonandroid:id="@+id/btn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="邊緣檢測"/></LinearLayout>
可以修改threshold2 查看輪廓
public class CanneyActivity extends AppCompatActivity {private ImageView imageView;private Bitmap bitmap;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.canney_activity_layout);imageView = findViewById(R.id.img);bitmap = ((BitmapDrawable) getResources().getDrawable(R.mipmap.photo)).getBitmap();imageView.setImageBitmap(bitmap);findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Bitmap canneyBitmap = toCanneyByOpencv(bitmap);imageView.setImageBitmap(canneyBitmap);}});}private Bitmap toCanneyByOpencv(Bitmap bitmap) {Mat mat = new Mat();Utils.bitmapToMat(bitmap, mat);Mat gray= new Mat();// 轉灰度圖片
// Imgproc.cvtColor(mat, gray, Imgproc.COLOR_BGR2GRAY);// 使用3x3的內核來降噪
// Imgproc.blur(mat, gray, new Size(3,3));// 高斯降噪
// Imgproc.GaussianBlur(mat, gray, new Size(3,3),5,5);//3 描繪邊緣Imgproc.Canny(mat, gray, 3, 3, 3, false);Utils.matToBitmap(gray, bitmap);return bitmap;}@Overrideprotected void onResume() {super.onResume();OpenCVLoader.initDebug();}}
這個時候在看下效果圖如下
?
總結
以上是生活随笔為你收集整理的Android OpenCV 边缘检测 Canny 的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 月嫂工资一个月多少钱啊?
- 下一篇: 今天加93号汽油,不小心加了97的油