Picture control显示图像及Mat转换为CImage
2014-07-23 22:29
267 查看
(以VS2010为例)
1. 点击资源, 选择Dialog并点开, 在任意对话资源上右击, 选择"插入"Dialog", 如图1所示.
2. 插入后, 默认ID为IDD_DIALOG1, 可修改为自己相应的ID, 修改方式为:选择"属性"(可右击资源, 选择属性, 也可以选中资源后, 点右上的"属性")
3. 选择工具箱->Picture Control 控件, 并将Picture Control控件拖到对话框上.
拖上去后, 可做相关属性的修改. 如可以其ID修改为IDC_MY_PIC(下面的程序以将其ID修改为IDC_MY_PIC为例).
4. 在适当位置添加如下代码
CImage myImage;
myImage.Load(_T("d:\\lena.bmp"));
CRect rect;
CWnd *pWnd = GetDlgItem(IDC_MY_PIC); (这是在此资源创建的类的内部, 若是在外部, 可先通过获得CMainFrame的指针, 再通过pMianFrame->GetDlgItem(IDCk_MY_PIC)来获取)
CDC *pDC = pWnd->GetDC();
pWnd->GetClientRect(&rect);
pDC->SetStretchBltMode(STRETCH_HALFTONE);
myImage.Draw(pDC->m_hDC, rect);
ReleaseDC(pDC);
myImage.Destroy();
Mat转换为CImage:
[cpp] view
plaincopy
void MatToCImage( Mat &mat, CImage &cImage)
{
//create new CImage
int width = mat.cols;
int height = mat.rows;
int channels = mat.channels();
cImage.Destroy(); //clear
cImage.Create(width,
height, //positive: left-bottom-up or negative: left-top-down
8*channels ); //numbers of bits per pixel
//copy values
uchar* ps;
uchar* pimg = (uchar*)cImage.GetBits(); //A pointer to the bitmap buffer
//The pitch is the distance, in bytes. represent the beginning of
// one bitmap line and the beginning of the next bitmap line
int step = cImage.GetPitch();
for (int i = 0; i < height; ++i)
{
ps = (mat.ptr<uchar>(i));
for ( int j = 0; j < width; ++j )
{
if ( channels == 1 ) //gray
{
*(pimg + i*step + j) = ps[j];
}
else if ( channels == 3 ) //color
{
for (int k = 0 ; k < 3; ++k )
{
*(pimg + i*step + j*3 + k ) = ps[j*3 + k];
}
}
}
}
}
转自:http://vipjy2008.blog.163.com/blog/static/372087672014026314586/ http://blog.csdn.net/merlin_q/article/details/7041040/
1. 点击资源, 选择Dialog并点开, 在任意对话资源上右击, 选择"插入"Dialog", 如图1所示.
2. 插入后, 默认ID为IDD_DIALOG1, 可修改为自己相应的ID, 修改方式为:选择"属性"(可右击资源, 选择属性, 也可以选中资源后, 点右上的"属性")
3. 选择工具箱->Picture Control 控件, 并将Picture Control控件拖到对话框上.
拖上去后, 可做相关属性的修改. 如可以其ID修改为IDC_MY_PIC(下面的程序以将其ID修改为IDC_MY_PIC为例).
4. 在适当位置添加如下代码
CImage myImage;
myImage.Load(_T("d:\\lena.bmp"));
CRect rect;
CWnd *pWnd = GetDlgItem(IDC_MY_PIC); (这是在此资源创建的类的内部, 若是在外部, 可先通过获得CMainFrame的指针, 再通过pMianFrame->GetDlgItem(IDCk_MY_PIC)来获取)
CDC *pDC = pWnd->GetDC();
pWnd->GetClientRect(&rect);
pDC->SetStretchBltMode(STRETCH_HALFTONE);
myImage.Draw(pDC->m_hDC, rect);
ReleaseDC(pDC);
myImage.Destroy();
Mat转换为CImage:
[cpp] view
plaincopy
void MatToCImage( Mat &mat, CImage &cImage)
{
//create new CImage
int width = mat.cols;
int height = mat.rows;
int channels = mat.channels();
cImage.Destroy(); //clear
cImage.Create(width,
height, //positive: left-bottom-up or negative: left-top-down
8*channels ); //numbers of bits per pixel
//copy values
uchar* ps;
uchar* pimg = (uchar*)cImage.GetBits(); //A pointer to the bitmap buffer
//The pitch is the distance, in bytes. represent the beginning of
// one bitmap line and the beginning of the next bitmap line
int step = cImage.GetPitch();
for (int i = 0; i < height; ++i)
{
ps = (mat.ptr<uchar>(i));
for ( int j = 0; j < width; ++j )
{
if ( channels == 1 ) //gray
{
*(pimg + i*step + j) = ps[j];
}
else if ( channels == 3 ) //color
{
for (int k = 0 ; k < 3; ++k )
{
*(pimg + i*step + j*3 + k ) = ps[j*3 + k];
}
}
}
}
}
转自:http://vipjy2008.blog.163.com/blog/static/372087672014026314586/ http://blog.csdn.net/merlin_q/article/details/7041040/
相关文章推荐
- MFC 显示Mat,不使用Mat到CImage转换
- MFC中打开任意图像并在picture control控件中显示Mat对象
- CImage显示OpenCV:Mat矩阵图像
- Numpy、matplotlib实现二维数据到图像的转换,添加colormap,无边距显示
- OpenCV图像MAT格式转换为QT中QImage并显示
- 将OpenCV:Mat矩阵图像转换为MFC:CImage图像
- MFC:CImage显示OpenCV:Mat矩阵图像
- MFC:CImage显示OpenCV:Mat矩阵图像
- Opencv图像识别从零到精通(5)-----Mat_ROI、颜色转换、多图显示、保存输出
- MFC:CImage显示OpenCV:Mat矩阵图像
- MFC:CImage显示OpenCV:Mat矩阵图像
- MFC:CImage显示OpenCV:Mat矩阵图像
- Qt中用QLabel显示OpenCV中Mat图像数据出现扭曲现象的解决
- 利用CImage完成图片的显示、拷贝、绘制及转换图片格式
- OpenCV学习笔记(24)CLR模式下System::Drawing::Bitmap与cv::Mat图像格式的转换
- CImage实现jpeg到bmp内存图像转换
- Qt中使用OpenCV显示图片时,Mat结构转换为QImage结构的问题
- DICM和BMP图像的显示及转换
- 使用CImage类处理图像显示与格式转换(转)
- 利用CImage完成图片的显示、拷贝、绘制及转换图片格式 .