彻底解决显示Opencv中Mat图像到Mfc窗口问题
第一步,轉(zhuǎn)換格式前預(yù)先獲得待顯示控件的大小,若相等則不做處理,若不等則首先改變Mat圖像大小,再進(jìn)行轉(zhuǎn)換。
?? ??? ?CRect rect;
?? ??? ?GetDlgItem(IDC_STATIC_SRC)->GetClientRect(&rect);
?? ??? ?cv::Size winSize(rect.right, rect.bottom);
?
?? ??? ?// Resize the source to the size of the destination image if necessary
?? ??? ?cv::Mat cvImgTmp(winSize, CV_8UC3);
?? ??? ?if (imgSrc.size() != winSize)
?? ??? ?{
?? ??? ??? ?cv::resize(imgSrc, cvImgTmp, winSize);
?? ??? ?}
?? ??? ?else
?? ??? ?{
?? ??? ??? ?cvImgTmp = imgSrc;
?? ??? ?}?? ?
第二步,將Mat圖像轉(zhuǎn)換成CImage格式
int Mat2CImage(Mat *mat, CImage &img) {
?? ?if (!mat || mat->empty())
?? ??? ?return -1;
?? ?int nBPP = mat->channels() * 8;
?? ?img.Create(mat->cols, mat->rows, nBPP);
?? ?if (nBPP == 8)
?? ?{
?? ??? ?static RGBQUAD pRGB[256];
?? ??? ?for (int i = 0; i < 256; i++)
?? ??? ??? ?pRGB[i].rgbBlue = pRGB[i].rgbGreen = pRGB[i].rgbRed = i;
?? ??? ?img.SetColorTable(0, 256, pRGB);
?? ?}
?? ?uchar* psrc = mat->data;
?? ?uchar* pdst = (uchar*)img.GetBits();
?? ?int imgPitch = img.GetPitch();
?? ?for (int y = 0; y < mat->rows; y++)
?? ?{
?? ??? ?memcpy(pdst, psrc, mat->cols*mat->channels());//mat->step is incorrect for those images created by roi (sub-images!)
?? ??? ?psrc += mat->step;
?? ??? ?pdst += imgPitch;
?? ?}
?
?? ?return 0;
}
第三步,可以直接顯示CImage格式圖像
?? ?CImage imgDst;
?? ??? ?Mat2CImage(&cvImgTmp, imgDst);
?? ??? ?imgDst.Draw(GetDlgItem(IDC_STATIC_SRC)->GetDC()->GetSafeHdc(), rect);
原文:https://blog.csdn.net/u014682691/article/details/51296219?
?
總結(jié)
以上是生活随笔為你收集整理的彻底解决显示Opencv中Mat图像到Mfc窗口问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: OpenCV学习笔记之 ( 三 ) MF
- 下一篇: RTP格式图 NNEXB格式和RTP格