实现步骤
前五个已经在前几章说过了,在这就不再说了,有不明白的可以回去看一下。
,斑点j是指二维图像中周围有颜色差异和灰度差异的区域,因为斑点代表的是一个区域,所以其相对于单纯的角点,具有更好的稳定性和更好的抗干扰能力.斑点通常是指与周围有着颜色和灰度差别的区域。
代码注释已经很清楚了
public static List<Point> getPoint(Context context, Mat mSource, ImageView iv) {
// Mat mSource = new Mat();
//
// if (bitmap==null || mSource ==null){
// return null;
// }
// Utils.bitmapToMat(bitmap, mSource);
Mat grayMat = new Mat();
Imgproc.cvtColor(mSource,grayMat,Imgproc.COLOR_BGR2GRAY);//转换成灰度图
//高斯滤波
Mat gauss = new Mat();
Imgproc.GaussianBlur(grayMat, gauss,new Size(69.0, 69.0), 0);
//二值化
Mat BINARY = new Mat();
Imgproc.threshold(gauss, BINARY,115, 255.0,Imgproc.THRESH_BINARY);
//图像腐蚀
Mat kernel = Imgproc.getStructuringElement(CV_SHAPE_RECT,new Size(3, 3));
Mat result =new Mat();
Imgproc.erode(BINARY, result, kernel, new Point(-1,-1),1);
// 图像膨胀
Mat pz = Imgproc.getStructuringElement(CV_SHAPE_RECT, new Size(3, 3));
Mat pzResult = new Mat();
Imgproc.dilate(BINARY, pzResult, pz, new Point(-1,-1),6);
SimpleBlobDetector_Params Params= new SimpleBlobDetector_Params();
// 亮度阈值的步长控制,越小检测出来的斑点越多
Params.set_thresholdStep(2);
//颜色控制
Params.set_filterByColor(true);
// Params.set_minThreshold(52);
//像素面积大小控制
Params.set_filterByArea(true);
Params.set_minArea(144);
//凸度控制,凸性的定义是(斑点的面积/斑点凸包的面积
Params.set_filterByConvexity(true);
Params.set_minConvexity(0.87F);
//惯性率控制
Params.set_filterByInertia(true);
// 圆形的惯性率等于1,惯性率越接近1,圆度越高
Params.set_minInertiaRatio(0.01F);
SimpleBlobDetector detector = SimpleBlobDetector.create(Params);
MatOfKeyPoint point = new MatOfKeyPoint();
detector.detect(BINARY,point);
Log.e("dbj","point个数="+point.toArray().length);
if (point.toArray().length==0){
showImg(mSource,iv);
return null;
}
Mat keyPoint = new Mat();
drawKeypoints(pzResult,point,keyPoint, new Scalar(0,0,255.0), DrawMatchesFlags_DRAW_RICH_KEYPOINTS);
List<Point> points = new ArrayList<>();
if (point.toList().size()>0){
for (int i = 0; i < point.toList().size(); i++) {
points.add(new Point(point.toList().get(i).pt.x,point.toList().get(i).pt.y));
//在中心点绘制一个圆圈
Imgproc.circle(mSource,new Point(point.toList().get(i).pt.x,point.toList().get(i).pt.y),5, new Scalar(0, 255, 0), 4, LINE_AA);
}
}
Collections.sort(points,new Comparator<Point>() {
@Override
public int compare(Point o1, Point o2) {
return Double.compare(o1.x+ o1.y, o2.x+o2.y);
}
});
Log.e("dbj","point坐标为"+points.toString());
Imgproc.rectangle(mSource, points.get(0), points.get(points.size()-1), new Scalar(0.0, 0.0, 255.0), 6);
showImg(mSource,iv);
return points;
}