How to make shapes from images in java 2d

i have a png image of star and i want draw that shape using Line or Elippse by reading image or pixel then convert it to star shape using draw line or draw ellipse
, but there is a problems of
outliers and cluster ,

i can solve the cluster algorithm but howto solve the problems of outliers

also why iam getting ouliers if there is no black color present in the first image.

my full code is

public class GetPixelColor extends JPanel
{
static int x;
static int y;

public static void main(String args[]) throws IOException{
GetPixelColor gh = new GetPixelColor();

  JFrame frame = new JFrame("Points");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  //frame.add(points);
  frame.setSize(1600, 1600);
  frame.setLocationRelativeTo(null);
  frame.setVisible(true);
  frame.add(gh);

File file= new File(“f://sss1.jpg”);
BufferedImage image = ImageIO.read(file);
// Getting pixel color by position x and y

int x1 = 0;
int y1 = 0;
int x2 = image.getWidth();
int y2 = image.getHeight();
for( x=0;x<x2;x++)
{
for( y=0;y<y2;y++)
{

int clr= image.getRGB(x,y);
int red = (clr & 0x00ff0000) >> 16;
int green = (clr & 0x0000ff00) >> 8;
int blue = clr & 0x000000ff;

if(red>=0 && red<=1 && blue>=0 && blue<=1 && green>=0 && green<=1)
{
System.out.println("Red Color value = "+ red);
System.out.println("Green Color value = "+ green);
System.out.println("Blue Color value = "+ blue);
System.out.println("position x "+ x+“y==”+y);
gh.hello(x,y);
}

}

}

}
void hello(int x,int y)
{
repaint();
}

public void paintComponent(Graphics g) {
super.paintComponents(g);

    Paint paint = new GradientPaint(20, 0, Color.blue,
              40, getHeight(), Color.yellow, true);
      ((Graphics2D) g).setPaint(paint);
    
    Graphics2D g2d = (Graphics2D) g;

    //g2d.setColor(Color.BLACK);

Shape fg = new Rectangle2D.Double(x,y,1,1);

      g2d.fill(fg);
     // g2d.drawLine(x, y,x, y);

}
}

Quite a few of the outliers clearly aren’t pure black - many of them look partially grey (and if your algorithm is eliminating all black pixels, then all of the outliers aren’t pure black), suggesting

  1. antialiasing (the edges blend smoothly with the white background without hard, jagged edges)
  2. lossy image encoding (your file is a jpg)

Image processing algorithms can almost never rely on a pixel being totally a certain color.

Converting raster images to vector images is not an easy task, and you will probably get better answers asking in a general purpose graphics forum than asking here.

1 Like

Maybe you look how Inkscape makes this. That open source software has a function to convert pixel images into vector graphics.

found this open source java svg editor helpful

http://glipssvgeditor.sourceforge.net/