Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Image Processing in Java - Creating a Mirror Image
Next article icon

Image Processing in Java - Changing Orientation of Image

Last Updated : 13 Jan, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Prerequisites:

  • Image Processing in Java - Read and Write
  • Image Processing In Java - Get and Set Pixels
  • Image Processing in Java - Colored Image to Grayscale Image Conversion
  • Image Processing in Java - Colored Image to Negative Image Conversion
  • Image Processing in Java - Colored to Red Green Blue Image Conversion
  • Image Processing in Java - Colored Image to Sepia Image Conversion
  • Image Processing in Java - Creating a Random Pixel Image
  • Image Processing in Java - Creating a Mirror Image
  • Image Processing in Java - Face Detection
  • Image Processing in Java - Watermarking an Image

Changing the Orientation of Image

Here we will use OpenCV to change the orientation of any input image, by using the CORE.flip() method of the 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 the input image.
  • put(int row, int column, byte[] data) or 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 correspond to input and output mat objects and the flip_value decides the orientation type.flip_value can be either 0 (flipping along the x-axis), 1 (flipping along the y-axis), -1 (flipping along both the axis).

Implementation: Let the sample image be as follows:

Example:

Java
// Java program to Illustrate How to Change // Orientation of an Image  // Importing required classes 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;  // Main class // OrientingImage public class GFG {      // Main driver method     public static void main(String[] args)         throws IOException     {          // Loading methods of the opencv library         System.loadLibrary(Core.NATIVE_LIBRARY_NAME);          // Passing the input image as path from local         // directory by creating object of File class         File input = new File("E:\\test.jpg");          // Reading image         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         // using flip() method         Mat newMat             = new Mat(image.getHeight(), image.getWidth(),                       CvType.CV_8UC3);          // Fipping the image about both axis         Core.flip(mat, newMat, -1);          // 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);          // Storing the output image by         // creating object of File class         File output = new File("E:\\result.jpg");         ImageIO.write(image1, "jpg", output);     } } 


Output:

Note: Do not scale for resolution as the output image is appended bigger to perceive better, it will be generated on IDE of same size as that of resolution and size passed of the input image. 


Next Article
Image Processing in Java - Creating a Mirror Image

P

Pratik Agarwal
Improve
Article Tags :
  • Java
  • Image-Processing
Practice Tags :
  • Java

Similar Reads

  • Image Processing in Java - Creating a Mirror Image
    Prerequisite:  Image Processing in Java - Read and WriteImage Processing In Java - Get and Set PixelsImage Processing in Java - Colored Image to Grayscale Image ConversionImage Processing in Java - Colored Image to Negative Image ConversionImage Processing in Java - Colored to Red Green Blue Image C
    3 min read
  • Image Processing in Java - Comparison of Two Images
    Prerequisites: Image Processing in Java - Read and WriteImage Processing In Java - Get and Set PixelsImage Processing in Java - Colored Image to Grayscale Image ConversionImage Processing in Java - Colored Image to Negative Image ConversionImage Processing in Java - Colored to Red Green Blue Image C
    4 min read
  • Image Processing in Java - Creating a Random Pixel Image
    Prerequisites: Image Processing in Java - Read and WriteImage Processing In Java - Get and Set PixelsImage Processing in Java - Colored Image to Grayscale Image ConversionImage Processing in Java - Colored Image to Negative Image ConversionImage Processing in Java - Colored to Red Green Blue Image C
    2 min read
  • Image Processing in Java - Colored image to Negative Image Conversion
    Prerequisites: Image Processing in Java - Read and WriteImage Processing In Java - Get and Set PixelsImage Processing in Java - Colored image to Grayscale Image Conversion In this set, we will be converting a colored image to a negative image.  Colored Image (RGB Color Model) - The RGB color model i
    3 min read
  • Image Processing In Java - Get and Set Pixels
    Prerequisite - Image Processing in Java - Read and Write In this set, we will learn about the pixels of images, how we can get pixel values of an image and how to set pixel values in an image using Java programming language. Pixels are the smallest unit of an image which consists of four components
    3 min read
  • Image Processing in Java - Face Detection
    Prerequisites: Image Processing in Java - Read and WriteImage Processing In Java - Get and Set PixelsImage Processing in Java - Colored Image to Grayscale Image ConversionImage Processing in Java - Colored Image to Negative Image ConversionImage Processing in Java - Colored to Red Green Blue Image C
    3 min read
  • Image Processing in Java - Watermarking an Image
    Prerequisites: Image Processing in Java - Read and WriteImage Processing In Java - Get and Set PixelsImage Processing in Java - Colored Image to Grayscale Image ConversionImage Processing in Java - Colored Image to Negative Image ConversionImage Processing in Java - Colored to Red Green Blue Image C
    3 min read
  • Image Processing in MATLAB | Fundamental Operations
    1. Reading Images Images are read into the MATLAB Environment using imread() function which takes filename with applicable extension as the argument For Example: >> I=imread('nature.jpg'); This will read JPEG image 'nature' into the image array. Note: The semicolon(;) at the end of command lin
    3 min read
  • Image Processing in Java - Contrast Enhancement
    Prerequisites: Image Processing in Java - Read and WriteImage Processing In Java - Get and Set PixelsImage Processing in Java - Colored Image to Grayscale Image ConversionImage Processing in Java - Colored Image to Negative Image ConversionImage Processing in Java - Colored to Red Green Blue Image C
    2 min read
  • Image Processing in Java - Colored Image to Sepia Image Conversion
    Prerequisites:  Image Processing in Java - Read and WriteImage Processing In Java - Get and Set PixelsImage Processing in Java - Colored Image to Grayscale Image ConversionImage Processing in Java - Colored Image to Negative Image ConversionImage Processing in Java - Colored to Red Green Blue Image
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences