Sunday, October 11, 2015

Load Image on Java GUI using Open CV

Finally Open CV has support for desktop java. We can directly use this feature to do so many interesting image processing applications. We can head start with trivial application loading the image on jframe using Open CV.

Create small GUI with the menu bar. Add the action for the menu clicks. Instantiate file chooser in java to enable the user to browse the image file. So that the GUI looks like as below


Once the View Image menu is clicked the image must load. divide this task into simple tasks
1. Get the image path as below
fc.getSelectedFile().getAbsolutePath()
2. Read the image data from disc as below
Mat m = Imgcodecs.imread("image location");
3. Resize the large images so that it's new size will match with the component on the jframe.
Imgproc.resize(m, dest, new Size(label.getWidth(),label.getHeight()));
4. Convert the Open CV Mat data structure to the Java Buffered Image type so that we can directly load this image on the jlabel as below

int type = BufferedImage.TYPE_BYTE_GRAY;
      if ( m.channels() > 1 )
      {
          type = BufferedImage.TYPE_3BYTE_BGR;
      }
      int bufferSize = m.channels()*m.cols()*m.rows();
      byte [] b = new byte[bufferSize];
      m.get(0,0,b); // get all the pixels
      BufferedImage image = new BufferedImage(m.cols(),m.rows(), type);
      final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
      System.arraycopy(b, 0, targetPixels, 0, b.length);
      return image;

5. load the returned image on jlabel




DC motor control with Pulse Width Modulation Part 1

DC Motor intro DC motor is a device which converts electrical energy into kinetic energy. It converts the DC power into movement. The typica...