blEncodeAndDecode — Simple functions to encode and decode images in memory using OpenCV without having to save/read to disk

This entry is part 16 of 17 in the series blImageAPI -- BarbatoLabs Image API

Introduction

I have seen many of these questions pop up everywhere on the web, like this stackoverflow.com question. But as it turns out, there are two very easy to use functions in opencv that allow us to encode/decode to/from several formats such as jpeg, png, pxm and many more.

In this very short article, I first show you how to use them, and then wrap these two functions to easily use them with the blImageAPI.

How to encode/decode using opencv raw functions

The declarations of the encode and decode functions look like the following:

CvMat* cvEncodeImage(const char* ext,const CvArr* image,const int* params = 0)
IplImage* cvDecodeImage(const CvMat* buf,int iscolor = CV_LOAD_IMAGE_COLOR)

The following snippet will give you an idea of how to use them. It simply loads an image from file, it encodes it, and then decodes it. The decoded image looks just like the original.

// First we load an image from file
    IplImage* MyImage = NULL;
    MyImage = cvLoadImage("MyImageFile.jpg",CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR,);

    // We then encode it into a CvMat structure
    CvMat* EncodedImage = cvEncodeImage(".jpg",MyImage);

    // If we want to decode the
    // image later in the code, we
    // do the following
    IplImage* DecodedImage = cvDecodeImage(EncodedImage);

Wrapping the code to work nicely with blImage

Making the above work with blImage is very easy and I have created two functions to do just that, EncodeImage and DecodeImage, saved them in a file called blEncodeAndDecode.hpp as follows:

blEncodeAndDecode.hpp (Click to see…)

Usage

Using the two functions could not be any easier. The following snippet loads an image from file, encodes it and then decodes it, and shows them respectively. Note that the images can be of different types, they can be of color or grayscale, but not necessarily have to be of the same type (unlike the raw opencv functions I show above)

#include "blImageAPI/blImageAPI.hpp"
using namespace blImageAPI;

int main(int argc, char *argv[])
{
    blImage< blColor3 > MyImage;
    MyImage.LoadImageFromFile("C:/Documents and Settings/VBarbato/Desktop/HairTest.jpg");

    cvShowImage("Original",MyImage);

    // First we encode the image
    blImage EncodedImage;
    EncodeImage(MyImage,EncodedImage,".png");

    cvShowImage("Encoded",EncodedImage);

    // Second we decode the image
    blImage< blColor3 > DecodedImage;
    DecodeImage(EncodedImage,DecodedImage);

    cvShowImage("Decoded",DecodedImage);

    cout << "Size of Original image = " << MyImage.GetSizeInBytes() << " bytes\n";
    cout << "Size of Encoded image = " << EncodedImage.GetSizeInBytes() << " bytes\n";
    cout << "Size of Decoded image = " << DecodedImage.GetSizeInBytes() << " bytes\n";

    cvWaitKey(0);
}

The output of the program looks like the following:


The console output shows that the encoded image is smaller than the original, and that the original is the same size as the decoded image:

Downloads

I have put all the files into a zip file which can be downloaded here. ¬†All you have to do is extract it somewhere, let’s say in a directory called blImageAPI, and then include the blImageAPI.hpp file as follows:


#include "blImageAPI/blImageAPI.hpp"

using namespace blImageAPI;

Note: Everything is declared in a namespace blImageAPI, and such you would use it as: blImageAPI::blImage

blImageAPI.zip -- Ver Jun/06/2011 1:43pm (4992)

Updates

Updates regarding these functions will be listed here

  1. Jun/06/2011 — Updated the “DecodeImage” function to check for a NULL pointer caused by an image of size zero.

About Vincenzo Barbato

Known to his friends as Enzo, he's an outside-the-box engineer/researcher whose interests and expertise span many fields, including controls systems, multi-physics simulations, mechatronics, oil technologies, data analysis and machine vision just to name a few.

Refusing to grow up, he's on a continuous journey to develop simple and creative solutions that have the power of disrupting industries by optimizing systems and processes.

Married to a beautiful wife, with two beautiful daughters and two identical twin boys, his home is a never ending chaotic fountain of inspiration.

His outlook on life:

"Never blindly accept what you're told, listen, but then question, with curiosity, creativity and collaboration we can change the world"

About Enzo

Known to his friends as Enzo, he's an outside-the-box engineer/researcher whose interests and expertise span many fields, including controls systems, multi-physics simulations, mechatronics, oil technologies, data analysis and machine vision just to name a few. Refusing to grow up, he's on a continuous journey to develop simple and creative solutions that have the power of disrupting industries by optimizing systems and processes. Married to a beautiful wife, with two beautiful daughters and two identical twin boys, his home is a never ending chaotic fountain of inspiration. His outlook on life: "Never blindly accept what you're told, listen, but then question, with curiosity, creativity and collaboration we can change the world"

5 Comments

  1. …私は純粋ので、将来内で あなたの記事の読みしたいと考えていますウェブログ私はあなたがお気に入りに追加されました。設計|、私はそれを共有したい他の人だから私はしかし、私はちょうど私がこの1 1のような品質のウェブサイトのを見ると..ああ、いや、それをブックマークできませんでした公式サイトあなたインターネットサイト(から…

  2. 私がしたかったあなたが知っているあなたが議論してきたものは助けるために私のパートナーと私は感謝人の生活を向上させる方法事項|この対象で女の子 |男性}と女性 領域内の専門家 {。 仕事本当にあなたの優れた非常に良い良いへの感謝の気持ち| それはそれはです。おかげでノーベルコーリングカードこんにちは

  3. 私としては、ここで材料である実際 素晴らしいあなたの努力のために、よろしくサイト私はコンテンツ素材コンテンツは信じ所有。いくつかの純粋素晴らしい素晴らしい

  4. どのようにそれだけで誰もがブログを書いて、このように人気を得ることができるのですか?そのyouveはもっとホードような信じられないほど印象的なものは、あなたがについて何も知らない問題よりもかなり絵を描いたと述べた好きではありません!私はここで、平均音にしたいいけません。しかし、あなたは真剣にあなたには、いくつかの非常に写真を追加し、純粋に何も言わないで逃げることができると思いますか?このことについて話をする時間を割いて

  5. イェーイGoogleは私の王はサイトこの優れたサイトを発見!。インターネットサイトリアル{素晴らしい

Leave a Reply

Your email address will not be published. Required fields are marked *