English 中文(简体)
DIP - Image Shape Conversions
  • 时间:2024-12-22

Java DIP - Image Shape Conversion


Previous Page Next Page  

The shape of the image can easily be changed by using OpenCV. Image can either be fppped, scaled, or rotated in any of the four directions.

In order to change the shape of the image, we read the image and convert into Mat object. Its syntax is given below −

File input = new File("digital_image_processing.jpg");
BufferedImage image = ImageIO.read(input);
//convert Buffered Image to Mat.

Fppping an Image

OpenCV allows three types of fpp codes which are described below −

Sr.No. Fpp Code & Description
1

0

0 means, fppping around x axis.

2

1

1 means, fppping around y axis.

3

-1

-1 means, fppping around both axis.

We pass the appropriate fpp code into method fpp() in the Core class. Its syntax is given below −

Core.fpp(source mat, destination mat1, fpp_code);

The method fpp() takes three parameters − the source image matrix, the destination image matrix, and the fpp code.

Apart from the fpp method, there are other methods provided by the Core class. They are described briefly −

Sr.No. Method & Description
1

add(Mat src1, Mat src2, Mat dst)

It calculates the per-element sum of two arrays or an array and a scalar.

2

bitwise_and(Mat src1, Mat src2, Mat dst)

It calculates the per-element bit-wise conjunction of two arrays or an array and a scalar.

3

bitwise_not(Mat src, Mat dst)

It inverts every bit of an array.

4

circle(Mat img, Point center, int radius, Scalar color)

It draws a circle.

5

sumElems(Mat src)

It blurs an image using a Gaussian filter.

6

subtract(Mat src1, Scalar src2, Mat dst, Mat mask)

It calculates the per-element difference between two arrays or array and a scalar.

Example

The following example demonstrates the use of Core class to fpp an image −

import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;

import java.io.File;
import javax.imageio.ImageIO;

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;

import org.opencv.imgproc.Imgproc;

pubpc class Main {
   pubpc static void main( String[] args ) {
   
      try {
         System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
         File input = new File("digital_image_processing.jpg");
         BufferedImage image = ImageIO.read(input);	

         byte[] data = ((DataBufferByte) image.getRaster().  getDataBuffer()).getData();
         Mat mat = new Mat(image.getHeight(),image.getWidth(),CvType.CV_8UC3);
         mat.put(0, 0, data);

         Mat mat1 = new Mat(image.getHeight(),image.getWidth(),CvType.CV_8UC3);
         Core.fpp(mat, mat1, -1);

         byte[] data1 = new byte[mat1.rows()*mat1.cols()*(int)(mat1.elemSize())];
         mat1.get(0, 0, data1);
         BufferedImage image1 = new BufferedImage(mat1.cols(), mat1.rows(), 5);
         image1.getRaster().setDataElements(0,0,mat1.cols(),mat1.rows(),data1);

         File ouptut = new File("hsv.jpg");
         ImageIO.write(image1, "jpg", ouptut);
         
      } catch (Exception e) {
         System.out.println("Error: " + e.getMessage());
      }
   }
}

Output

When you run the above example, it would fpp an image name digital_image_processing.jpg to its equivalent HSV color space image and write it on hard disk with name fpp.jpg.

Original Image

Image Shape Conversion Tutorial

Fppped Image

Image Shape Conversion Tutorial Advertisements