1.处理为灰度
2.确定一下圆的大小
3.找出黑色点,然后根据半径计算点,统计次半径下,点数之和.
4.最大的点就是圆心所在
5.画出找出的圆
public void run(){
Pixmap pixmap = new Pixmap(Gdx.files.internal("screenshot.png"));
Image im = new Image(new Texture(pixmap));
addActor(im);
im.addAction(Actions.forever(
Actions.sequence(
Actions.fadeOut(6),
Actions.fadeIn(6)
)));
int width = pixmap.getWidth();
int height = pixmap.getHeight();
int MIN_RADIUS = 20;
int MAX_RADIUS = 21;
int[][][] accumulator = new int[width][height][MAX_RADIUS - MIN_RADIUS + 1];
ByteBuffer pixels = pixmap.getPixels();
// for (int i = 0; i < 360; i++) {
// Image image = new Image(Asset.getAsset().getTexture("white_bg.png"));
// addActor(image);
// image.setSize(1,1);
// image.setColor(Color.RED);
// image.setPosition((float) (180+MAX_RADIUS*Math.cos(i)), (float) (380+MAX_RADIUS*Math.sin(i)),Align.center);
// }
// for (int i = 0; i < height; i++) {
// for (int i1 = 0; i1 < width; i1++) {
// int pixel = pixmap.getPixel(i, i1);
// float r = (pixel >> 24 & 0xFF) / 255f;
// float g = (pixel >> 16 & 0xFF) / 255f;
// float b = (pixel >> 8 & 0xFF) / 255f;
//
//
// if (r==0&&g==0&&b==0) {
// Image image = new Image(Asset.getAsset().getTexture("white_bg.png"));
// addActor(image);
// image.setColor(Color.BLACK);
// image.setPosition(i,i1);
// }
//
// }
// }
for (int y = 0; y <height; y++) {
for (int x = 0; x < width; x++) {
int pixel = pixmap.getPixel(x, height-y-1);
float r = (pixel >> 24 & 0xFF) / 255f;
float g = (pixel >> 16 & 0xFF) / 255f;
float b = (pixel >> 8 & 0xFF) / 255f;
if (r==0&&g==0&&b==0) {
// Edge pixel
for (int rad = MIN_RADIUS; rad <= MAX_RADIUS; rad++) {
for (int theta = 0; theta < 360; theta+=1) {
int a = (int)(x - rad * Math.cos(Math.toRadians(theta)));
int b1 = (int)(y - rad * Math.sin(Math.toRadians(theta)));
if (a >= 0 && a < width-2 && b1 >= 0 && b1 < height-2) {
accumulator[a][b1][rad - MIN_RADIUS]++;
// if (accumulator[a][b1][rad - MIN_RADIUS]==1) {
// Image image = new Image(Asset.getAsset().getTexture("white_bg.png"));
// addActor(image);
// image.setSize(1, 1);
// image.setPosition(x, y, Align.center);
// image.setColor(Color.BLACK);
// image.getColor().a = 0.2f;
// }
}
}
}
// Edge pixel
// Image image = new Image(Asset.getAsset().getTexture("white_bg.png"));
// addActor(image);
// image.setPosition(y, x, Align.center);
// image.setColor(Color.BLACK);
// image.setSize(1, 1);
// image.getColor().a = 0.2f;
}else {
// System.out.println("------out-------");
}
}
}
//
for (int y = 0; y < height-4; y++) {
for (int x = 0; x < width-4; x++) {
for (int r = MIN_RADIUS; r <= MAX_RADIUS; r++) {
if (accumulator[x][y][r - MIN_RADIUS] >= 360) {
// System.out.println(accumulator[x][y][r - MIN_RADIUS]);
// System.out.println("---------------");
Image image = new Image(Asset.getAsset().getTexture("white_bg.png"));
addActor(image);
image.setSize(1,1);
image.setPosition(x,y,Align.center);
image.setColor(Color.BLACK);
Bean bean = new Bean();
bean.x = x;
bean.y = y;
bean.radius = MAX_RADIUS;
array.add(bean);
// image.setDebug(true);
// System.out.println(accumulator[x][y][r - MIN_RADIUS]);
}
}
// }
}
}
// Pixmap pixmap1 = new Pixmap(Gdx.files.internal("cirtest2.png"));
// Image image = new Image(new Texture(pixmap1));
// addActor(image);
}
private Array<Bean> array = new Array<>();
@Override
public void render(float delta) {
super.render(delta);
renderer.begin(ShapeRenderer.ShapeType.Line);
renderer.setColor(Color.RED);
for (Bean bean : array) {
renderer.circle(bean.x,bean.y,bean.radius);
}
renderer.end();
}
class Bean{
private float x;
private float y;
private float radius;
}
}