Click here to Skip to main content
15,868,292 members
Articles / Programming Languages / C++
Article

Mango, Photo Editor with OpenCV

Rate me:
Please Sign up or sign in to vote.
4.95/5 (17 votes)
10 Dec 2008CPOL3 min read 112.5K   10.3K   50   28
An article on implementing basic use of OpenCV to manipulate pictures

Introduction

I made a simple but nice photo editor with OpenCV. It can be used to edit photos, such as, adding simple effects, changing the brightness and the contrast, histogram equalization, face detection, etc. For the GUI, I use wxWidgets so it can be easily ported to the Linux operating system.

Background

OpenCV is an open source computer vision library in C/C++. It provides us many APIs for image data manipulation. It is OS independent and optimized for real-time applications. Go to the OpenCV website and download the latest version. Install and check sample directory. Find some tutorials and enjoy.

wxWidgets is an OS independent open source GUI library. By using this library we can make an OS independent application. Check here at the wxWidgets website.

The Plan

Here is the plan. We are going to make a photo editor software that is capable of:

  • Converting an image to grayscale and black and white.
  • Do per channel adjustment of brightness and contrast based on its color space.
  • Change color space: RGB, YCrCb, HSV, and HLS.
  • Rotate, flip, or resize an image.
  • Do histogram equalization.
  • Show histogram graph.
  • Detect faces.
  • Generate erode and dilate effects.
  • Have "UNDO" capability.

The Code

The class for image manipulation is allocated in files mango.h and mango.cpp with name CMango.

C++
class CMango
{

public:
    enum type{
        IM_AUTOMATIC,
        IM_GRAYSCALE,
        IM_BLACKNWHITE,
        IM_RGB,
        IM_HSV,
        IM_HLS,
        IM_YCrCb,
        IM_ZOOMIN,
        IM_ZOOMOUT,
        IM_HORIZONTAL = 25,
        IM_VERTICAL,
        IM_DILATE = 40,
        IM_ERODE,
        IM_FACE1,
    };

    CMango(void);
    ~CMango(void);

    int LoadImageFromFile(char *FileName);
    void ShowActiveImage(char *WindowName);
    int SetProperty();
    std::string GetProperty();
    void LoadPrestoredImage(int type);
    void PushImage(IplImage *Image){ImageList.push_front(Image);};
    void PopImage(){ if (!ImageList.empty()) ImageList.pop_front();};
    int SaveToFile(char *FileName);

    void Rotate(int angle);
    void ZoomImage(int flag);
    int GetImageElement(int channel, int brightness, int contrast);
    void FlipImage(int flag);
    void ApplyEffects(int flag);
    void FaceDetect(int flag);
    void ShowHistogram();
    int HistogramEqualization();
    int ChangeColorSpace(int flag);

private:
    static IplImage *Automatic;
    static IplImage *Blacknwhite;
    static IplImage *Grayscale;
    static IplImage *Buf;

    static std::list<IplImage *> ImageList;
    static std::list<std::string> PropertyList;

    static int ColorSpace;
};

When an image is loaded, it is kept in data member Automatic. Actually we have three pre-stored images here: Automatic, Blacknwhite, and Grayscale. Every operation we perform will push an active image (image that is being displayed) to ImageList so we can perform "undo." The "undo" command basically will popup an image from ImageList to be displayed.

I also did some experiments on color spaces. Adjusting the per-channel brightness and contrast of an image can give us an awesome effect depending on its color spaces. I used an algorithm that I found in this site. Since OpenCV will only put RGB on display, I have to do these: Convert image to target color space, do the adjustment that I want, and finally, convert it back to RGB to be displayed.

Unfortunately, I encountered a problem. The problem is that the algorithm is for RGB color space. I can't just use it for other color spaces. But, take a look at this:

  Range
RGB R 0 - 1
 G 0 - 1
 B 0 - 1
YCrCb Y 0 - 1
 Cr -0.5 - 0.5
 Cb -0.5 - 0.5
HSV H 0-360
 S 0 - 1
 V 0 - 1
HLS H 0-360
 L 0 - 1
 S 0 - 1

Based on this table I think it is possible to apply the same algorithm for other color spaces besides RGB as long as they are in the same range.

Please notice that I only implemented the histogram curve for RGB color space only. If you change the color space, the histogram curve is no longer valid because of difference in range.

Points of Interest

I implemented many basic functions of OpenCV here. I hope It might be useful for you to start learning about OpenCV.

References

License

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


Written By
Student
Indonesia Indonesia
http://kataauralius.com/

Comments and Discussions

 
GeneralError: Cannot Initialize OLE Pin
Giampy8215-Oct-09 13:43
Giampy8215-Oct-09 13:43 
Hi,
Sorry for my english, I encountered the error describe in the subject of this message when I use Opencv and wxwidget together in codeblocks for WindowsXP. So I Think that you can help me. I use wxWidget 2.8 and Opencv 1.1. Can you help me?
Thanks a lot
GeneralRe: Error: Cannot Initialize OLE Pin
auralius manurung15-Oct-09 22:39
auralius manurung15-Oct-09 22:39 
Generalat compilation: can't find wx/foldbar/foldpanelbar.h [modified] Pin
LudoTW23-Apr-09 16:38
LudoTW23-Apr-09 16:38 
GeneralRe: at compilation: can't find wx/foldbar/foldpanelbar.h Pin
auralius manurung1-May-09 3:34
auralius manurung1-May-09 3:34 
GeneralRe: at compilation: can't find wx/foldbar/foldpanelbar.h Pin
ollydbg2313-May-09 15:32
ollydbg2313-May-09 15:32 
GeneralLinux/Ubuntu doesnt work Pin
Dansveen20-Jan-09 6:06
Dansveen20-Jan-09 6:06 
GeneralRe: Linux/Ubuntu doesnt work Pin
auralius manurung22-Jan-09 19:08
auralius manurung22-Jan-09 19:08 
GeneralRe: Linux/Ubuntu doesnt work Pin
Dansveen23-Jan-09 1:41
Dansveen23-Jan-09 1:41 
GeneralRe: Linux/Ubuntu doesnt work Pin
auralius manurung23-Jan-09 15:30
auralius manurung23-Jan-09 15:30 
GeneralRe: Linux/Ubuntu doesnt work Pin
Dansveen24-Jan-09 11:42
Dansveen24-Jan-09 11:42 
GeneralRe: Linux/Ubuntu doesnt work Pin
auralius manurung26-Jan-09 11:47
auralius manurung26-Jan-09 11:47 
GeneralI can't build it in codeblocks and mingw Pin
ollydbg2324-Dec-08 2:53
ollydbg2324-Dec-08 2:53 
GeneralRe: I can't build it in codeblocks and mingw Pin
auralius manurung25-Dec-08 9:32
auralius manurung25-Dec-08 9:32 
GeneralRe: I can't build it in codeblocks and mingw Pin
ollydbg2325-Dec-08 15:29
ollydbg2325-Dec-08 15:29 
QuestionRe: I can't build it in codeblocks and mingw Pin
ginger_tosser13-May-09 10:08
ginger_tosser13-May-09 10:08 
AnswerRe: I can't build it in codeblocks and mingw Pin
ollydbg2313-May-09 15:30
ollydbg2313-May-09 15:30 
GeneralRe: I can't build it in codeblocks and mingw Pin
ginger_tosser13-May-09 20:43
ginger_tosser13-May-09 20:43 
QuestionDid you use OpenCV? Pin
Xiaoming Huang21-Dec-08 22:40
Xiaoming Huang21-Dec-08 22:40 
AnswerRe: Did you use OpenCV? Pin
ollydbg2324-Dec-08 2:05
ollydbg2324-Dec-08 2:05 
GeneralGerat article! Pin
ollydbg2316-Dec-08 2:25
ollydbg2316-Dec-08 2:25 
GeneralRe: Gerat article! Pin
auralius manurung16-Dec-08 15:31
auralius manurung16-Dec-08 15:31 
GeneralRe: Gerat article! Pin
ollydbg2317-Dec-08 3:22
ollydbg2317-Dec-08 3:22 
GeneralThe demo cannot start on WinXP Pin
Tage Lejon11-Dec-08 1:10
Tage Lejon11-Dec-08 1:10 
GeneralRe: The demo cannot start on WinXP [modified] Pin
auralius manurung11-Dec-08 18:05
auralius manurung11-Dec-08 18:05 
GeneralThe demo runs with the downloading, thanks Pin
Tage Lejon11-Dec-08 22:51
Tage Lejon11-Dec-08 22:51 

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

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