Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #15600 > unrolled thread

Image Thinning using JAVA

Started bysumera <kanwal.sumera@yahoo.com>
First post2012-06-26 11:50 -0500
Last post2012-06-27 07:01 -0700
Articles 8 — 5 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Image Thinning using JAVA sumera <kanwal.sumera@yahoo.com> - 2012-06-26 11:50 -0500
    Re: Image Thinning using JAVA markspace <-@.> - 2012-06-26 10:21 -0700
    Re: Image Thinning using JAVA "John B. Matthews" <nospam@nospam.invalid> - 2012-06-26 14:39 -0400
    Re: Image Thinning using JAVA Knute Johnson <nospam@knutejohnson.com> - 2012-06-26 14:26 -0700
      Re: Image Thinning using JAVA "John B. Matthews" <nospam@nospam.invalid> - 2012-06-26 23:15 -0400
        Re: Image Thinning using JAVA Knute Johnson <nospam@knutejohnson.com> - 2012-06-26 22:04 -0700
          Re: Image Thinning using JAVA "John B. Matthews" <nospam@nospam.invalid> - 2012-06-27 21:35 -0400
    Re: Image Thinning using JAVA Roedy Green <see_website@mindprod.com.invalid> - 2012-06-27 07:01 -0700

#15600 — Image Thinning using JAVA

Fromsumera <kanwal.sumera@yahoo.com>
Date2012-06-26 11:50 -0500
SubjectImage Thinning using JAVA
Message-ID<csqdnUsK6PaldXTSnZ2dnUVZ_vednZ2d@giganews.com>
Hi!
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);
    }       
 }

[toc] | [next] | [standalone]


#15602

Frommarkspace <-@.>
Date2012-06-26 10:21 -0700
Message-ID<jscr37$i17$1@dont-email.me>
In reply to#15600
On 6/26/2012 9:50 AM, sumera wrote:
> Hi! 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.


What would you consider correct output, if the conversion is successful?



[toc] | [prev] | [next] | [standalone]


#15604

From"John B. Matthews" <nospam@nospam.invalid>
Date2012-06-26 14:39 -0400
Message-ID<nospam-D9C64E.14394326062012@news.aioe.org>
In reply to#15600
In article <csqdnUsK6PaldXTSnZ2dnUVZ_vednZ2d@giganews.com>,
 sumera <kanwal.sumera@yahoo.com> wrote:

> 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:

AffineTransformOp works well for scaling an image, as it allows control 
over the interpolation type. There's an example here:

<https://sites.google.com/site/trashgod/scaled>

-- 
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

[toc] | [prev] | [next] | [standalone]


#15617

FromKnute Johnson <nospam@knutejohnson.com>
Date2012-06-26 14:26 -0700
Message-ID<jsd9em$hur$1@dont-email.me>
In reply to#15600
On 6/26/2012 9:50 AM, sumera wrote:
> Hi!
> 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);
>      }
>   }
>
>

     public static BufferedImage convertToGray(BufferedImage image) {
         BufferedImage gray = new BufferedImage(image.getWidth(),
          image.getHeight(),BufferedImage.TYPE_BYTE_GRAY);
         ColorConvertOp op = new ColorConvertOp(
          image.getColorModel().getColorSpace(),
          gray.getColorModel().getColorSpace(),null);
         op.filter(image,gray);
         return gray;
     }

You can use the same technique as above with an AffineTransformOp, as 
John Matthews mentioned, to scale an image.

-- 

Knute Johnson

[toc] | [prev] | [next] | [standalone]


#15650

From"John B. Matthews" <nospam@nospam.invalid>
Date2012-06-26 23:15 -0400
Message-ID<nospam-D8169E.23155626062012@news.aioe.org>
In reply to#15617
In article <jsd9em$hur$1@dont-email.me>,
 Knute Johnson <nospam@knutejohnson.com> wrote:

>     public static BufferedImage convertToGray(BufferedImage image) {
>         BufferedImage gray = new BufferedImage(image.getWidth(),
>             image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
>         ColorConvertOp op = new ColorConvertOp(
>             image.getColorModel().getColorSpace(),
>             gray.getColorModel().getColorSpace(), null);
>         op.filter(image, gray);
>         return gray;
>     }

Thanks for weighing in on this. Your approach has always worked 
flawlessly on JPG images, but I had trouble with a PNG file: the result 
was unusually dark, and a subsequent call to gray.getGrapics() failed. 
I'd welcome any insight you can offer.

> You can use the same technique as above with an AffineTransformOp, as 
> John Matthews mentioned, to scale an image.

I had good results with AffineTransformOp.TYPE_NEAREST_NEIGHBOR for 
down sampling:

<https://sites.google.com/site/trashgod/scaled>

As recently suggested by BGB:

<https://groups.google.com/d/msg/comp.lang.java.programmer/zH_xK85o2mA/V--P6ruObwUJ>

-- 
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

[toc] | [prev] | [next] | [standalone]


#15653

FromKnute Johnson <nospam@knutejohnson.com>
Date2012-06-26 22:04 -0700
Message-ID<jse48p$pku$1@dont-email.me>
In reply to#15650
On 6/26/2012 8:15 PM, John B. Matthews wrote:
> In article <jsd9em$hur$1@dont-email.me>,
>   Knute Johnson <nospam@knutejohnson.com> wrote:
>
>>      public static BufferedImage convertToGray(BufferedImage image) {
>>          BufferedImage gray = new BufferedImage(image.getWidth(),
>>              image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
>>          ColorConvertOp op = new ColorConvertOp(
>>              image.getColorModel().getColorSpace(),
>>              gray.getColorModel().getColorSpace(), null);
>>          op.filter(image, gray);
>>          return gray;
>>      }
>
> Thanks for weighing in on this. Your approach has always worked
> flawlessly on JPG images, but I had trouble with a PNG file: the result
> was unusually dark, and a subsequent call to gray.getGrapics() failed.
> I'd welcome any insight you can offer.
>
>> You can use the same technique as above with an AffineTransformOp, as
>> John Matthews mentioned, to scale an image.
>
> I had good results with AffineTransformOp.TYPE_NEAREST_NEIGHBOR for
> down sampling:
>
> <https://sites.google.com/site/trashgod/scaled>
>
> As recently suggested by BGB:
>
> <https://groups.google.com/d/msg/comp.lang.java.programmer/zH_xK85o2mA/V--P6ruObwUJ>
>

You got me interested on that one.  I made a really simple test program 
because of time constraints.

What I found was that if you just did a ColorConvertOP to a PNG or a 
JPEG image, the image was in fact fairly dark.  But if you then convert 
that image to a compatible image it looks really good in gray scale.

Here's the simple code.

package com.knutejohnson.test;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

import com.knutejohnson.classes.ImageUtilities;

public class PNGtoGray extends JPanel implements ActionListener {
     private BufferedImage bi;

     public PNGtoGray(BufferedImage bi) {
         this.bi = bi;

         setPreferredSize(new Dimension(bi.getWidth(),bi.getHeight()));
     }

     public void actionPerformed(ActionEvent ae) {
         bi = ImageUtilities.convertToGray(bi);
         bi = ImageUtilities.convertToCompatible(bi);
         repaint();
     }

     public void paintComponent(Graphics g) {
         g.drawImage(bi,0,0,null);
     }

     public static void main(final String[] args) {
         EventQueue.invokeLater(new Runnable() {
             public void run() {
                 try {
                     BufferedImage bi = ImageIO.read(new File(args[0]));
                     JFrame f = new JFrame("PNGtoGray");
                     f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                     PNGtoGray ptg = new PNGtoGray(bi);
                     f.add(ptg,BorderLayout.CENTER);
                     JButton b = new JButton("Conver to Gray");
                     b.addActionListener(ptg);
                     f.add(b,BorderLayout.SOUTH);
                     f.pack();
                     f.setVisible(true);
                 } catch (IOException ioe) {
                     System.out.println(ioe);
                 }
             }
         });
     }
}

package com.knutejohnson.classes;

import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.io.*;
import java.util.*;
import javax.imageio.*;
import javax.imageio.stream.*;
import javax.imageio.plugins.jpeg.*;

public class ImageUtilities {
     public static void writeJPEG(RenderedImage image, float quality, 
File file)
      throws IOException {
         if (quality < 0.0f || quality > 1.0f)
             throw new IllegalArgumentException("0.0 < Quality < 1.0");
         ImageWriter writer = null;
         Iterator iter = ImageIO.getImageWritersByFormatName("JPEG");
         if (!iter.hasNext())
             throw new IOException("No Writers Available");
         writer = (ImageWriter)iter.next();
         if (file.exists())
             file.delete();
         ImageOutputStream ios = ImageIO.createImageOutputStream(file);
         writer.setOutput(ios);
         JPEGImageWriteParam iwp = new JPEGImageWriteParam(null);
         iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
         iwp.setCompressionQuality(quality);
         writer.write(null,new IIOImage(image,null,null),iwp);
         ios.flush();
         writer.dispose();
         ios.close();
     }

     public static BufferedImage convertToGray(BufferedImage image) {
         BufferedImage gray = new BufferedImage(image.getWidth(),
          image.getHeight(),BufferedImage.TYPE_BYTE_GRAY);
         ColorConvertOp op = new ColorConvertOp(
          image.getColorModel().getColorSpace(),
          gray.getColorModel().getColorSpace(),null);
         op.filter(image,gray);
         return gray;
     }

     public static BufferedImage scaleImage(BufferedImage src, double sx,
      double sy, int interpolationType) {
         AffineTransformOp op = new AffineTransformOp(
          AffineTransform.getScaleInstance(sx,sy),interpolationType);
         return op.filter(src,null);
     }

     public static BufferedImage scaleImage(BufferedImage src, double sx,
      double sy, RenderingHints hints) {
         AffineTransformOp op = new AffineTransformOp(
          AffineTransform.getScaleInstance(sx,sy),hints);
         return op.filter(src,null);
     }

     public static BufferedImage convertToCompatible(BufferedImage image) {
         GraphicsEnvironment ge =
          GraphicsEnvironment.getLocalGraphicsEnvironment();
         GraphicsDevice gd = ge.getDefaultScreenDevice();
         GraphicsConfiguration gc = gd.getDefaultConfiguration();

         BufferedImage compatible = 
gc.createCompatibleImage(image.getWidth(),
          image.getHeight());

         if (compatible.getType() == image.getType())
             return image;

         ColorConvertOp op = new ColorConvertOp(
          image.getColorModel().getColorSpace(),
          compatible.getColorModel().getColorSpace(),null);

         return op.filter(image,compatible);
     }
}


-- 

Knute Johnson

[toc] | [prev] | [next] | [standalone]


#15691

From"John B. Matthews" <nospam@nospam.invalid>
Date2012-06-27 21:35 -0400
Message-ID<nospam-C6CEE4.21351427062012@news.aioe.org>
In reply to#15653
In article <jse48p$pku$1@dont-email.me>,
 Knute Johnson <nospam@knutejohnson.com> wrote:

> What I found was that if you just did a ColorConvertOP to a PNG or a 
> JPEG image, the image was in fact fairly dark.  But if you then convert 
> that image to a compatible image it looks really good in gray scale.

Having a compatible image was the key; thank you.

-- 
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

[toc] | [prev] | [next] | [standalone]


#15659

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-06-27 07:01 -0700
Message-ID<ih4mu71i5tnv4g181st5htcihq3aqahj81@4ax.com>
In reply to#15600
On Tue, 26 Jun 2012 11:50:00 -0500, sumera <kanwal.sumera@yahoo.com>
wrote, quoted or indirectly quoted someone who said :

>Hi!
>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:

How do you define thinning?  Shrinking the image by deleting every
second pixel? Trying to make a JPG smaller?

-- 
Roedy Green Canadian Mind Products
http://mindprod.com
When you get stuck trying to solve a computer program: 
1. Go into the kitchen and make coffee.
2. If that fails, go for a walk.
3. If that fails, take a nap.
Why? To avoid being swamped with details, to see the big picture,
to allow in some random noise to kick you out of your thinking rut.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web