Okay. Ich habe die Antwort auf meine eigene Frage für WxWidgets. Ein Schlüssel ist, sich nicht mit OpenCV City Hall über die RGB-Sequenz zu streiten. OpenCv mag "BGR" wirklich. WxWidgets verwendet nur "RGB". Die opencv-Datenstruktur hat ein Feld für die Byte-Reihenfolge, aber es wird nur selten beachtet. Sogar die highGui-Funktion (unter MS Windows), die ein Bild anzeigt, zeigt spektakulär blaue Mandarinen an, wenn die Byte-Sequenz auf "RGB" eingestellt ist. Ich habe diesen Fehler in meiner lokalen Installation hartnäckig behoben, aber auch andere Operationen schlugen fehl. Also seufze ich nur und setze die Byte-Reihenfolge auf der opencv-Seite auf "BGR" und tausche die Bytes nach Bedarf aus.
Der folgende C++-Code setzt voraus, dass die openCV-Bilder, die er in wxImages konvertiert, RGB, Sequenz "BGR", 8 Bit Tiefe und 3 verschachtelte Kanäle sind und width_step = width*3 haben. Die Routinen prüfen die Kompatibilität nicht. Verwendung auf eigene Gefahr. Eine einsatzbereite Version würde Interessensbereiche (ROI) und andere Spielereien ermöglichen.
#include "wx/wx.h"
#include "cv.h"
#include "highgui.h" // Optional
void copy_and_swap_rb(char *s, char *d, int size) {
// Copy image data source s to destination d, swapping R and B channels.
// Assumes 8 bit depth, 3 interleaved channels, and width_step = width*3
const int step = 3;
char *end = s + size;
while (s<end) {
d[0] = s[2];
d[1] = s[1];
d[2] = s[0];
d += step; s += step;
}
}
void wx2cv(wxImage &wx, IplImage *ipl) {
// Copy image data from wxWidgets image to Ipl (opencv) image
// Assumes ipl image has seq "GBR", depth 8, and 3 channels, and
// has the same size as the wxImage, and width_step = width*3.
copy_and_swap_rb((char*)wx.GetData(), ipl->imageData, ipl->imageSize);
}
void cv2wx(IplImage *ipl, wxImage &wx ) {
// Copy image data from Ipl (opencv) image to wxImage
// Assumes ipl image has seq "GBR", depth 8, and 3 channels, and
// has the same size as the wxImage, and width_step = width*3.
copy_and_swap_rb( ipl->imageData, (char*)wx.GetData(),
wx.GetWidth()*wx.GetHeight()*3);
}
IplImage *cv_from_wx(wxImage &wx) {
// Return a new IplImage copied from a wxImage.
// Must be freed by user with cvReleaseImage().
IplImage *ret = cvCreateImage(cvSize(wx.GetWidth(), wx.GetHeight()),
IPL_DEPTH_8U, 3);
wx2cv(wx, ret);
return ret;
}
wxImage wx_from_cv( IplImage *cx) {
// Return new wxImage copied from a compatible IplImage.
// Assumes ipl image has seq "GBR", depth 8, and 3 channels
// Fear not. The copy on return is cheap; does not deep-copy the data.
wxImage wx(cx->width, cx->height, (unsigned char*) malloc(cx->imageSize), false);
cv2wx(cx, wx);
return wx;
}