This is a guide of making OpenCV and Gimp worked togther. It's not complex as you might thought. Taking 5 minutes, you will understand how to make your first OpenCV-oriented Gimp plugin.
Gimp is powerful enough, why OpenCV comes into? (Don't care about this paragraph, it's a crap :))
Yes, Gimp is excellent, but it will take a while for me to explain why I involved OpenCV in. I knew OpenCV before I knew Gimp. I wrote my program in OpenCV several months ago. When all core functions have been finished, I felt that UI is also critical for the program. However it is quite time-consuming to implement a full featured UI which can handle image load, save, display, mouse capture etc. Finally I searched google and come to realize that create a GIMP plugin is a good idea. That's the exact reason that I used OpenCV in Gimp plugin.
By querying the mailling list of GIMP I figured out that Gimp's plugin is nothing but "normal" programs which is conformed with GIMP's plugin interface. It means you can use any library you like in plugin coding, including OpenCV. As I already got my OpenCV based program born, what I need to do is just write some middle code to adapter my code to Gimp.
You can find the classic GIMP plugin tutorial in here: How to write a GIMP plug-in
I am not going to repeat the content of that tutorial, instead, let's have a look of how the adapter function works.
void process_it (GimpDrawable *drawable, GimpPreview *preview)
gint i, j, k, channels;
gint x1, y1, x2, y2;
gint width, height;
GimpPixelRgn rgn_in, rgn_out;
if (! preview)
gimp_progress_init ("Dust Cleaner");
/* Gets upper left and lower right coordinates,
* and layers number in the image */
gimp_preview_get_position (preview, &x1, &y1);
gimp_preview_get_size (preview, &width, &height);
x2 = x1 + width;
y2 = y1 + height;
width = x2 - x1;
height = y2 - y1;
channels = gimp_drawable_bpp (drawable->drawable_id);
/* Region for reading */
x2 - x1, y2 - y1,
/* Region for writting */
x2 - x1, y2 - y1,
preview == NULL, TRUE);
/* Initialise enough memory for rect*/
rect = g_new (guchar, channels * width * height); //<----this is the raw data represents the image.
/* Put raw data from rgn_in to rect */
/* Create OpenCV matrix object from raw data */
mat = cvMat( height, width, CV_8UC3, rect ); //<----
this is the exact code of creating object of OpenCV
findDust(&mat); //<----this is the core function to handle the image processing
/* Set modified raw data back to writable region */
/* Free memory */
g_free (rect); //<----don't forget release the raw data
/* Update the modified region */
gimp_drawable_preview_draw_region (GIMP_DRAWABLE_PREVIEW (preview),
gimp_drawable_merge_shadow (drawable->drawable_id, TRUE);
As you can see from the above code, there are three steps to get things done:
- Create a raw data represents the image object in Gimp.
- Create a CvMat object by the raw data.
- Give the CvMat object a whirl with your local function. Remember, when you modify any pixel of that CvMat object you are actually modifying the raw data wrapped by the CvMat header.
That's all. It's fortunate that both Gimp and OpenCV used the same sequence of RGB color, so it's simplified the conversion between them, not like Matlab.
Thank David Hodson and Sven