Click here to Skip to main content
Click here to Skip to main content
Go to top

How to Convert Mat to BufferedImage & Vice Versa

, 31 Mar 2014
Rate this:
Please Sign up or sign in to vote.
In this tip, we learn how to convert Mat to BufferedImage and vice versa

Introduction

In this tip, we see how to do the conversion between Mat and BufferedImage. Mat is a data structure from OpenCV to process image. BufferedImage is a data structure from Java to store images.

Using the Code

Convert Mat to BufferedImage

Mat data structure has image data, image type (GRAY, BGR), Height, Width. In mat2Img, the following function extracts meta data from Mat data structure and gets assigned to BufferedImage. This way, Mat is assigned to BufferedImage.

public static BufferedImage mat2Img(Mat in)
    {
        BufferedImage out;
        byte[] data = new byte[320 * 240 * (int)in.elemSize()];
        int type;
        in.get(0, 0, data);

        if(in.channels() == 1)
            type = BufferedImage.TYPE_BYTE_GRAY;
        else
            type = BufferedImage.TYPE_3BYTE_BGR;

        out = new BufferedImage(320, 240, type);

        out.getRaster().setDataElements(0, 0, 320, 240, data);
        return out;
    } 

Convert BufferedImage to Mat

img2Mat function accepts BufferedImage object as parameter and returns the Mat object. Mat Object is created with 320 width and 240 height, then extract RGB values from BufferedImage object and assigned to databuff which is a one dimensional int array. databuff is right shifted to 16 , 8, 0 gets ANDED with 0XFF, then assigned to Mat object.

public static Mat img2Mat(BufferedImage in)
    {
          Mat out;
          byte[] data;
          int r, g, b;

          if(in.getType() == BufferedImage.TYPE_INT_RGB)
          {
              out = new Mat(240, 320, CvType.CV_8UC3);
              data = new byte[320 * 240 * (int)out.elemSize()];
              int[] dataBuff = in.getRGB(0, 0, 320, 240, null, 0, 320);
              for(int i = 0; i < dataBuff.length; i++)
              {
                  data[i*3] = (byte) ((dataBuff[i] >> 16) & 0xFF);
                  data[i*3 + 1] = (byte) ((dataBuff[i] >> 8) & 0xFF);
                  data[i*3 + 2] = (byte) ((dataBuff[i] >> 0) & 0xFF);
              }
          }
          else
          {
              out = new Mat(240, 320, CvType.CV_8UC1);
              data = new byte[320 * 240 * (int)out.elemSize()];
              int[] dataBuff = in.getRGB(0, 0, 320, 240, null, 0, 320);
              for(int i = 0; i < dataBuff.length; i++)
              {
                r = (byte) ((dataBuff[i] >> 16) & 0xFF);
                g = (byte) ((dataBuff[i] >> 8) & 0xFF);
                b = (byte) ((dataBuff[i] >> 0) & 0xFF);
                data[i] = (byte)((0.21 * r) + (0.71 * g) + (0.07 * b)); //luminosity
              }
           }
           out.put(0, 0, data);
           return out;
     } 

Points of Interest

  • Learn how to convert Mat to BufferedImage
  • Learn how to convert BufferedImage to Mat

Sorry

Sorry for my English. If you notice errors or can suggest a more correct version, please let me know.

History

  • 31st March, 2014 - First release

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

SOHAM_GANDHI
Software Developer
India India
I am Soham Gandhi from India. I studied BE in Information Technology at University of Pune. (www.unipune.ac.in). I have been learning OpenCV, Enjoying it.
My research interest in Image Processing, Artificial Intelligence, Security Systems, Security Metrics. I work on OpenCV, EmguCV, Visual Studio 2008/10, Java platform.
 
Home Page (Blog)
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
BugI found some mistakes and corrected it PinmemberRemon Kamal17-Aug-14 4:19 
GeneralRe: I found some mistakes and corrected it PinmemberSOHAM_GANDHI17-Aug-14 19:06 
QuestionProblems in understanding constant values used PinmemberMember 965856026-Jun-14 21:42 
AnswerRe: Problems in understanding constant values used PinmemberSOHAM_GANDHI26-Jun-14 21:51 
QuestionHelpful PinmemberCHARLIE9126-May-14 19:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 31 Mar 2014
Article Copyright 2014 by SOHAM_GANDHI
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid