天星湾吧 关注:4贴子:20
  • 12回复贴,共1

VS2013+ Opencv3.0+MFC

只看楼主收藏回复

打开图片


IP属地:上海1楼2017-01-03 19:35回复
    转自百度经验:VS2010 / MFC + OpenCV 2.4.1打开图片


    IP属地:上海2楼2017-01-03 19:37
    回复


      IP属地:上海3楼2017-01-03 19:43
      回复

        选择VC++\MFC应用程序


        IP属地:上海4楼2017-01-03 19:46
        回复



          设置向导,一步步下来


          IP属地:上海5楼2017-01-03 19:48
          回复
            二、配置OpenCV环境
            选择视图、属性管理器,右键工程,打开属性页;
            配置属性,VC++目录:
            包含目录,添加opencv下build,include文件夹,以及opencv和opencv2两个子文件夹;
            库目录,添加opencv下build,x86(我使用的是32位操作系统),v12(即vs2013)下两个子文件夹lib和staticlib,这里主要是为了加载lib文件成功
            链接器:
            选择“输入”,在附加依赖项中添加工程代码中需要的lib文件,这里用到
            opencv_core300d.lib
            opencv_highgui300d.lib
            opencv_imgcodecs300d.lib
            opencv_video300d.lib
            opencv_ml300d.lib
            opencv_imgproc300d.lib(debug模式)
            注意:一定要确保你的电脑里包含这几个lib文件。与此同时,对应的dll文件(与lib文件同名)也要在bin文件夹中存在,否则需要重新设置好环境变量,保证没有确实dll文件


            IP属地:上海6楼2017-01-03 20:04
            收起回复
              三、添加和设置控件
              菜单,视图,工具箱,打开工具箱
              选择Button控件,作为触发打开图片的·按钮;
              以及Picture Control控件,作为图片显示的区域
              右击按钮,添加事件处理程序,选择BN_CLICKED,类列表为CMFC_DEMODlg,函数名为OnBnClickedOpenImg,添加编辑
              在新增按钮的响应函数中添加代码为如下
              void CMFC_DEMODlg::OnBnClickedOpenImg()
              {
              // TODO: 在此添加控件通知处理程序代码
              IplImage *image=NULL; //原始图像
              if(image) cvReleaseImage(&image);
              image = cvLoadImage("D:\\demo.jpg",1); //显示图片
              DrawPicToHDC(image, IDC_STATIC);
              }
              其中,D:\\demo.jpg为预先存储到D:\demo.jpg的已有图片文件,IDC_STATIC为Picture控件的ID号


              IP属地:上海7楼2017-01-03 20:11
              收起回复
                四、添加OpenCV的CvvImage文件
                右击解决资源方案管理器中的头文件,添加,现有项,选择CvvImage.h
                #pragma once
                #ifndef CVVIMAGE_CLASS_DEF
                #define CVVIMAGE_CLASS_DEF
                #include "opencv.hpp"
                /* CvvImage class definition */
                class CvvImage
                {
                public:
                CvvImage();
                virtual ~CvvImage();
                /* Create image (BGR or grayscale) */
                virtual bool Create( int width, int height, int bits_per_pixel, int image_origin = 0 );
                /* Load image from specified file */
                virtual bool Load( const char* filename, int desired_color = 1 );
                /* Load rectangle from the file */
                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
                /* Save entire image to specified file. */
                virtual bool Save( const char* filename );
                /* Get copy of input image ROI */
                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);
                /* width and height of ROI */
                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 );
                /* draw to highgui window */
                virtual void Show( const char* window );
                #if defined WIN32 || defined _WIN32
                /* draw part of image to the specified DC */
                virtual void Show( HDC dc, int x, int y, int width, int height,
                int from_x = 0, int from_y = 0 );
                /* draw the current image ROI to the specified rectangle of the destination DC */
                virtual void DrawToHDC( HDC hDCDst, RECT* pDstRect );
                #endif
                protected:
                IplImage* m_img;
                };
                typedef CvvImage CImage;
                #endif


                IP属地:上海8楼2017-01-03 20:13
                回复
                  同样,源文件中添加CvvImage.cpp
                  #include "StdAfx.h"
                  #include "CvvImage.h"
                  #include "opencv2/imgcodecs.hpp"
                  #include "opencv2/imgcodecs/imgcodecs_c.h"//记得添加imgcodecs300d.lib
                  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();
                  /* prepare IPL header */
                  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;
                  }
                  /* truncate r to source image */
                  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) );
                  }


                  IP属地:上海9楼2017-01-03 20:14
                  回复
                    六、编译运行
                    点击菜单下方工具栏中的绿色向右箭头即可编译生成程序
                    点击OpenImg按钮即可打开D:\demo.jpg并显示在Picture Control控件上


                    IP属地:上海11楼2017-01-03 20:22
                    回复
                      vs2017版本的有没有啊楼主,想要借鉴下,我opencv代码已经弄出来了不知道怎么用mfc建立程序


                      来自Android客户端13楼2020-03-08 19:20
                      回复