怎样用java解析一张椭圆图片,得到此椭圆的中心,有什么思想也行!

解决方案 »

  1.   

    我也很奇怪,看来是CSDN出的一个BUG了,呵呵~~
      

  2.   

    我已经解决这个问题了,谢谢观注者,虽然你们没有给出什么意见!呵呵,下面和大家分享一下代码吧!现在有5张图片,像素1024*768,每张中有一个椭圆!
    结果:找到每张图片中椭圆的中心,同时从椭圆内随机取500个点的坐标,写入文件,以便查看!import java.awt.image.*;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.util.Random;import javax.imageio.ImageIO;/**
     * 解析图片,得到所有椭圆中点的坐标
     * 
     * @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); String file = "C:/" + (i + 1) + ".jpg";
    String string = "";
    dot[i] = app.getDots(file);// 从一个图的椭圆中随机的取500个点
    for (int j = 0; j < 500; j++) {
    string += new String(" (" + dot[i][j][0] + ","
    + dot[i][j][1] + ") ");
    if ((j + 1) % 10 == 0) {
    buffered.write(string);
    buffered.newLine();
    buffered.flush();
    string = "";
    }
    }
    buffered.close();
    out.close();
    t++;
    }

    } catch (Exception e) {
    e.printStackTrace();
    }
    } /**
     * 构造函数
     */
    public ParseImage() { } // 得到图像中椭圆中心的坐标
    public long[] getImageNum(String file) {
    long arg[] = null;
    try {
    imagesrc = ImageIO.read(new File(file));// 读取图像
    BufferedImage newima = null;
    newima = imagesrc.getSubimage(0, 0, 1024, 768); 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;
    } /**
     * 读取图片,在图片中椭圆内部随机取500个点返回
     * 
     * @param file
     * @return
     */
    public int[][] getDots(String file) {
    int[][] dots = null;// 椭圆中所有的点的坐标集合
    int[][] dot = null;
    Random random = new Random();
    try {
    imagesrc = ImageIO.read(new File(file));// 读取图像
    BufferedImage newimage;
    newimage = imagesrc.getSubimage(0, 0, 1024, 768); int count = 0;// 点的计数器
    this.imWidth = newimage.getWidth();
    this.imHeight = newimage.getHeight();
    dots = new int[num[t]][2];
    dot = new int[DOT_NUMBER][2]; for (int i = 0; i < imWidth; i++)
    for (int j = 0; j < imHeight; j++) {
    if (isImage(newimage, i, j) && count < num[t]) {
    // 是椭圆中的点
    dots[count][0] = i;
    dots[count][1] = j;
    count++;
    }
    }
    // count=0;//计数器清零
    // 从椭圆中所有点中随机取出500个点的坐标
    for (int i = 0; i < DOT_NUMBER; i++) {
    int temp = random.nextInt(num[t]);
    dot[i][0] = dots[temp][0];
    dot[i][1] = dots[temp][1];
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    return dot;
    } }