2

I have written some code in java to convert a colored image into black and white image and then tried to perform thinning on that gray-scale image. Black and white conversion is done successfully, but image thinning is still not giving correct output. Kindly help me in fixing my problem. My code is as follows:

//colored image to black and white conversion; black and white image to thinned image.

public static void main(String[] args) 
{
    try
    {
        //colored image path
        BufferedImage colored_image = ImageIO.read(new File("D:\\logo.jpg"));
        //getting width and height of image
        double image_width = colored_image.getWidth();
        double image_height = colored_image.getHeight();
        BufferedImage img = colored_image;

        //drawing a new image
        BufferedImage bimg = new BufferedImage((int)image_width, (int)image_height, BufferedImage.TYPE_BYTE_GRAY);
        Graphics2D gg = bimg.createGraphics();
        gg.drawImage(img, 0, 0, img.getWidth(null), img.getHeight(null), null);

        //saving black and white image onto drive
        String temp = "logo in blackAndwhite.jpeg";
        File fi = new File("D:\\" + temp);
        ImageIO.write(bimg, "jpg", fi);

        //thinning by resizing gray scale image to desired eight and width
        BufferedImage bimg2 = new BufferedImage((int)image_width, (int)image_height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = bimg2.createGraphics();

        // Perform your drawing here
        g2.setColor(Color.BLACK);
        g2.drawLine(0, 0, 200, 200);

       //saving thinned image onto drive
       String temp2 = "logo thinned.jpeg";
       File fi2 = new File("D:\\" + temp2);
       ImageIO.write(bimg2, "jpg", fi2);
       //g2.dispose();
    }
    catch (Exception e)
    {
        System.out.println(e);
    }       
 }

}

  • see this https://www.google.co.uk/search?aq=f&sugexp=chrome,mod=16&sourceid=chrome&ie=UTF-8&q=image+thinning#hl=en&sclient=psy-ab&q=image%20thinning%20algorithm%20java&oq=image%20thinning%20java&aq=2bK&aqi=g-K2g-bK1&aql=&gs_l=serp.11.2.0i30l2j0i8i30.7089.8913.0.11092.5.5.0.0.0.0.83.295.5.5.0...0.0.mdnARUil3Sk&pbx=1&fp=1&biw=983&bih=655&pf=p&pdl=300&bav=on.2,or.r_gc.r_pw.r_qf.,cf.osb&cad=b – Zaz Gmy Jun 26 '12 at 12:26
  • 2
    @ZazGmy Don't post LMFTFY answers or comments. If you feel the OP hasn't put in enough effort, say it outright or just ignore the question. Or post a link to a google result that looks good to you. – millimoose Jun 26 '12 at 12:39

1 Answers1

0

Check java.awt.geom.AffineTransform

AffineTransform tx = new AffineTransform();
tx.scale(20, 30);
AffineTransformOp afop = new AffineTransformOp(tx,
   AffineTransformOp.TYPE_BILINEAR);
BufferedImage bi = afop.filter(ogininal, null);
Icon icon = new ImageIcon(bi);  //here icon will be your thumbnail image
Rahul Agrawal
  • 8,913
  • 18
  • 47
  • 59
  • Hm, what sort of quality does that have? I seem to recall that naively scaling images doesn't necessarily produce good results, but that was for making photograph thumbnails. – millimoose Jun 26 '12 at 12:31
  • 1
    We had a scenario, where any size image to be stored in 20 * 30 px format, so we have used that, and it has given a good quality image as output. – Rahul Agrawal Jun 26 '12 at 12:36
  • thank-you Mr. Rahul Agrawal. But can you please tell me about the first argument of drawImage i.e. imageIn? What would be its value I mean what should I give as a first parameter in g2d.drawImage(imageIn, tx, null); Thanks and Regards! – user1476497 Jun 26 '12 at 12:52
  • Its "Image imageIn". imageIn is of Type Image – Rahul Agrawal Jun 26 '12 at 12:58
  • I really don't understand Mr. Rahul. Can you guide me please. I am not that good in JAVA actually. :-( – user1476497 Jun 26 '12 at 13:03
  • thinning is not particularly easy or difficult. But it does require a fair amount of programming that is language agnostic. Simple knowledge of loops and conditions is more than enough - no need for OOP or anything too JAVA-like if thats your worry – AruniRC Jun 26 '12 at 13:33
  • Actually I have to write a program which does thinning. That's why I need help. – user1476497 Jun 26 '12 at 13:52
  • I have copied code for your reference, we did only that much. If you need more help please refer http://blog.futuremedium.com.au/2011/01/28/a-journey-with-resizing-and-cropping-images-in-java/ http://www.mkyong.com/java/how-to-resize-an-image-in-java/ – Rahul Agrawal Jun 27 '12 at 04:58
  • You can also refer : http://stackoverflow.com/questions/9749121/java-image-rotation-with-affinetransform-outputs-black-image-but-works-well-whe – Rahul Agrawal Jun 27 '12 at 05:03