基于图像帧差法 的 托盘检测程序PlateCheck.dsw

上一篇 / 下一篇  2008-04-08 16:16:18 / 个人分类:c++

4月初,老师突然遣师姐发QQ消息告诉我,要做一个基于多幅图像帧差法的 托盘检测程序...

于是了解了Cximage类库,学习了DLL创建,使用方法,最后又看了如何在c#中调用c++的dll模块..

最后完成了如下代码...

//////////////////////////////////////////////////////////////

                   PlateCheck.h

///////////////////////////////////////////////////////////////

// The following ifdef block is the standard way of creating macros which make exporting
// from a DLL simpler. All files within this DLL are compiled with the PlateCheck_API
// symbol defined on the command line. this symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see
// PlateCheck_API functions as being imported from a DLL, wheras this DLL sees symbols
// defined with this macro as being exported.
//

#ifndef PLATECHECK_H
#define PLATECHECK_H

#ifdef PlateCheck_API
#else
#define PlateCheck_API _declspec(dllimport)
#endif

//Declare All Functions as follows
//
//PlateCheck_API extern double Rfull, Rnull;
PlateCheck_API extern long Threhold;

PlateCheck_API bool SetRnull(char* BkGrndPicName, char* PrsntPicName);
PlateCheck_API bool SetRfull(char* BkGrndPicName, char* PrsntPicName);
PlateCheck_API bool SetRnull(double rnull);
PlateCheck_API bool SetRfull(double rfull);
PlateCheck_API double GetRnull(void);
PlateCheck_API double GetRfull(void);

PlateCheck_API int PlateCheck(char* BkGrndPicName, char* PrsntPicName);
PlateCheck_API bool SetThrehold(long threhold);

#endif

//int PlateCheck(char* BkGrndPicName, char* PrsntPicName, long Rfull, long Rnull, long THREHOLD = 10);

 

 

 

 

//////////////////////////////////////////////////////////////

                   PlateCheck.cpp

///////////////////////////////////////////////////////////////

//PlateCheck Project
//

#define PlateCheck_API _declspec(dllexport)

#include "math.h"
#include "ximage.h"
#include "PlateCheck.h"

PlateCheck_API double Rfull, Rnull;
PlateCheck_API long Threhold = 10;
TCHAR* FindExtension(const TCHAR * name);

/////////////////////////////////////////////////////////////////////////
//       PlateCheck    Function
/////////////////////////////////////////////////////////////////////////
int PlateCheck_API PlateCheck(char* BkGrndPicName, char* PrsntPicName)
{

 CxImage  BackGroundPic, PresentPic;

 //ReadImage BackGround and Present
 //

 //检测扩展名为 jpg or bmp
 //
 int Cximage_Sth_Unknown;
 if( !strcmp(FindExtension(BkGrndPicName), "JPG") ||
  !strcmp(FindExtension(BkGrndPicName), "jpg"))
 {
  Cximage_Sth_Unknown = CXIMAGE_FORMAT_JPG;
 }
 
 if( !strcmp(FindExtension(BkGrndPicName), "BMP") ||
  !strcmp(FindExtension(BkGrndPicName), "bmp"))
 {
  Cximage_Sth_Unknown = CXIMAGE_FORMAT_BMP;
 }


 BackGroundPic.Load(BkGrndPicName, Cximage_Sth_Unknown);
 PresentPic.Load(PrsntPicName, Cximage_Sth_Unknown);
 
 if (BackGroundPic.IsValid() && PresentPic.IsValid() )
 {
  
  //转换为灰度图像
  //
  BackGroundPic.GrayScale();
  PresentPic.GrayScale();

  //获取图像高度和宽度,求得面积
  //
  long Height;
  Height = BackGroundPic.GetHeight();
  long Width;
  Width = BackGroundPic.GetWidth();
  long Area;
  Area = Height * Width;
  
  //计算全部像素中,相同点的个数
  //
  long diff;
  long SamePixelNum = 0;
  long x, y;
  for(y = 0; y <= Height - 1; y++)
  {
   for(x = 0; x <= Width - 1; x++)
   {       
    diff = (BackGroundPic.GetPixelColor(x, y)).rgbRed - (PresentPic.GetPixelColor(x, y)).rgbRed;
    if(abs(diff) < Threhold )
    {
     SamePixelNum++;
    }
   }
  }

  //托盘检测,与Rfull, Rnull比较
  //
  double Rpresent;
  Rpresent = (double)(Area - SamePixelNum) / (double)Area;
  //需要输入 Rfull 和 Rnull 的值
  if(Rpresent >= Rfull)
   return 2;
  else
  {
   if(Rpresent > Rnull)
    return 1;
   else
    return 0;
  }
 }
 return -1;
}

 


/////////////////////////////////////////////////////////////////////////
//       SetRnull
/////////////////////////////////////////////////////////////////////////
bool PlateCheck_API SetRnull(char* BkGrndPicName, char* PrsntPicName)
{

 CxImage  BackGroundPic, PresentPic;

 //ReadImage BackGround and Present
 //
 //检测扩展名为 jpg or bmp
 //
 int Cximage_Sth_Unknown;
 if( !strcmp(FindExtension(BkGrndPicName), "JPG") ||
  !strcmp(FindExtension(BkGrndPicName), "jpg"))
 {
  Cximage_Sth_Unknown = CXIMAGE_FORMAT_JPG;
 }
 
 if( !strcmp(FindExtension(BkGrndPicName), "BMP") ||
  !strcmp(FindExtension(BkGrndPicName), "bmp"))
 {
  Cximage_Sth_Unknown = CXIMAGE_FORMAT_BMP;
 }


 BackGroundPic.Load(BkGrndPicName, Cximage_Sth_Unknown);
 PresentPic.Load(PrsntPicName, Cximage_Sth_Unknown);

 if (BackGroundPic.IsValid() && PresentPic.IsValid() )
 {
  
  //转换为灰度图像
  //
  BackGroundPic.GrayScale();
  PresentPic.GrayScale();

  //获取图像高度和宽度,求得面积
  //
  long Height;
  Height = BackGroundPic.GetHeight();
  long Width;
  Width = BackGroundPic.GetWidth();
  long Area;
  Area = Height * Width;

  //计算全部像素中,相同点的个数
  //
  long diff;
  long SamePixelNum = 0;
  long x, y;
  for(y = 0; y <= Height - 1; y++)
  {
   for(x = 0; x <= Width - 1; x++)
   {       
    diff = (BackGroundPic.GetPixelColor(x, y)).rgbRed - (PresentPic.GetPixelColor(x, y)).rgbRed;
    if(abs(diff) < Threhold )SamePixelNum++;
   }
  }

  
  Rnull = (double)(Area - SamePixelNum) / (double)Area;
  return TRUE;
 }
 return FALSE;
}

 

 

/////////////////////////////////////////////////////////////////////////
//       SetRfull
/////////////////////////////////////////////////////////////////////////
bool PlateCheck_API SetRfull(char* BkGrndPicName, char* PrsntPicName)
{

 CxImage  BackGroundPic, PresentPic;

 //ReadImage BackGround and Present
 //
 //检测扩展名为 jpg or bmp
 //
 int Cximage_Sth_Unknown;
 if( !strcmp(FindExtension(BkGrndPicName), "JPG") &&
  !strcmp(FindExtension(BkGrndPicName), "jpg"))
 {
  Cximage_Sth_Unknown = CXIMAGE_FORMAT_JPG;
 }
 
 if( !strcmp(FindExtension(BkGrndPicName), "BMP") &&
  !strcmp(FindExtension(BkGrndPicName), "bmp"))
 {
  Cximage_Sth_Unknown = CXIMAGE_FORMAT_BMP;
 }


 BackGroundPic.Load(BkGrndPicName, Cximage_Sth_Unknown);
 PresentPic.Load(PrsntPicName, Cximage_Sth_Unknown);

 if (BackGroundPic.IsValid() && PresentPic.IsValid() )
 {
  
  //转换为灰度图像
  //
  BackGroundPic.GrayScale();
  PresentPic.GrayScale();

  //获取图像高度和宽度,求得面积
  //
  long Height;
  Height = BackGroundPic.GetHeight();
  long Width;
  Width = BackGroundPic.GetWidth();
  long Area;
  Area = Height * Width;

  //计算全部像素中,相同点的个数
  //
  long diff;
  long SamePixelNum = 0;
  long x, y;
  for(y = 0; y <= Height - 1; y++)
  {
   for(x = 0; x <= Width - 1; x++)
   {       
    diff = (BackGroundPic.GetPixelColor(x, y)).rgbRed - (PresentPic.GetPixelColor(x, y)).rgbRed;
    if(abs(diff) < Threhold )
    {
     SamePixelNum++;
    }
   }
  }
  
  Rfull = (double)(Area - SamePixelNum) / (double)Area;
  return TRUE;
 }
 return FALSE;
}

 

 

/////////////////////////////////////////////////////////////////////////
//       SetRnull   OverLoad
/////////////////////////////////////////////////////////////////////////
bool PlateCheck_API SetRnull(double rnull)
{
 if(rnull >= 0 && rnull <= 1)
 {
  Rnull = rnull;
  return TRUE;
 }
 else
 {
  return FALSE;
 }
 return TRUE;
}

 


/////////////////////////////////////////////////////////////////////////
//       SetRfull   OverLoad
/////////////////////////////////////////////////////////////////////////
bool PlateCheck_API SetRfull(double rfull)
{
 if(rfull >= 0 && rfull <= 1)
 {
  Rfull = rfull;
  return TRUE;
 }
 else
 {
  return FALSE;
 }
 return TRUE;
}

 


/////////////////////////////////////////////////////////////////////////
//       SetThreHold    Function
/////////////////////////////////////////////////////////////////////////
bool PlateCheck_API SetThrehold(long threhold)
{
 if(threhold > 255 || threhold < 0)
  return FALSE;
 else
  Threhold = threhold;
 return TRUE;
}


/////////////////////////////////////////////////////////////////////////
//       GetRnull    Function
/////////////////////////////////////////////////////////////////////////
double PlateCheck_API GetRnull(void)
{
 return Rnull;
}


/////////////////////////////////////////////////////////////////////////
//       GetRfull    Function
/////////////////////////////////////////////////////////////////////////
double PlateCheck_API GetRfull(void)
{
 return Rfull;
}


/////////////////////////////////////////////////////////////////////////
//       FindExtension    Function
/////////////////////////////////////////////////////////////////////////
TCHAR* FindExtension(const TCHAR * name)
{
 int len = _tcslen(name);
 int i;
 for (i = len-1; i >= 0; i--)
 {
  if (name[i] == '.')
  {
   return (TCHAR*)(name+i+1);
  }
 }
 return (TCHAR*)(name+len);
}


 


TAG:

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

Open Toolbar