如何使用霍夫变换对图像中的圆进行计数?

时间:2019-04-25 02:53:04

标签: java hough-transform

我正在使用Java来计数图像中的骰子。我首先应用了sobel边缘检测滤波器,然后进行了霍夫圆变换。我的霍夫圆变换不起作用。我该如何解决?

public static int countCircles(BufferedImage bi) {
    int sum=0;
    for (int r = 10; r < 50; r++) {
        for (int x = r; x < bi.getWidth() - r; x++) {
            for (int y = r; y < bi.getHeight() - r; y++) {

                int score=0;

                for (int theta=0; theta < 360; theta +=10){
                    int checkX = x + (int) (r*Math.cos(Math.toRadians(theta)));
                    int checkY = y + (int) (r*Math.sin(Math.toRadians(theta)));
                    if(checkX < 0 || checkX >= bi.getWidth() ||  checkY < 0  || checkY >= bi.getHeight()){
                        break;
                    }

                    Color c = intToColor(bi.getRGB(checkX, checkY));
                    score+=c.getRed();
                }
                if(score > 300){
                    sum++;
                }
            }
        }
    }

    return sum;
}
public static Color intToColor(int argb) {
    int a = (argb >> 24) & 0xff;
    int r = (argb >> 16) & 0xff;
    int g = (argb >> 8) & 0xff;
    int b = argb & 0xff;
    return new Color(r,g,b,a);
}

0 个答案:

没有答案
相关问题