我想检测魔方的颜色。这就是我想要的:Link
我能够通过findContours
Open CV 的功能识别 9 个彩色字段。
这是我的代码:
Mat input = new Mat(); //The image
Mat blur = new Mat();
Mat canny = new Mat();
Imgproc.GaussianBlur(input, blur, new Size(3,3), 1.5); //GaussianBlur to reduce noise
Imgproc.Canny(blur, canny, 60, 70); //Canny to detect the edges
Imgproc.GaussianBlur(canny, canny, new Size(3,3), 1.5); //Again GaussianBlur to reduce noise
List<MatOfPoint> contours = new ArrayList<>();
Mat hierachy = new Mat();
Imgproc.findContours(canny, contours, hierachy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE); //Find contours
List<MatOfPoint2f> approxedShapes = new ArrayList<>();
for(MatOfPoint point : contours){
double area = Imgproc.contourArea(point);
if(area > 1000){
MatOfPoint2f shape = new MatOfPoint2f(point.toArray());
MatOfPoint2f approxedShape = new MatOfPoint2f();
double epsilon = Imgproc.arcLength(shape, true) / 10;
Imgproc.approxPolyDP(shape, approxedShape, epsilon, true); //"Smooth" the edges with approxPolyDP
approxedShapes.add(approxedShape);
}
}
//Visualisation
for(MatOfPoint2f point : approxedShapes){
RotatedRect rect = Imgproc.minAreaRect(new MatOfPoint2f(point.toArray()));
Imgproc.circle(input, rect.center, 5, new Scalar(0, 0, 255));
for(Point p : point.toArray()){
Imgproc.circle(input, p, 5, new Scalar(0,255,0));
}
}
这是“原始”源图像:
它产生这个输出(绿色圆圈:角落;蓝色圆圈:矩形的中心):
如您所见,检测到的矩形比 9 个多。我想在 Points 数组中获取 9 个中点。
我怎样才能选择合适的?
希望你能明白我的意思