opencv + freeimage 播放gif

最近CEF项目,跳转首页需要增加一个loading的过程。使用PictureEx播放gif文件发现会失真,不知道是我操作有误还是什么原因。后来又大概搜了一下,还有几种方法可以播放gif文件的,比如gdi+的方式,百度搜一下应该就可以找到。我这里使用opencv的方式主要还是处于学习的目的,了解下opencv的使用。

目前opencv官网已经更新到341的版本,最新的版本默认都是64位的版本,非常不凑巧,我的电脑是32位的系统,所以需要自己编译32位的版本,套路如CEF。使用CMake工具,操作比较简单,关键的一点就是需要编译静态库还是动态库,根据自己需要来定了。这里附一个参考的链接我临时找了一个链接,我没有参考这个,百度很多

编译成功后,项目设置那些也不说了,这里单列一下整个demo的流程(先写一个demo测试opencv的使用,然后再移植到项目里)。其实如果单纯播放gif而引入opencv其实不是特别明智。好在现在的项目部分也属于插件性质,而且控制opencv引入的体量,我们只要把需要的模块加进去就可以了。

同时还需要引入另一个微型的开源库freeimage, 它可以解析gif的内容,然后再通过opencv显示在窗口上。这是一个使用freeimage 和opencv的demo

整个demo是参考这个链接https://blog.csdn.net/sunrong0511/article/details/52052692

不同的地方是解析gif数据的时候,算法不够高效,有三层嵌套循环,这里需要没有做算法的优化,只是处理上把gif解析的部分放到线程里处理,然后发消息通知UI刷新。突然觉得也没啥可写的。。。。。。。还是写完吧。


void CCOpenCVDlg::playGifEx()
{
	std::thread t([](LPVOID pWnd){

		CCOpenCVDlg* pThis = (CCOpenCVDlg*)pWnd;
		if (pThis == nullptr)
		{
			return;
		}
	
		const char * filename = "D:\\WorkSpace\\COpenCV\\Debug\\loading.gif";

		FreeImage_Initialise();         //load the FreeImage function lib  
		FREE_IMAGE_FORMAT fif = FIF_GIF;
		FIBITMAP* fiBmp = FreeImage_Load(fif, filename, GIF_DEFAULT);
		FIMULTIBITMAP * pGIF = FreeImage_OpenMultiBitmap(fif, filename, 0, 1, 0, GIF_PLAYBACK);
		//  FIBITMAPINFO fiBmpInfo = getfiBmpInfo(fiBmp);  
		int gifImgCnt = FreeImage_GetPageCount(pGIF);
		FIBITMAP * pFrame;
		int width, height;
		width = FreeImage_GetWidth(fiBmp);
		height = FreeImage_GetHeight(fiBmp);
		IplImage * iplImg = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3);
		iplImg->origin = 1;//should set to 1-top-left structure(Windows bitmap style)  
		RGBQUAD* ptrPalette = new RGBQUAD; // = FreeImage_GetPalette(fiBmp);  
		BYTE intens;
		BYTE* pIntensity = &intens;
		//cvNamedWindow("gif", 0);
		printf("gifImgCnt %d \n", gifImgCnt);

		while (!g_bExitLoading)
		{
			for (int curFrame = 0; curFrame < 16; curFrame++)
			{
				pFrame = FreeImage_LockPage(pGIF, curFrame);
				//ptrPalette = FreeImage_GetPalette(pFrame);  
				char * ptrImgDataPerLine;
				for (int i = 0; i < height; i++)
				{
					
					if (g_bExitLoading == true)
					{
						break;
					}

					ptrImgDataPerLine = iplImg->imageData + i*iplImg->widthStep;
					for (int j = 0; j < width; j++)
					{
						if (g_bExitLoading == true)
						{
							break;
						}
						//get the pixel index   
						//FreeImage_GetPixelIndex(pFrame,j,i,pIntensity);    
						FreeImage_GetPixelColor(pFrame, j, i, ptrPalette);
						ptrImgDataPerLine[3 * j] = ptrPalette->rgbBlue;
						ptrImgDataPerLine[3 * j + 1] = ptrPalette->rgbGreen;
						ptrImgDataPerLine[3 * j + 2] = ptrPalette->rgbRed;
						//ptrImgDataPerLine[3*j] = ptrPalette[intens].rgbBlue;  
						//ptrImgDataPerLine[3*j+1] = ptrPalette[intens].rgbGreen;  
						//ptrImgDataPerLine[3*j+2] = ptrPalette[intens].rgbRed;  
					}
				}

				if (g_bExitLoading == true)
				{
					::PostMessage(pThis->m_hWnd, EXIT_DRAW_GIF, NULL, NULL);
					FreeImage_UnlockPage(pGIF, pFrame, 1);
					break;
				}

				printf("convert curFrame end %d \n", curFrame);
				////cvShowImage("gif", iplImg);

				IplImage* pImg = cvCloneImage(iplImg);;
				//memcpy(pImg, iplImg, width * height);

				::PostMessage(pThis->m_hWnd, DRAW_GIF, (WPARAM)(LPVOID)pImg, NULL);
				/*cimg.CopyOf(iplImg, iplImg->nChannels);
				cimg.DrawToHDC(hdc, &rect);
				*/
				cvWaitKey(10);
				FreeImage_UnlockPage(pGIF, pFrame, 1);
			}
		}
		FreeImage_Unload(fiBmp);
		FreeImage_DeInitialise();
	}, this);
	t.detach();
}

然后收到消息后,把gif当前的帧显示在窗口,操作如下

LRESULT CCOpenCVDlg::OnDrawGif( WPARAM w, LPARAM lParam)
{
	IplImage * p = (IplImage*)(w);
	if (p == nullptr)
	{
		return S_FALSE;
	}

	CDC *pDC = GetDlgItem(IDC_STATIC)->GetDC();//根据ID获得窗口指针再获取与该窗口关联的上下文指针
	HDC hdc = pDC->GetSafeHdc();                      // 获取设备上下文句柄
	CRect rect;
	GetDlgItem(IDC_STATIC)->GetClientRect(&rect); //获取box1客户区

	//cvShowImage("gif", iplImg);
	CvvImage cimg;
	cimg.CopyOf(p, p->nChannels);
	cimg.DrawToHDC(hdc, &rect);

	//delete p;
	//p = nullptr;
	cvReleaseImage(&p);
	ReleaseDC(pDC);

	return S_OK;
}

 这个过程,有些方法需要成对使用,不然会有内存泄漏。比如opencv的cvReleaseImage/cvCloneImage。异步消息需要注意参数的作用域,freeimage 解析出来的gif 数据填充到opencv 的结构中时,需要把数据同时传到UI线程,所以需要拷贝一份数据,这里不可以用new或者其他语言支持的开辟内存的方法,这里可能是由于有锁,所以只能通过cvCloneImage克隆的方式,创建出一个填充的副本作为参数传给UI层,UI绘制结束需要释放cvReleaseImage。

基本这样就可以在窗口上播放gif了。如果gif需要做无背景的处理,这里有个链接可以参考https://blog.csdn.net/aixh0505/article/details/42005583?utm_source=itdadao&utm_medium=referral

到这里主要的部分其实已经讲完了。做出来的效果也很好,gif也没有失真。

opencv的新版本源码中缺少两个文件,cvvimage是用来处理opencv对象显示在窗口对象上的。这里送上源码,供参考

#pragma once

#ifndef CVVIMAGE_CLASS_DEF

#define CVVIMAGE_CLASS_DEF

#include <windef.h>
#include "opencv2/opencv.hpp"


class CvvImage
{
public:
	CvvImage();
	virtual ~CvvImage();

	virtual bool  Create(int width, int height, int bits_per_pixel, int image_origin = 0);
	virtual bool  Load(const char* filename, int desired_color = 1);
	virtual bool  LoadRect(const char* filename,int desired_color, CvRect r);

#if defined WIN32 || defined _WIN32

	virtual bool  LoadRect(const char* filename,int desired_color, RECT r)
	{
		return LoadRect(filename, desired_color,
			cvRect(r.left, r.top, r.right - r.left, r.bottom - r.top));
	}
#endif

	virtual bool  Save(const char* filename);
	virtual void  CopyOf(CvvImage& image, int desired_color = -1);
	virtual void  CopyOf(IplImage* img, int desired_color = -1);

	IplImage* GetImage() { return m_img; };
	virtual void  Destroy(void);


	int Width() { return !m_img ? 0 : !m_img->roi ? m_img->width : m_img->roi->width; };
	int Height() { return !m_img ? 0 : !m_img->roi ? m_img->height : m_img->roi->height; };
	int Bpp() { return m_img ? (m_img->depth & 255)*m_img->nChannels : 0; };
	virtual void  Fill(int color);

	virtual void  Show(const char* window);

#if defined WIN32 || defined _WIN32
	virtual void  Show(HDC dc, int x, int y, int width, int height,int from_x = 0, int from_y = 0);
	virtual void  DrawToHDC(HDC hDCDst, RECT* pDstRect);

#endif

protected:
	IplImage*  m_img;
};

typedef CvvImage CImage;
#endif // CVVIMAGE_CLASS_DEF

#include "stdafx.h"
#include "cvvimage.h"


//#include "StdAfx.h"
//#include "CvvImage.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CV_INLINE RECT NormalizeRect(RECT r);
CV_INLINE RECT NormalizeRect(RECT r)
{
	int t;
	if (r.left > r.right)
	{
		t = r.left;
		r.left = r.right;
		r.right = t;
	}
	if (r.top > r.bottom)
	{
		t = r.top;
		r.top = r.bottom;
		r.bottom = t;
	}

	return r;
}
CV_INLINE CvRect RectToCvRect(RECT sr);
CV_INLINE CvRect RectToCvRect(RECT sr)
{
	sr = NormalizeRect(sr);
	return cvRect(sr.left, sr.top, sr.right - sr.left, sr.bottom - sr.top);
}
CV_INLINE RECT CvRectToRect(CvRect sr);
CV_INLINE RECT CvRectToRect(CvRect sr)
{
	RECT dr;
	dr.left = sr.x;
	dr.top = sr.y;
	dr.right = sr.x + sr.width;
	dr.bottom = sr.y + sr.height;

	return dr;
}
CV_INLINE IplROI RectToROI(RECT r);
CV_INLINE IplROI RectToROI(RECT r)
{
	IplROI roi;
	r = NormalizeRect(r);
	roi.xOffset = r.left;
	roi.yOffset = r.top;
	roi.width = r.right - r.left;
	roi.height = r.bottom - r.top;
	roi.coi = 0;

	return roi;
}
void  FillBitmapInfo(BITMAPINFO* bmi, int width, int height, int bpp, int origin)
{
	assert(bmi && width >= 0 && height >= 0 && (bpp == 8 || bpp == 24 || bpp == 32));

	BITMAPINFOHEADER* bmih = &(bmi->bmiHeader);

	memset(bmih, 0, sizeof(*bmih));
	bmih->biSize = sizeof(BITMAPINFOHEADER);
	bmih->biWidth = width;
	bmih->biHeight = origin ? abs(height) : -abs(height);
	bmih->biPlanes = 1;
	bmih->biBitCount = (unsigned short)bpp;
	bmih->biCompression = BI_RGB;
	if (bpp == 8)
	{
		RGBQUAD* palette = bmi->bmiColors;
		int i;
		for (i = 0; i < 256; i++)
		{
			palette[i].rgbBlue = palette[i].rgbGreen = palette[i].rgbRed = (BYTE)i;
			palette[i].rgbReserved = 0;
		}
	}
}
CvvImage::CvvImage()
{
	m_img = 0;
}
void CvvImage::Destroy()
{
	cvReleaseImage(&m_img);
}
CvvImage::~CvvImage()
{
	Destroy();
}
bool  CvvImage::Create(int w, int h, int bpp, int origin)
{
	const unsigned max_img_size = 10000;

	if ((bpp != 8 && bpp != 24 && bpp != 32) ||
		(unsigned)w >= max_img_size || (unsigned)h >= max_img_size ||
		(origin != IPL_ORIGIN_TL && origin != IPL_ORIGIN_BL))
	{
		assert(0); // most probably, it is a programming error
		return false;
	}
	if (!m_img || Bpp() != bpp || m_img->width != w || m_img->height != h)
	{
		if (m_img && m_img->nSize == sizeof(IplImage))
			Destroy();

		m_img = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, bpp / 8);
	}
	if (m_img)
		m_img->origin = origin == 0 ? IPL_ORIGIN_TL : IPL_ORIGIN_BL;
	return m_img != 0;
}
void  CvvImage::CopyOf(CvvImage& image, int desired_color)
{
	IplImage* img = image.GetImage();
	if (img)
	{
		CopyOf(img, desired_color);
	}
}
#define HG_IS_IMAGE(img)                                                  \
  ((img) != 0 && ((const IplImage*)(img))->nSize == sizeof(IplImage) && \
  ((IplImage*)img)->imageData != 0)
void  CvvImage::CopyOf(IplImage* img, int desired_color)
{
	if (HG_IS_IMAGE(img))
	{
		int color = desired_color;
		CvSize size = cvGetSize(img);
		if (color < 0)
			color = img->nChannels > 1;
		if (Create(size.width, size.height,
			(!color ? 1 : img->nChannels > 1 ? img->nChannels : 3) * 8,
			img->origin))
		{
			cvConvertImage(img, m_img, 0);
		}
	}
}
bool  CvvImage::Load(const char* filename, int desired_color)
{
	IplImage* img = cvLoadImage(filename, desired_color);
	if (!img)
		return false;

	CopyOf(img, desired_color);
	cvReleaseImage(&img);

	return true;
}
bool  CvvImage::LoadRect(const char* filename,
	int desired_color, CvRect r)
{
	if (r.width < 0 || r.height < 0) return false;

	IplImage* img = cvLoadImage(filename, desired_color);
	if (!img)
		return false;
	if (r.width == 0 || r.height == 0)
	{
		r.width = img->width;
		r.height = img->height;
		r.x = r.y = 0;
	}
	if (r.x > img->width || r.y > img->height ||
		r.x + r.width < 0 || r.y + r.height < 0)
	{
		cvReleaseImage(&img);
		return false;
	}

	if (r.x < 0)
	{
		r.width += r.x;
		r.x = 0;
	}
	if (r.y < 0)
	{
		r.height += r.y;
		r.y = 0;
	}
	if (r.x + r.width > img->width)
		r.width = img->width - r.x;

	if (r.y + r.height > img->height)
		r.height = img->height - r.y;
	cvSetImageROI(img, r);
	CopyOf(img, desired_color);
	cvReleaseImage(&img);
	return true;
}
bool  CvvImage::Save(const char* filename)
{
	if (!m_img)
		return false;
	cvSaveImage(filename, m_img);
	return true;
}
void  CvvImage::Show(const char* window)
{
	if (m_img)
		cvShowImage(window, m_img);
}
void  CvvImage::Show(HDC dc, int x, int y, int w, int h, int from_x, int from_y)
{
	if (m_img && m_img->depth == IPL_DEPTH_8U)
	{
		uchar buffer[sizeof(BITMAPINFOHEADER) + 1024];
		BITMAPINFO* bmi = (BITMAPINFO*)buffer;
		int bmp_w = m_img->width, bmp_h = m_img->height;
		FillBitmapInfo(bmi, bmp_w, bmp_h, Bpp(), m_img->origin);
		from_x = MIN(MAX(from_x, 0), bmp_w - 1);
		from_y = MIN(MAX(from_y, 0), bmp_h - 1);
		int sw = MAX(MIN(bmp_w - from_x, w), 0);
		int sh = MAX(MIN(bmp_h - from_y, h), 0);
		SetDIBitsToDevice(
			dc, x, y, sw, sh, from_x, from_y, from_y, sh,
			m_img->imageData + from_y*m_img->widthStep,
			bmi, DIB_RGB_COLORS);
	}
}
void  CvvImage::DrawToHDC(HDC hDCDst, RECT* pDstRect)
{
	if (pDstRect && m_img && m_img->depth == IPL_DEPTH_8U && m_img->imageData)
	{
		uchar buffer[sizeof(BITMAPINFOHEADER) + 1024];
		BITMAPINFO* bmi = (BITMAPINFO*)buffer;
		int bmp_w = m_img->width, bmp_h = m_img->height;
		CvRect roi = cvGetImageROI(m_img);
		CvRect dst = RectToCvRect(*pDstRect);
		if (roi.width == dst.width && roi.height == dst.height)
		{
			Show(hDCDst, dst.x, dst.y, dst.width, dst.height, roi.x, roi.y);
			return;
		}
		if (roi.width > dst.width)
		{
			SetStretchBltMode(
				hDCDst,          // handle to device context
				HALFTONE);
		}
		else
		{
			SetStretchBltMode(
				hDCDst,          // handle to device context
				COLORONCOLOR);
		}
		FillBitmapInfo(bmi, bmp_w, bmp_h, Bpp(), m_img->origin);
		::StretchDIBits(
			hDCDst,
			dst.x, dst.y, dst.width, dst.height,
			roi.x, roi.y, roi.width, roi.height,
			m_img->imageData, bmi, DIB_RGB_COLORS, SRCCOPY);
	}
}
void  CvvImage::Fill(int color)
{
	cvSet(m_img, cvScalar(color & 255, (color >> 8) & 255, (color >> 16) & 255, (color >> 24) & 255));
}

猜你喜欢

转载自blog.csdn.net/moyebaobei1/article/details/81743824
今日推荐