好的。我已经回答了我自己的 WxWidgets 问题。一个关键是不要和openCV市政厅打RGB序列。 OpenCv 真的很喜欢“BGR”。 WxWidgets 仅使用“RGB”。 opencv 数据结构有一个字节序列字段,但很少被尊重。如果字节序列设置为“RGB”,即使是显示图像的 highGui 函数(在 MS Windows 上)也会显示出惊人的蓝色橘子。我在本地安装中顽固地修复了该错误,但其他操作也失败了。所以,我只是叹了口气,将opencv端的字节顺序设置为“BGR”,并根据需要进行字节交换。
下面的 C++ 代码要求它转换为 wxImages 的 openCV 图像是 RGB、序列“BGR”、8 位深度和 3 个交错通道,并且 width_step = width*3。例程不检查兼容性。使用后果自负。为黄金时段做好准备的版本将提供感兴趣的区域 (ROI) 和其他幻想。
#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;
}