好的。我已經爲我自己的WxWidgets問題得到了答案。一個關鍵不是要打開關於RGB序列的openCV市政廳。 OpenCv真的很喜歡「BGR」。 WxWidgets只使用「RGB」。opencv數據結構有一個用於字節序列的字段,但它很少被使用。如果字節序列設置爲「RGB」,即使是顯示圖像的highGui函數(在MS Windows上)也會顯示出壯觀的藍色橘子。我在本地安裝中固執地修復了這個bug,但其他操作也失敗了。所以,我只是感嘆並將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;
}
這似乎適用於RGB圖像。如果我有一個灰度IplImage怎麼辦? – Sunny 2010-06-08 08:30:21