/**
* 解析图片,得到所有椭圆中点的坐标
*
* @author zhoubols
*
*/
public class ParseImage {
private final static int DOT_NUMBER = 500;
private BufferedImage imagesrc = null;//
private int imWidth, imHeight;// 图像的宽和高
private int num[] = new int[5];// 记录每个区域中是椭圆中点的个数
private static int t = 0;// 计数器
public static void main(String[] args) {
int count = 0;// 计数器
double result[] = new double[5];
try {
ParseImage app = new ParseImage();
for (int i = 0; i < 5; i++) {
String file = "C:/" + (i + 1) + ".jpg";
// System.out.println(file);
long locate[] = app.getImageNum(file);// 得到识别字符串
System.out.println("第" + (i + 1) + "个椭圆的中心坐标是:(" + locate[0]
+ "," + locate[1] + ")");
t++;
}
/*-------------下面随机得到每个图中椭圆内部500个点的坐标,并写入个文件中------------*/
File outfile = null;
FileWriter out = null;
int dot[][][] = new int[5][][];
t = 0;
for (int i = 0; i < 5; i++) {
outfile = new File("C:/data" + (i + 1) + ".txt");
out = new FileWriter(outfile);
BufferedWriter buffered = new BufferedWriter(out);
long countX = 0;// 横坐标方向像素累加计数器
long countY = 0;// 纵坐标方向像素累加计数器
arg = new long[2];// 表示椭圆的中心坐标
int count = 0;// 记录一个区域中是椭圆中点的个数
// ImageIO.write(newima, "JPEG", new File("C:/test" + t + ".JPEG"));
this.imWidth = newima.getWidth();
this.imHeight = newima.getHeight();
for (int i = 0; i < imWidth; i++)
for (int j = 0; j < imHeight; j++) {
if (isImage(newima, i, j)) {
// 此点是椭圆中的点
countX += i;// 横坐标方向像素累加
countY += j;// 纵坐标方向像素累加
count += 1;
}
}
if (count != 0) {// 得到中点坐标
arg[0] = countX / count;
arg[1] = countY / count;
}
num[t] = count;
System.out.println("第" + (t + 1) + "个椭圆中一共有" + count + "个点");
} catch (Exception e) {
e.printStackTrace();
}
return arg;
}
/**
* 判断(x,y)这点是不是椭圆中的点
*
* @param bufferedImage
* @param x
* @param y
* @return
*/
public boolean isImage(BufferedImage bufferedImage, int x, int y) {
int rgb = bufferedImage.getRGB(x, y);
// 得到三元色
int r = (rgb & 16711680) >> 16;
int g = (rgb & 65280) >> 8;
int b = (rgb & 255);
// 此点是椭圆中的点
if (r < 50 && g < 50 && b < 50) {
return true;
}
return false;
}