3
我想要检测Rubiks立方体的颜色。这就是我想要的:Link
我能够识别OpenCV的findContours
函数的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));
}
}
这是 “原始” 的源图像:
它产生此输出(绿色圆圈:角部;蓝色圆圈:矩形的中心) :
正如你所看到的,也有更多的DETE我想获得点数组中的九个中点。
我该如何选择合适的?
希望你能明白我的意思
您是否可以上传您的输入图像而无需任何后期处理?以便我们可以尝试我们的方法? –