Ok, even I'm not satisfied now with the code extracts I posted. So
here's the whole test app.
--------------------------- CUT HERE ---------------------------
#include <FL/Fl.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Sys_Menu_Bar.H>
#include <FL/Fl_Toggle_Button.H>
#include <FL/Fl_Native_File_Chooser.H>
#include <FL/Fl_Menu_Button.H>
#include <FL/Fl_Choice.H>
#include <FL/Fl_Image.H>
#include <FL/Fl_Shared_Image.H>
#include <FL/Fl_RGB_Image.H>
#include <FL/Fl_PNG_Image.H>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <map>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/flann/miniflann.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgproc/types_c.h"
#include <FL/fl_draw.H> // not sure if I need this
using namespace cv;
using namespace std;
void show_image(unsigned char *buff, unsigned long tsize, string fn);
// Comment: the exact same function call, using the exact same prototype
// (except for the function name itself) fails as not valid. WTFO?
// Globals
Mat t;
map<int, string> imgFilename;
map<string, int> imgW;
map<string, int> imgH;
map<string, Fl_PNG_Image*> image;
map<string, Fl_Box*> box;
map<string, Fl_Window*> win;
int idx = 0;
void window_cb(Fl_Widget* w, void*) {
((Fl_Double_Window *)w)->hide();
}
int streq(string s1, string s2) {
const char* ts1 = s1.c_str();
const char* ts2 = s2.c_str();
if (strcmp(ts1, ts2) == 0) return 1;
return 0;
}
void load_image(Mat t, string fn) {
const char* sfn = fn.c_str(); // Fl_window needs const char *
int fileAlreadyLoaded = 0;
idx += 1;
for (int i=0; i<idx; ++i) {
if (streq(fn, imgFilename[i])) {
fileAlreadyLoaded = 1;
idx -= 1;
} else {
imgFilename[idx] = fn;
}
}
imgW[fn] = t.cols;
imgH[fn] = t.rows;
vector<uchar> tbuff;
vector<int> param = vector<int>(2);
param[0]=CV_IMWRITE_PNG_COMPRESSION;
param[1]=0; //default(3) 0--9.
imencode(".png", t, tbuff, param);
unsigned char *buff = tbuff.data();
unsigned long tsize = tbuff.size();
if (fileAlreadyLoaded) {
image[fn] = new Fl_PNG_Image(fn.c_str(), buff, tsize);
box[fn]->image(image[fn]);
win[fn]->show();
} else {
show_image(buff, tsize, fn);
}
}
void show_image(unsigned char *buff, unsigned long tsize, string fn) {
const char* sfn = fn.c_str(); // Fl_window needs const char *
win[fn] = new Fl_Window(imgW[fn],imgH[fn]);
win[fn]->label(sfn);
box[fn] = new Fl_Box(0,0,imgW[fn],imgH[fn]);
image[fn] = new Fl_PNG_Image(fn.c_str(), buff, tsize);
box[fn]->image(image[fn]);
win[fn]->show();
}
int main(int argc, char **argv) {
string fn = "/Users/jim/testimage1.jpg";
Mat t = imread(fn, -1); // -1 loads image as is (with its alpha channel)
load_image(t,fn);
// test updating the same image after modification of the image
// fn = "/Users/jim/testimage2.png";
t = imread(fn, -1); // -1 loads image as is (with its alpha channel)
Mat newMat;
cvtColor(t, newMat, CV_BGR2GRAY);
load_image(newMat,fn); // SHOULD show in the same window
return Fl::run();
}
--------------------------- CUT HERE ---------------------------
The objective here is to update an existing image after it's been
modified via some (any) image processing as a Mat in OpenCV. This test
is meant to apply a one-liner mod and then see if I have a clue how to
update the existing image. Clearly, I don't.
Thanks,
--jim
--
THE SCORE: ME: 2 CANCER: 0
73 DE N5IAL (/4) |
spook...@gmail.com | BOFH Excuse for the day:
< Running Mac OS X Lion > |
ICBM / Hurricane: | Your parity check is overdrawn and
30.44406N 86.59909W | you're out of cache.
http://fineartamerica.com/profiles/4-james-graham.html