search
尋找貓咪~QQ 地點 桃園市桃園區 Taoyuan , Taoyuan

[Android] Android Stutio OPENCV 霍夫曼圓 靜態圖片找圓

此篇只使用霍夫曼圓公式找圓

Bitmap圖片取得方法請看這篇:

Bitmap圖片繪製篇

OPENCV安裝請看這篇:

OPENCV安裝

示範

JAVA

Bitmapp = ((BitmapDrawable) ig.getDrawable()).getBitmap();//取得Bitmap圖片
Utils.bitmapToMat(Bitmapp, mat);//將bitmap圖片轉換成Mat格式
Imgproc.cvtColor(mat, grayMat, Imgproc.COLOR_RGB2GRAY);//將彩度圖轉化為灰度圖PS:霍夫曼規定要用灰度圖
double dp = 1;//辨識率1是最大
double minDist = 10;//最小同心圓距離
double param1 = 100;//圓半徑包含的Pixel值上限
double param2 = 50;//圓半徑包含的Pixel值下限
int minRadius = 10;//最小元半徑範圍
int maxRadius = 20;//最大元半徑範圍
Mat circles = new Mat(Bitmapp.getWidth(), Bitmapp.getHeight(), CvType.CV_8UC1);//創建Mat畫布,寬和高依照Bitmap圖片
Imgproc.HoughCircles(grayMat, circles, Imgproc.CV_HOUGH_GRADIENT, dp, minDist, param1, param2, minRadius, maxRadius);//霍夫曼圓公式
int radius = 0;
//利用迴圈找出圖片中的圓
if (circles.cols() != 0) {
    try {
        for (int ig = 0; ig <= Math.min(circles.cols(), 100); ig++) {//可以控制要抓出的圓數量
            double circleVec[] = circles.get(0, ig);
            Point center = new Point((int) circleVec[0], (int) circleVec[1]);//圓心
            radius = (int) circleVec[2];//圓周
            Imgproc.circle(mat, center, radius, new Scalar(255, 255, 0), 1);//畫圓周(MAT畫布/圓心/圓周/顏色/粗細)
            Imgproc.circle(mat, center, 1, new Scalar(100, 100, 50), 1);//畫圓心(MAT畫布/圓心/圓周/顏色/粗細)
        }
//try{}catch()必加不可省略
    } catch (Exception e) {
    }
}
Utils.matToBitmap(mat, Bitmapp);//轉換Mat圖片回Bitmap圖片
ig.setImageBitmap(Bitmapp);//設置Bitmap圖片到ImageView元件裡面

 

展示

 

相關文章:

[Android] Android 學習總集



熱門推薦

本文由 kk665403pixnetnetblog 提供 原文連結

寵物協尋 相信 終究能找到回家的路
寫了7763篇文章,獲得2次喜歡
留言回覆
回覆
精彩推薦