Image Processing in Java | Set 11 (Changing orientation of image)
In this article we will use opencv to change orientation of any input image, by using the CORE.flip() method of OpenCV library.
The main idea is that an input buffered image object will be converted to a mat object and then a new mat object will be created in which the original mat object values are put after orientation modification.
For achieving the above result, we will be requiring some of the OpenCV methods:
- getRaster() – The method returns a writable raster which in turn is used to get the raw data from input image.
- put(int row, int column, byte[] data) / get(int row, int column, byte[] data) – used to read/write the raw data into a mat object.
- flip(mat mat1, mat mat2, int flip_value) – mat1 and mat2 corresponds to input and output mat objects and the flip_value decides the orientation type.flip_value can be either 0 (flipping along x-axis), 1 (flipping along y-axis), -1 (flipping along both the axis).
// Java program to illustrate orientation modification of image import java.awt.image.BufferedImage; import java.awt.image.DataBufferByte; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import org.opencv.core.Core; import org.opencv.core.CvType; import org.opencv.core.Mat; public class OrientingImage { public static void main( String[] args ) throws IOException { // loads methods of the opencv library System.loadLibrary( Core.NATIVE_LIBRARY_NAME ); // input buffered image object File input = new File("E:\\test.jpg"); BufferedImage image = ImageIO.read(input); // converting buffered image object to mat object byte[] data = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); Mat mat = new Mat(image.getHeight(),image.getWidth(),CvType.CV_8UC3); mat.put(0, 0, data); // creating a new mat object and putting the modified input mat object by using flip() Mat newMat = new Mat(image.getHeight(),image.getWidth(),CvType.CV_8UC3); Core.flip(mat, newMat, -1); //flipping the image about both axis // converting the newly created mat object to buffered image object byte[] newData = new byte[newMat.rows()*newMat.cols()*(int)(newMat.elemSize())]; newMat.get(0, 0, newData); BufferedImage image1 = new BufferedImage(newMat.cols(), newMat.rows(), 5); image1.getRaster().setDataElements(0,0,newMat.cols(),newMat.rows(),newData); File ouptut = new File("E:\\result.jpg"); ImageIO.write(image1, "jpg", ouptut); } } |
Output:
test.jpgresult.jpg
![]()
This article is contributed by Pratik Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Image Processing in Java | Set 4 (Colored image to Negative image conversion)
- Image Processing in Java | Set 3 (Colored image to greyscale image conversion)
- Image Processing in Java | Set 6 (Colored image to Sepia image conversion)
- Image Processing in Java | Set 5 (Colored to Red Green Blue Image Conversion)
- Image Processing in Java | Set 7 (Creating a random pixel image)
- Image Processing in Java | Set 8 (Creating mirror image)
- Image Processing in Java | Set 10 ( Watermarking an image )
- Getting started with Scikit-image: image processing in Python
- Image Processing In Java | Set 2 (Get and set Pixels)
- Image Processing in Java | Set 12 ( Contrast Enhancement )
- Image Processing in Java | Set 1 (Read and Write)
- Image Processing in Java | Set 9 ( Face Detection )
- Image Processing in Java | Set 14 ( Comparison of two images )
- Image Processing using OpenCV in Java | Set 13 (Brightness Enhancement)
- Image Processing using OpenCV in Java | Set 14 ( Sharpness Enhancement )





