本篇博客给出了一个基于对话框读视频的例子。事实上显示视频和显示图像的原理是一样的,它们都是对一副图像的处理。
本例程编译环境为VC6.0和Opencv1.0.
这里首先给出显示图像的函数:
本人所建的工程名字为ReadNewVedio,所以图像显示的函数为:
void CReadNewVedioDlg::ShowImage(int ID, IplImage *pImage)
其中ID为读入视频的路径。
void CReadNewVedioDlg::ShowImage(int ID, IplImage *pImage)
void CReadNewVedioDlg::ShowImage(int ID, IplImage *pImage)
{
int nW = pImage->width;
int nH = pImage->height;
CRect rect;
GetDlgItem(ID)->GetClientRect(&rect);
CDC * pDC_ShowVideo = GetDlgItem(ID)->GetDC();
int width_dst = rect.Width()-2;
int height_dst = rect.Height()-2;
if(pImage->width==width_dst && pImage->height==height_dst)
{
ShowColorBmpOnScreen(pDC_ShowVideo, 1, 1, width_dst, height_dst,
0, 0, pImage->width, pImage->height,
(unsigned char *)(pImage->imageData));
}
else
{
IplImage *pImg_Show = cvCreateImage(cvSize(width_dst, height_dst), 8, 3);
memset(pImg_Show->imageData, 240, pImg_Show->imageSize);
CvRect rect_roi;
float ratio;
float r1 = pImg_Show->width * 1.0f / pImage->width;
float r2 = pImg_Show->height * 1.0f / pImage->height;
if(r1<r2)
{
ratio = r1;
fRetio = float(1 / r1);
rect_roi.x = 0;
rect_roi.width = pImg_Show->width;
rect_roi.height = int(pImage->height * ratio - 0.5f);
rect_roi.y = (pImg_Show->height - rect_roi.height)/2;
}
else
{
ratio = r2;
fRetio = float(1 / r2);
rect_roi.y = 0;
rect_roi.height = pImg_Show->height;
rect_roi.width = int(pImage->width * ratio - 0.5f);
rect_roi.x = (pImg_Show->width - rect_roi.width)/2;
}
rect_Roi.top = rect_roi.y;
rect_Roi.left = rect_roi.x;
rect_Roi.bottom = rect_roi.y + rect_roi.height;
rect_Roi.right = rect_roi.x + rect_roi.width;
rect_roi.height;
nStartX = rect_roi.x; // “显示图像”的起点X坐标
nStartY = rect_roi.y; // “显示图像”的起点Y坐标
cvSetImageROI(pImg_Show, rect_roi);
cvResize(pImage, pImg_Show);
ShowColorBmpOnScreen(pDC_ShowVideo, 1, 1, width_dst, height_dst,
0, 0, pImg_Show->width, pImg_Show->height,
(unsigned char *)(pImg_Show->imageData));
cvResetImageROI(pImg_Show);
cvReleaseImage(&pImg_Show);
}
ReleaseDC(pDC_ShowVideo);
}
void CReadNewVedioDlg::ShowColorBmpOnScreen(CDC *pdc, int screen_left, int screen_top, int screen_width, int screen_height, int image_left, int image_top, int image_width, int image_height, unsigned char *pImageData)
void CReadNewVedioDlg::ShowColorBmpOnScreen(CDC *pdc, int screen_left, int screen_top, int screen_width, int screen_height, int image_left, int image_top, int image_width, int image_height, unsigned char *pImageData)
{
pdc->SetStretchBltMode(COLORONCOLOR);
BITMAPINFO bmpinfo;
bmpinfo.bmiHeader.biBitCount=24;
bmpinfo.bmiHeader.biWidth=image_width;
bmpinfo.bmiHeader.biHeight=-image_height; //注意到因为bmp是从下向上
bmpinfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
bmpinfo.bmiHeader.biSizeImage=image_width*image_height*3;
bmpinfo.bmiHeader.biPlanes=1;
bmpinfo.bmiHeader.biCompression=BI_RGB;
::StretchDIBits(pdc->m_hDC,
screen_left,screen_top,screen_width,screen_height,
image_left,image_top,image_width,image_height,
LPBYTE(pImageData), &bmpinfo,DIB_RGB_COLORS,SRCCOPY);
}
源代码下载地址:
http://download.csdn.net/detail/beijingmake209/7339397
显示结果: