Commit ef64ee34 张士柳

1 个父辈 71f29f62
...@@ -719,7 +719,7 @@ namespace eyemLib_Sharp ...@@ -719,7 +719,7 @@ namespace eyemLib_Sharp
private static extern int eyemCountObjectE(EyemImage tpImage, string fileName, ref string pNumObj, out EyemImage tpDstImg); private static extern int eyemCountObjectE(EyemImage tpImage, string fileName, ref string pNumObj, out EyemImage tpDstImg);
//异型器件(新版本) //异型器件(新版本)
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)] [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
private static extern int eyemCountObjectIrregularPartsE(EyemImage tpImage, string fileName, string ccTplName, double dMinScore, ref string pNumObj, out EyemImage tpDstImg); private static extern int eyemCountObjectIrregularPartsE(EyemImage tpImage, string fileName, string ccTplName, ref string pNumObj, out EyemImage tpDstImg);
//创建模板匹配模型 //创建模板匹配模型
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)] [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
private static extern int eyemCreateTemplateModel(EyemImage tpImage, EyemRect tpRoi, double dMinScore, string ccTplName); private static extern int eyemCreateTemplateModel(EyemImage tpImage, EyemRect tpRoi, double dMinScore, string ccTplName);
...@@ -910,40 +910,53 @@ namespace eyemLib_Sharp ...@@ -910,40 +910,53 @@ namespace eyemLib_Sharp
//eyemEdgesPixel(ucpImage, 15); //eyemEdgesPixel(ucpImage, 15);
#endregion #endregion
EyemRect tpRoi = new EyemRect(); EyemRect tpRoi = new EyemRect();
tpRoi.iXs = 1026; tpRoi.iYs = 513; tpRoi.iXs = 200; tpRoi.iYs = 200;
tpRoi.iWidth = 82; tpRoi.iWidth = image.iWidth - 400;
tpRoi.iHeight = 60; tpRoi.iHeight = image.iHeight - 400;
//EyemRect tpRoi = new EyemRect(); //
//tpRoi.iXs = 200; tpRoi.iYs = 200; string pNumObj = "";
//tpRoi.iWidth = image.iWidth - 400; string file = fileName.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries)[2];
//tpRoi.iHeight = image.iHeight - 400;
//获取用于制作模板的图像
flag = eyemAchvTemplateImage(image, tpRoi, out tpDstImg);
//Bitmap bmp = eyemCvtToBitmap(tpDstImg);
//if (bmp != null)
//{
// bmp.Save("D:\\模板文件原图\\" + file);
//}
//创建模板匹配模型 //创建模板匹配模型
double dMinScore = 0.75; EyemRect tpRoi2 = new EyemRect();
flag = eyemCreateTemplateModel(image, tpRoi, dMinScore, "D://模板文件//template.tpl"); tpRoi2.iXs = 1112; tpRoi2.iYs = 414;
tpRoi2.iWidth = 68;
tpRoi2.iHeight = 79;
//double matchDeg = 0.75;
//flag = eyemCreateTemplateModel(tpDstImg, tpRoi2, matchDeg, "D://模板文件//" + file.Replace(".png", ".tpl"));
string selectModel = ""; string selectModel = "";
flag = eyemMatchTemplateModel(image, "D:\\模板文件", ref selectModel); flag = eyemMatchTemplateModel(tpDstImg, "D:\\模板文件", ref selectModel);
//flag = eyemAchvTemplateImage(image, tpRoi, out tpDstImg); //最好释放掉
eyemImageFree(ref tpDstImg);
//
string pNumObj = "";
string file = fileName.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries)[2];
//"IP_SMALL_PARTS","IP_LARGE_PARTS","IP_LONG_PARTS","" //"IP_SMALL_PARTS","IP_LARGE_PARTS","IP_LONG_PARTS",""
//eyemCountObject(image, file.Replace(".png", ""), 35, 0, 100, 5, ref pNumObj, out tpDstImg); //eyemCountObject(image, file.Replace(".png", ""), 35, 0, 100, 5, ref pNumObj, out tpDstImg);
//eyemCountObjectIrregularParts(image, file.Replace(".png", ""), 0.1, "IP_LARGE_PARTS", 100, 7, ref pNumObj, out tpDstImg); //eyemCountObjectIrregularParts(image, file.Replace(".png", ""), 0.1, "IP_LARGE_PARTS", 100, 7, ref pNumObj, out tpDstImg);
//eyemCountObjectE(image, fileName, ref pNumObj, out tpDstImg); //eyemCountObjectE(image, fileName, ref pNumObj, out tpDstImg);
//eyemCountObjectIrregularPartsE(image, file.Replace(".png", ""), "D://批量测试图像模板文件//" + file.Replace(".png", "") + "_tpl.png", 0.75, ref pNumObj, out tpDstImg); eyemCountObjectIrregularPartsE(image, file.Replace(".png", ""), selectModel, ref pNumObj, out tpDstImg);
//Bitmap bmp = eyemCvtToBitmap(tpDstImg); Bitmap bitmap = eyemCvtToBitmap(tpDstImg);
//if (bmp != null) if (bitmap != null)
//{ {
// bmp.Save(System.Windows.Forms.Application.StartupPath + "\\ResOut\\" + file); bitmap.Save(System.Windows.Forms.Application.StartupPath + "\\ResOut\\" + file);
//} }
////<解码测试 ////<解码测试
//int ipNum; EyemBarCode* tpResults; //int ipNum; EyemBarCode* tpResults;
...@@ -1128,17 +1141,16 @@ namespace eyemLib_Sharp ...@@ -1128,17 +1141,16 @@ namespace eyemLib_Sharp
BitmapData bd = bitmap.LockBits(new Rectangle(0, 0, tpImage.iWidth, tpImage.iHeight), BitmapData bd = bitmap.LockBits(new Rectangle(0, 0, tpImage.iWidth, tpImage.iHeight),
ImageLockMode.WriteOnly, format); ImageLockMode.WriteOnly, format);
int srcStep = tpImage.iWidth * tpImage.iChannels;
int dstStep = ((tpImage.iWidth * tpImage.iChannels) + 3) / 4 * 4;
long bytesToCopy = tpImage.iWidth * tpImage.iChannels;
try try
{ {
int pd = ((tpImage.iWidth * tpImage.iChannels) + 3) / 4 * 4;
long bytesToCopy = tpImage.iWidth * tpImage.iChannels;
for (int y = 0; y < tpImage.iHeight; y++) for (int y = 0; y < tpImage.iHeight; y++)
{ {
long offsetSrc = (y * srcStep); long offsetSrc = (y * tpImage.iWidth * tpImage.iChannels);
long offsetDst = (y * dstStep); long offsetDst = (y * pd);
Buffer.MemoryCopy((byte*)(tpImage.vpImage.ToPointer()) + offsetSrc, (byte*)(bd.Scan0.ToPointer()) + offsetDst, bytesToCopy, bytesToCopy); Buffer.MemoryCopy((byte*)(tpImage.vpImage.ToPointer()) + offsetSrc, (byte*)(bd.Scan0.ToPointer()) + offsetDst, bytesToCopy, bytesToCopy);
} }
......
...@@ -594,10 +594,25 @@ namespace eyemLib_Sharp ...@@ -594,10 +594,25 @@ namespace eyemLib_Sharp
#region 项目 #region 项目
//普通器件 //普通器件
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)] [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
private static extern int eyemCountObject(EyemImage tpImage, string fileName, ref string pNumObj, out EyemImage tpDstImg); private static extern int eyemCountObject(EyemImage tpImage, string fileName, double dOffset, int iMinArea, int iMaxArea, int iWinSize, ref string pNumObj, out EyemImage tpDstImg);
//异型器件 //异型器件
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)] [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
private static extern int eyemCountObjectIrregularParts(EyemImage tpImage, string fileName, string strType, string tplName, double dMinScore, ref string pNumObj, out EyemImage tpDstImg); private static extern int eyemCountObjectIrregularParts(EyemImage tpImage, string fileName, double dOffset, string strType, int iMaxArea, int iWinSize, ref string pNumObj, out EyemImage tpDstImg);
//普通器件(新版本)
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
private static extern int eyemCountObjectE(EyemImage tpImage, string fileName, ref string pNumObj, out EyemImage tpDstImg);
//异型器件(新版本)
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
private static extern int eyemCountObjectIrregularPartsE(EyemImage tpImage, string fileName, string ccTplName, ref string pNumObj, out EyemImage tpDstImg);
//创建模板匹配模型
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
private static extern int eyemCreateTemplateModel(EyemImage tpImage, EyemRect tpRoi, double dMinScore, string ccTplName);
//获取模板图像
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
private static extern int eyemAchvTemplateImage(EyemImage tpImage, EyemRect tpRoi, out EyemImage tpDstImg);
//选取模板
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
private static extern int eyemMatchTemplateModel(EyemImage tpImage, string ccTplNames, ref string lpszTplName);
//读码程序 //读码程序
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)] [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
private static extern int eyemDetectAndDecode(EyemImage tpImage, EyemRect tpRoi, string fileName, string strCodeType, out DataCodeHandle hObject, out EyemBarCode* tpResults, out int ipNum, bool bUseNiBlack, int iBlockSize, int iRangeC, int iSymbolMin, int iSymbolMax, double dScaleUpAndDown = 0.5, double dToleErr = 0.5, double dMinorStep = 1.0); private static extern int eyemDetectAndDecode(EyemImage tpImage, EyemRect tpRoi, string fileName, string strCodeType, out DataCodeHandle hObject, out EyemBarCode* tpResults, out int ipNum, bool bUseNiBlack, int iBlockSize, int iRangeC, int iSymbolMin, int iSymbolMax, double dScaleUpAndDown = 0.5, double dToleErr = 0.5, double dMinorStep = 1.0);
...@@ -665,7 +680,77 @@ namespace eyemLib_Sharp ...@@ -665,7 +680,77 @@ namespace eyemLib_Sharp
#endregion #endregion
//
//点料算法例程
public static void eyeyTestTemplateModelMethod(string fileName)
{
EyemImage image;
EyemImage tpDstImg = new EyemImage();
int flag = eyemImageRead(fileName, -1, out image);
if (flag != 0)
{
Console.WriteLine("读图失败!");
return;
}
//用于将黑边等不需要及影响预处理的部分去除
EyemRect tpRoi = new EyemRect();
tpRoi.iXs = 200; tpRoi.iYs = 200;
tpRoi.iWidth = image.iWidth - 400;
tpRoi.iHeight = image.iHeight - 400;
//
string file = fileName.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries)[2];
//获取用于制作模板的图像
flag = eyemAchvTemplateImage(image, tpRoi, out tpDstImg);
Bitmap bmp = eyemCvtToBitmap(tpDstImg);
if (bmp != null)
{
bmp.Save("D:\\模板文件原图\\" + file);
}
//创建模板匹配模型
EyemRect tpRoi2 = new EyemRect();
tpRoi2.iXs = 1112; tpRoi2.iYs = 414;
tpRoi2.iWidth = 68;
tpRoi2.iHeight = 79;
//匹配分数
double matchDeg = 0.75;
flag = eyemCreateTemplateModel(tpDstImg, tpRoi2, matchDeg, "D://模板文件//" + file.Replace(".png", ".tpl"));
//可以选择使用自动匹配或者指定模板文件
string selectModel = "";
flag = eyemMatchTemplateModel(tpDstImg, "D:\\模板文件", ref selectModel);
//最好释放掉,点料会用到,或者重新创建一个供不同函数使用
eyemImageFree(ref tpDstImg);
string pNumObj = "";
//"IP_SMALL_PARTS","IP_LARGE_PARTS","IP_LONG_PARTS",""
//eyemCountObject(image, file.Replace(".png", ""), 35, 0, 100, 5, ref pNumObj, out tpDstImg);
//eyemCountObjectIrregularParts(image, file.Replace(".png", ""), 0.1, "IP_LARGE_PARTS", 100, 7, ref pNumObj, out tpDstImg);
//eyemCountObjectE(image, fileName, ref pNumObj, out tpDstImg);
eyemCountObjectIrregularPartsE(image, file.Replace(".png", ""), selectModel, ref pNumObj, out tpDstImg);
Bitmap bitmap = eyemCvtToBitmap(tpDstImg);
if (bitmap != null)
{
bitmap.Save(System.Windows.Forms.Application.StartupPath + "\\ResOut\\" + file);
}
//free image
eyemImageFree(ref tpDstImg);
eyemImageFree(ref image);
}
//背景变化追踪例程
public static void eyemTestVideoCapture(string fileName) public static void eyemTestVideoCapture(string fileName)
{ {
List<EyemRect3> tpRois = new List<EyemRect3>(); List<EyemRect3> tpRois = new List<EyemRect3>();
...@@ -735,6 +820,71 @@ namespace eyemLib_Sharp ...@@ -735,6 +820,71 @@ namespace eyemLib_Sharp
} }
} }
#region EyemImage转换成Bitmap
public static Bitmap eyemCvtToBitmap(EyemImage tpImage)
{
if (tpImage.vpImage == IntPtr.Zero)
throw new ArgumentNullException("图像不存在");
if (tpImage.iDepth != 0)
throw new ArgumentException("图像必须是8位无符号整型");
PixelFormat format;
switch (tpImage.iChannels)
{
case 1:
format = PixelFormat.Format8bppIndexed;
break;
case 3:
format = PixelFormat.Format24bppRgb;
break;
case 4:
format = PixelFormat.Format32bppArgb;
break;
default:
return null;
}
Bitmap bitmap = new Bitmap(tpImage.iWidth, tpImage.iHeight, format);
//对于输出灰度图像
if (format == PixelFormat.Format8bppIndexed)
{
ColorPalette palette = bitmap.Palette;
for (int i = 0; i < 256; i++)
{
palette.Entries[i] = Color.FromArgb(i, i, i);
}
bitmap.Palette = palette;
}
//锁定数据区
BitmapData bd = bitmap.LockBits(new Rectangle(0, 0, tpImage.iWidth, tpImage.iHeight),
ImageLockMode.WriteOnly, format);
try
{
int pd = ((tpImage.iWidth * tpImage.iChannels) + 3) / 4 * 4;
long bytesToCopy = tpImage.iWidth * tpImage.iChannels;
for (int y = 0; y < tpImage.iHeight; y++)
{
long offsetSrc = (y * tpImage.iWidth * tpImage.iChannels);
long offsetDst = (y * pd);
Buffer.MemoryCopy((byte*)(tpImage.vpImage.ToPointer()) + offsetSrc, (byte*)(bd.Scan0.ToPointer()) + offsetDst, bytesToCopy, bytesToCopy);
}
}
finally
{
bitmap.UnlockBits(bd);
}
return bitmap;
}
#endregion
#region 结构体转内存指针 #region 结构体转内存指针
public static IntPtr eyemStructArray2IntPtr<T>(T[] tpArray) public static IntPtr eyemStructArray2IntPtr<T>(T[] tpArray)
{ {
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
int eyemImageRead(const char *fileName, int iFlag, EyemImage *tpImage) int eyemImageRead(const char *fileName, int iFlag, EyemImage *tpImage)
{ {
cv::Mat _src = cv::imread(fileName, iFlag); cv::Mat _src = cv::imread(fileName, iFlag);
if (_src.empty()) if (_src.empty())
return FUNC_IMAGE_NOT_EXIST; return FUNC_IMAGE_NOT_EXIST;
...@@ -323,6 +323,9 @@ int eyemImageReadRaw(const char *filename, int iWidth, int iHeight, int iDepth, ...@@ -323,6 +323,9 @@ int eyemImageReadRaw(const char *filename, int iWidth, int iHeight, int iDepth,
else else
return FUNC_IMAGE_NOT_EXIST; return FUNC_IMAGE_NOT_EXIST;
//关闭文件
fclose(fp);
return FUNC_OK; return FUNC_OK;
} }
......
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
int main() int main()
{ {
//char* filename = "D:\\Matlabͼ\\circle_plate_04.png"; char* filename = "D:\\Matlabͼ\\circle_plate_04.png";
//cv::Mat src = cv::imread(filename, cv::IMREAD_GRAYSCALE); cv::Mat src = cv::imread(filename, cv::IMREAD_UNCHANGED);
//EyemImage tpImage; //EyemImage tpImage;
//tpImage.iHeight = src.rows; //tpImage.iHeight = src.rows;
//tpImage.iWidth = src.cols; //tpImage.iWidth = src.cols;
......
...@@ -835,7 +835,7 @@ extern "C" { ...@@ -835,7 +835,7 @@ extern "C" {
EXPORTS int eyemCountObject(EyemImage tpImage, const char *fileName, double dOffset, int iMinArea, int iMaxArea, int iWinSize, LPSTR *lpszNumObj, EyemImage *tpDstImg); EXPORTS int eyemCountObject(EyemImage tpImage, const char *fileName, double dOffset, int iMinArea, int iMaxArea, int iWinSize, LPSTR *lpszNumObj, EyemImage *tpDstImg);
EXPORTS int eyemCountObjectE(EyemImage tpImage, const char *fileName, LPSTR *lpszNumObj, EyemImage *tpDstImg); EXPORTS int eyemCountObjectE(EyemImage tpImage, const char *fileName, LPSTR *lpszNumObj, EyemImage *tpDstImg);
EXPORTS int eyemCountObjectIrregularParts(EyemImage tpImage, const char *fileName, double dOffset, const char * ccSubType, int iMaxArea, int iWinSize, LPSTR *lpszNumObj, EyemImage *tpDstImg); EXPORTS int eyemCountObjectIrregularParts(EyemImage tpImage, const char *fileName, double dOffset, const char * ccSubType, int iMaxArea, int iWinSize, LPSTR *lpszNumObj, EyemImage *tpDstImg);
EXPORTS int eyemCountObjectIrregularPartsE(EyemImage tpImage, const char *fileName, const char *ccTplName, double dMinScore, LPSTR *lpszNumObj, EyemImage *tpDstImg); EXPORTS int eyemCountObjectIrregularPartsE(EyemImage tpImage, const char *fileName, const char *ccTplName, LPSTR *lpszNumObj, EyemImage *tpDstImg);
EXPORTS int eyemAchvTemplateImage(EyemImage tpImage, EyemRect tpRoi, EyemImage *tpDstImg); EXPORTS int eyemAchvTemplateImage(EyemImage tpImage, EyemRect tpRoi, EyemImage *tpDstImg);
EXPORTS int eyemCreateTemplateModel(EyemImage tpImage, EyemRect tpRoi, double dMinScore, const char *ccTplName); EXPORTS int eyemCreateTemplateModel(EyemImage tpImage, EyemRect tpRoi, double dMinScore, const char *ccTplName);
EXPORTS int eyemMatchTemplateModel(EyemImage tpImage, const char *ccTplNames, LPSTR *lpszTplName); EXPORTS int eyemMatchTemplateModel(EyemImage tpImage, const char *ccTplNames, LPSTR *lpszTplName);
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!