Commit a2f38dd2 张士柳

1 个父辈 56be4c30
...@@ -755,6 +755,12 @@ namespace eyemLib_Sharp ...@@ -755,6 +755,12 @@ namespace eyemLib_Sharp
//通过名称获取模板 //通过名称获取模板
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)] [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
private static extern int eyemAchvModelByName(string ccTplName, IntPtr hModelID, ref EyemModelID tpModelID); private static extern int eyemAchvModelByName(string ccTplName, IntPtr hModelID, ref EyemModelID tpModelID);
//插入模板
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
private static extern int eyemInsertModel(ref IntPtr hModelID, string ccTplName);
//通过名称移除模板
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
private static extern int eyemRemoveModelByName(ref IntPtr hModelID, string ccTplName);
//释放模板内存 //释放模板内存
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)] [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
private static extern int eyemReleaseModel(ref IntPtr hModelID); private static extern int eyemReleaseModel(ref IntPtr hModelID);
...@@ -973,16 +979,30 @@ namespace eyemLib_Sharp ...@@ -973,16 +979,30 @@ namespace eyemLib_Sharp
//flag = eyemCreateTemplateModel(tpDstImg, tpRoi2, matchDeg, "D:\\模板文件\\" + file.Replace(".png", ".tpl")); //flag = eyemCreateTemplateModel(tpDstImg, tpRoi2, matchDeg, "D:\\模板文件\\" + file.Replace(".png", ".tpl"));
//加载模板到内存 //加载模板到内存
IntPtr hModelIDs = IntPtr.Zero; IntPtr hModelID = IntPtr.Zero;
flag = eyemInitModel("D:\\模板文件", out hModelIDs); flag = eyemInitModel("D:\\模板文件", out hModelID);
string selectModel = ""; string selectModel = "";
flag = eyemMatchTemplateModel(tpDstImg, hModelIDs, ref selectModel); flag = eyemMatchTemplateModel(tpDstImg, hModelID, ref selectModel);
//插入模板
flag = eyemInsertModel(ref hModelID, "D:\\模板文件及图像\\df871193-6632-48f9-abfe-540c3fc49c3f.tpl");
//根据名称获取模板 //根据名称获取模板
EyemModelID tpModelID = new EyemModelID(); EyemModelID tpModelID = new EyemModelID();
eyemAchvModelByName(selectModel.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0], hModelIDs, ref tpModelID); eyemAchvModelByName(selectModel.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0], hModelID, ref tpModelID);
//EyemImage tpModeImg = new EyemImage();
//tpModeImg.iChannels = 1; tpModeImg.iDepth = 0;
//tpModeImg.iWidth = tpModelID.iWidth; tpModeImg.iHeight = tpModelID.iHeight; tpModeImg.vpImage = tpModelID.vpImage;
//Bitmap bitmap = eyemCvtToBitmap(tpModeImg);
//if (bitmap != null)
//{
// bitmap.Save(System.Windows.Forms.Application.StartupPath + "\\ResOut\\" + file);
//}
//最好释放掉,如果对象供其他接口使用要先释放 //最好释放掉,如果对象供其他接口使用要先释放
eyemImageFree(ref tpDstImg); eyemImageFree(ref tpDstImg);
...@@ -991,9 +1011,12 @@ namespace eyemLib_Sharp ...@@ -991,9 +1011,12 @@ namespace eyemLib_Sharp
//eyemCountObject(image, tpRoi, file.Replace(".png", ""), 35, 0, 100, 5, ref pNumObj, out tpDstImg); //eyemCountObject(image, tpRoi, file.Replace(".png", ""), 35, 0, 100, 5, ref pNumObj, out tpDstImg);
//eyemCountObjectIrregularParts(image, tpRoi, file.Replace(".png", ""), 0.1, "IP_LARGE_PARTS", 100, 7, ref pNumObj, out tpDstImg); //eyemCountObjectIrregularParts(image, tpRoi, file.Replace(".png", ""), 0.1, "IP_LARGE_PARTS", 100, 7, ref pNumObj, out tpDstImg);
//eyemCountObjectE(image, tpRoi, fileName, ref pNumObj, out tpDstImg); //eyemCountObjectE(image, tpRoi, fileName, ref pNumObj, out tpDstImg);
eyemCountObjectIrregularPartsE(image, tpRoi, file.Replace(".png", ""), selectModel.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0], hModelIDs, ref pNumObj, out tpDstImg); eyemCountObjectIrregularPartsE(image, tpRoi, file.Replace(".png", ""), selectModel.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0], hModelID, ref pNumObj, out tpDstImg);
//eyemCountObjectIrregularPartsE(image, tpRoi, file.Replace(".png", ""), "D:\\模板文件\\" + /*file.Replace(".png", ".tpl")*/"74d571ed-9fd4-4959-85dd-3195261e4b48.tpl", ref pNumObj, out tpDstImg); //eyemCountObjectIrregularPartsE(image, tpRoi, file.Replace(".png", ""), "D:\\模板文件\\" + /*file.Replace(".png", ".tpl")*/"74d571ed-9fd4-4959-85dd-3195261e4b48.tpl", ref pNumObj, out tpDstImg);
//移除模板
flag = eyemRemoveModelByName(ref hModelID, "D:\\模板文件及图像\\df871193-6632-48f9-abfe-540c3fc49c3f.tpl");
//Bitmap bitmap = eyemCvtToBitmap(tpDstImg); //Bitmap bitmap = eyemCvtToBitmap(tpDstImg);
//if (bitmap != null) //if (bitmap != null)
...@@ -1016,7 +1039,7 @@ namespace eyemLib_Sharp ...@@ -1016,7 +1039,7 @@ namespace eyemLib_Sharp
Console.WriteLine("耗时:" + sw.ElapsedMilliseconds.ToString() + ",结果:" + pNumObj); Console.WriteLine("耗时:" + sw.ElapsedMilliseconds.ToString() + ",结果:" + pNumObj);
//在关闭程序时释放 //在关闭程序时释放
eyemReleaseModel(ref hModelIDs); eyemReleaseModel(ref hModelID);
//free image //free image
eyemImageFree(ref tpDstImg); eyemImageFree(ref tpDstImg);
eyemImageFree(ref image); eyemImageFree(ref image);
...@@ -1049,6 +1072,13 @@ namespace eyemLib_Sharp ...@@ -1049,6 +1072,13 @@ namespace eyemLib_Sharp
//获取用于制作模板的图像 //获取用于制作模板的图像
flag = eyemAchvTemplateImage(image, tpRoi, out tpDstImg); flag = eyemAchvTemplateImage(image, tpRoi, out tpDstImg);
//测试插入模板
//eyemInsertModel(ref hModelID, "D:\\模板文件及图像\\df871193-6632-48f9-abfe-540c3fc49c3f.tpl");
////测试获取模板
//EyemModelID tpModelID0 = new EyemModelID();
//eyemAchvModelByName("D:\\模板文件及图像\\df871193-6632-48f9-abfe-540c3fc49c3f.tpl", hModelID, ref tpModelID0);
string selectModel = ""; string selectModel = "";
flag = eyemMatchTemplateModel(tpDstImg, hModelID, ref selectModel); flag = eyemMatchTemplateModel(tpDstImg, hModelID, ref selectModel);
...@@ -1056,11 +1086,13 @@ namespace eyemLib_Sharp ...@@ -1056,11 +1086,13 @@ namespace eyemLib_Sharp
EyemModelID tpModelID = new EyemModelID(); EyemModelID tpModelID = new EyemModelID();
eyemAchvModelByName(selectModel.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0], hModelID, ref tpModelID); eyemAchvModelByName(selectModel.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0], hModelID, ref tpModelID);
//EyemImage tpModeImg = new EyemImage(); EyemImage tpModeImg = new EyemImage();
//tpModeImg.iChannels = 1; tpModeImg.iDepth = 0; tpModeImg.iChannels = 1; tpModeImg.iDepth = 0;
//tpModeImg.iWidth = tpModelID.iWidth; tpModeImg.iHeight = tpModelID.iHeight; tpModeImg.vpImage = tpModelID.vpImage; tpModeImg.iWidth = tpModelID.iWidth; tpModeImg.iHeight = tpModelID.iHeight; tpModeImg.vpImage = tpModelID.vpImage;
//Bitmap bitmap = eyemCvtToBitmap(tpModeImg); string str = Marshal.PtrToStringAnsi(tpModelID.lpszName);
Bitmap bitmap = eyemCvtToBitmap(tpModeImg);
//if (bitmap != null) //if (bitmap != null)
//{ //{
...@@ -1073,6 +1105,9 @@ namespace eyemLib_Sharp ...@@ -1073,6 +1105,9 @@ namespace eyemLib_Sharp
//点料 //点料
eyemCountObjectIrregularPartsE(image, tpRoi, file.Replace(".png", ""), selectModel.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0], hModelID, ref pNumObj, out tpDstImg); eyemCountObjectIrregularPartsE(image, tpRoi, file.Replace(".png", ""), selectModel.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0], hModelID, ref pNumObj, out tpDstImg);
//测试移除模板
//eyemRemoveModelByName(ref hModelID, "D:\\模板文件及图像\\df871193-6632-48f9-abfe-540c3fc49c3f.tpl");
//free image //free image
eyemImageFree(ref tpDstImg); eyemImageFree(ref tpDstImg);
eyemImageFree(ref image); eyemImageFree(ref image);
......
...@@ -23,19 +23,33 @@ namespace eyemLib_Sharp ...@@ -23,19 +23,33 @@ namespace eyemLib_Sharp
//} //}
//EyemLib.eyemTest2(fileNames); //EyemLib.eyemTest2(fileNames);
//foreach (var item in fileNames)
//{
// EyemLib.eyemReadImageTool(item);
//}
//for (int i = 0; i < 1; i++) //for (int i = 0; i < 1; i++)
//{ //{
// EyemLib.eyemTestVideoCapture("D:\\插件完成检测\\视频\\WeChat2.mp4"); // EyemLib.eyemTestVideoCapture("D:\\插件完成检测\\视频\\WeChat2.mp4");
//} //}
IntPtr hModelID; IntPtr hModelID;
EyemLib.eyemInitModelE(out hModelID); EyemLib.eyemInitModelE(out hModelID);
for (int i = 0; i < 5000; i++) for (int i = 0; i < 5000; i++)
{ {
for (int j = 0; j < fileNames.Length; j++) //for (int j = 0; j < fileNames.Length; j++)
//{
// EyemLib.eyemReadImageToolE(fileNames[j], hModelID);
//}
//并行测试
ParallelOptions po = new ParallelOptions();
po.MaxDegreeOfParallelism = 3;
Parallel.ForEach(fileNames, po, fn =>
{ {
EyemLib.eyemReadImageToolE(fileNames[j], hModelID); EyemLib.eyemReadImageToolE(fn, hModelID);
} });
} }
EyemLib.eyemReleaseModelE(ref hModelID); EyemLib.eyemReleaseModelE(ref hModelID);
......
...@@ -1225,6 +1225,7 @@ int eyemDetectAndDecode(EyemImage tpImage, EyemRect tpRoi, const char *ccFileNam ...@@ -1225,6 +1225,7 @@ int eyemDetectAndDecode(EyemImage tpImage, EyemRect tpRoi, const char *ccFileNam
bool eyemDetectAndDecodeFree(IntPtr hObject) bool eyemDetectAndDecodeFree(IntPtr hObject)
{ {
std::vector<EyemBarCode> *tpResults = reinterpret_cast<std::vector<EyemBarCode>*>(hObject); std::vector<EyemBarCode> *tpResults = reinterpret_cast<std::vector<EyemBarCode>*>(hObject);
//清空容器 //清空容器
tpResults->clear(); tpResults->clear();
......
...@@ -894,9 +894,13 @@ bool eyemVideoCaptureFree(IntPtr hObject) ...@@ -894,9 +894,13 @@ bool eyemVideoCaptureFree(IntPtr hObject)
for (std::vector<EyemImage>::iterator it = tpImages->begin(); it != tpImages->end(); ++it) for (std::vector<EyemImage>::iterator it = tpImages->begin(); it != tpImages->end(); ++it)
{ {
(*it).iWidth = (*it).iHeight = (*it).iDepth = (*it).iChannels = 0; EyemImage *tpImage = &(*it);
free((*it).vpImage), (*it).vpImage = NULL; tpImage->iWidth = tpImage->iHeight = tpImage->iDepth = tpImage->iChannels = 0;
free(tpImage->vpImage), tpImage->vpImage = NULL;
} }
//清空
tpImages->clear();
//释放 //释放
delete tpImages; delete tpImages;
tpImages = NULL; tpImages = NULL;
......
...@@ -853,8 +853,8 @@ extern "C" { ...@@ -853,8 +853,8 @@ extern "C" {
EXPORTS int eyemMatchTemplateModel(EyemImage tpImage, IntPtr hModelID, LPSTR *lpszTplName); EXPORTS int eyemMatchTemplateModel(EyemImage tpImage, IntPtr hModelID, LPSTR *lpszTplName);
EXPORTS int eyemInitModel(const char *ccTplName, IntPtr *hModelID); EXPORTS int eyemInitModel(const char *ccTplName, IntPtr *hModelID);
EXPORTS int eyemAchvModelByName(const char *ccTplName, IntPtr hModelID, EyemModelID &tpModelID); EXPORTS int eyemAchvModelByName(const char *ccTplName, IntPtr hModelID, EyemModelID &tpModelID);
EXPORTS int eyemInsertModel(const char *ccTplName, IntPtr hModelID, EyemModelID &tpModelID); EXPORTS int eyemInsertModel(IntPtr &hModelID, const char *ccTplName);
EXPORTS int eyemInsertModel(const char *ccTplName, IntPtr hModelID, EyemModelID &tpModelID); EXPORTS int eyemRemoveModelByName(IntPtr &hModelID, const char *ccTplName);
EXPORTS int eyemReleaseModel(IntPtr &hModelID); EXPORTS int eyemReleaseModel(IntPtr &hModelID);
EXPORTS int eyemTrackFeature(EyemImage tpPrevImg, EyemImage tpNextImg, EyemRect3 *tpRois, int iRoiNum, int *ipResults); EXPORTS int eyemTrackFeature(EyemImage tpPrevImg, EyemImage tpNextImg, EyemRect3 *tpRois, int iRoiNum, int *ipResults);
EXPORTS int eyemAOIForTSAV(EyemImage tpRefImg, EyemImage tpNextImg, EyemRect3 *tpRois, int iRoiNum); EXPORTS int eyemAOIForTSAV(EyemImage tpRefImg, EyemImage tpNextImg, EyemRect3 *tpRois, int iRoiNum);
......
...@@ -276,68 +276,75 @@ static void loadTrackModel(const char *fileName, cv::OutputArray tplMat, cv::Poi ...@@ -276,68 +276,75 @@ static void loadTrackModel(const char *fileName, cv::OutputArray tplMat, cv::Poi
if (stat(fileName, &_Stat) != 0) if (stat(fileName, &_Stat) != 0)
return; return;
FILE *fps = fopen(fileName, "r"); //上锁
mtx.lock();
FILE *fps = NULL;
fopen_s(&fps, fileName, "r");
//判断文件是否打开
if (NULL == fps) if (NULL == fps)
return; return;
fseek(fps, 0, SEEK_END);
//获取文件大小 //获取文件大小
const int _Size = ftell(fps); const int _Size = (int)_Stat.st_size;
//重新指向文件开始
rewind(fps);
//分配内存
unsigned char *_Data = (unsigned char *)malloc(_Size); unsigned char *_Data = (unsigned char *)malloc(_Size);
if (NULL == _Data) if (NULL == _Data)
return; return;
//初始化
memset(_Data, 0, _Size);
//读取数据 //读取数据
fread(_Data, sizeof(uint8_t), _Size, fps); fread(_Data, sizeof(uint8_t), _Size, fps);
//偏移量 //关闭文件
const int offset = 8; fclose(fps);
//解锁
mtx.unlock();
//获取图像数据大小 //获取图像数据大小
unsigned char header[offset]; unsigned char _SizeData[8];
memcpy(header, _Data, offset); memcpy(_SizeData, _Data, 8);
//有效信息长度 //有效信息长度
const int valSize = std::atoi((const char *)header); const int valSize = std::atoi((const char *)_SizeData);
//文件信息 //文件信息
const int bitSize = _Size - valSize - offset; const int tplSize = _Size - valSize - 8;
char *bitInfo = new char[bitSize]; //拷贝文件信息
memcpy(bitInfo, _Data + valSize + offset, bitSize); char *tplInfo = new char[tplSize];
memcpy(tplInfo, _Data + valSize + 8, tplSize);
//获取文件信息 //获取文件信息
std::string line(bitInfo); std::string line(tplInfo);
std::vector<std::string> hints; std::vector<std::string> hints;
split(line, ",", hints); split(line, ",", hints);
//释放内存
delete[] bitInfo;
bitInfo = NULL;
//宽,高,坐标,匹配度 //宽,高,坐标,匹配度
int X = std::atoi(hints[2].c_str()), Y = std::atoi(hints[3].c_str()); int X = std::atoi(hints[2].c_str()), Y = std::atoi(hints[3].c_str());
dMatchDeg = std::atof(hints[4].c_str()); pt = cv::Point(std::atoi(hints[0].c_str()), std::atoi(hints[1].c_str()));
dMatchDeg = std::atof(hints[4].substr(0, 4).c_str()); pt = cv::Point(std::atoi(hints[0].c_str()), std::atoi(hints[1].c_str()));
//创建图像文件 //创建图像文件
tplMat.create(Y, X, CV_8UC1, -1, true); tplMat.create(Y, X, CV_8UC1, -1, true);
//拷贝数据 //拷贝数据
cv::Mat _tplMat = tplMat.getMat(); cv::Mat _tplMat = tplMat.getMat();
memcpy(_tplMat.data, _Data + offset, valSize); memcpy(_tplMat.data, _Data + 8, valSize);
//释放内存 //释放内存
delete[] tplInfo;
tplInfo = NULL;
//释放文件内存
free(_Data); free(_Data);
_Data = NULL; _Data = NULL;
//关闭文件
fclose(fps);
} }
/** 获取文件夹内所有文件 /** 获取文件夹内所有文件
...@@ -499,7 +506,7 @@ static double getThreshVal_Otsu_8u(const cv::Mat& _src) ...@@ -499,7 +506,7 @@ static double getThreshVal_Otsu_8u(const cv::Mat& _src)
} }
return max_val; return max_val;
} }
/** 计算元件尺寸 /** 计算元件尺寸
srcPrev 输入图像 srcPrev 输入图像
...@@ -620,7 +627,7 @@ static bool checkSize(cv::Mat &srcPrev, cv::Mat &mask, int &partSize) ...@@ -620,7 +627,7 @@ static bool checkSize(cv::Mat &srcPrev, cv::Mat &mask, int &partSize)
int eyemCountObject(EyemImage tpImage, EyemRect tpRoi, const char *fileName, double dOffset, int iMinArea, int iMaxArea, int iWinSize, LPSTR *lpszNumObj, EyemImage *tpDstImg) int eyemCountObject(EyemImage tpImage, EyemRect tpRoi, const char *fileName, double dOffset, int iMinArea, int iMaxArea, int iWinSize, LPSTR *lpszNumObj, EyemImage *tpDstImg)
{ {
cv::Mat src = cv::Mat(tpImage.iHeight, tpImage.iWidth, MAKETYPE(tpImage.iDepth, tpImage.iChannels), tpImage.vpImage); cv::Mat src = cv::Mat(tpImage.iHeight, tpImage.iWidth, MAKETYPE(tpImage.iDepth, tpImage.iChannels), tpImage.vpImage).clone();
if (src.empty()) { if (src.empty()) {
return FUNC_IMAGE_NOT_EXIST; return FUNC_IMAGE_NOT_EXIST;
} }
...@@ -1589,7 +1596,7 @@ int eyemCountObject(EyemImage tpImage, EyemRect tpRoi, const char *fileName, dou ...@@ -1589,7 +1596,7 @@ int eyemCountObject(EyemImage tpImage, EyemRect tpRoi, const char *fileName, dou
int eyemCountObjectIrregularParts(EyemImage tpImage, EyemRect tpRoi, const char *fileName, double dOffset, const char * ccSubType, int iMaxArea, int iWinSize, LPSTR *lpszNumObj, EyemImage *tpDstImg) int eyemCountObjectIrregularParts(EyemImage tpImage, EyemRect tpRoi, const char *fileName, double dOffset, const char * ccSubType, int iMaxArea, int iWinSize, LPSTR *lpszNumObj, EyemImage *tpDstImg)
{ {
cv::Mat src = cv::Mat(tpImage.iHeight, tpImage.iWidth, MAKETYPE(tpImage.iDepth, tpImage.iChannels), tpImage.vpImage); cv::Mat src = cv::Mat(tpImage.iHeight, tpImage.iWidth, MAKETYPE(tpImage.iDepth, tpImage.iChannels), tpImage.vpImage).clone();
if (src.empty()) { if (src.empty()) {
return FUNC_IMAGE_NOT_EXIST; return FUNC_IMAGE_NOT_EXIST;
} }
...@@ -3113,6 +3120,7 @@ int eyemCountObjectIrregularParts(EyemImage tpImage, EyemRect tpRoi, const char ...@@ -3113,6 +3120,7 @@ int eyemCountObjectIrregularParts(EyemImage tpImage, EyemRect tpRoi, const char
filePath += "\\ResOut"; filePath += "\\ResOut";
if (_access(filePath.c_str(), 0) == -1) if (_access(filePath.c_str(), 0) == -1)
_mkdir(filePath.c_str());//不存在则创建 _mkdir(filePath.c_str());//不存在则创建
//格式化文件名 //格式化文件名
char file[256]; char file[256];
sprintf_s(file, "%s\\%s-Mark.png", filePath.c_str(), fileName); sprintf_s(file, "%s\\%s-Mark.png", filePath.c_str(), fileName);
...@@ -3123,7 +3131,7 @@ int eyemCountObjectIrregularParts(EyemImage tpImage, EyemRect tpRoi, const char ...@@ -3123,7 +3131,7 @@ int eyemCountObjectIrregularParts(EyemImage tpImage, EyemRect tpRoi, const char
int eyemCountObjectE(EyemImage tpImage, EyemRect tpRoi, const char *fileName, LPSTR *lpszNumObj, EyemImage *tpDstImg) int eyemCountObjectE(EyemImage tpImage, EyemRect tpRoi, const char *fileName, LPSTR *lpszNumObj, EyemImage *tpDstImg)
{ {
cv::Mat src = cv::Mat(tpImage.iHeight, tpImage.iWidth, MAKETYPE(tpImage.iDepth, tpImage.iChannels), tpImage.vpImage); cv::Mat src = cv::Mat(tpImage.iHeight, tpImage.iWidth, MAKETYPE(tpImage.iDepth, tpImage.iChannels), tpImage.vpImage).clone();
if (src.empty()) { if (src.empty()) {
return FUNC_IMAGE_NOT_EXIST; return FUNC_IMAGE_NOT_EXIST;
} }
...@@ -4098,7 +4106,7 @@ int eyemCountObjectE(EyemImage tpImage, EyemRect tpRoi, const char *fileName, LP ...@@ -4098,7 +4106,7 @@ int eyemCountObjectE(EyemImage tpImage, EyemRect tpRoi, const char *fileName, LP
int eyemCountObjectIrregularPartsE(EyemImage tpImage, EyemRect tpRoi, const char *fileName, const char *ccTplName, IntPtr hModelID, LPSTR *lpszReelNum, EyemImage *tpDstImg) int eyemCountObjectIrregularPartsE(EyemImage tpImage, EyemRect tpRoi, const char *fileName, const char *ccTplName, IntPtr hModelID, LPSTR *lpszReelNum, EyemImage *tpDstImg)
{ {
cv::Mat src = cv::Mat(tpImage.iHeight, tpImage.iWidth, MAKETYPE(tpImage.iDepth, tpImage.iChannels), tpImage.vpImage); cv::Mat src = cv::Mat(tpImage.iHeight, tpImage.iWidth, MAKETYPE(tpImage.iDepth, tpImage.iChannels), tpImage.vpImage).clone();
//判断文件是否存在 //判断文件是否存在
if (src.empty() || NULL == hModelID) { if (src.empty() || NULL == hModelID) {
...@@ -4734,12 +4742,12 @@ int eyemCountObjectIrregularPartsE(EyemImage tpImage, EyemRect tpRoi, const char ...@@ -4734,12 +4742,12 @@ int eyemCountObjectIrregularPartsE(EyemImage tpImage, EyemRect tpRoi, const char
trackEnd = (!found); trackEnd = (!found);
} while (!trackEnd); } while (!trackEnd);
} }
#if OPTIMIZE_ON #if OPTIMIZE_ON
, ,
[&] [&]
#endif #endif
{ {
///< 逆时针追踪 ///< 逆时针追踪
//追踪起点 //追踪起点
cv::Point2f trackCenter(startCenter.x, startCenter.y); cv::Point2f trackCenter(startCenter.x, startCenter.y);
...@@ -4834,7 +4842,7 @@ int eyemCountObjectIrregularPartsE(EyemImage tpImage, EyemRect tpRoi, const char ...@@ -4834,7 +4842,7 @@ int eyemCountObjectIrregularPartsE(EyemImage tpImage, EyemRect tpRoi, const char
{ {
cv::line(cc, ptsT[j], ptsT[(j + 1) % 4], cv::Scalar(0, 0, 255, 255), 1); cv::line(cc, ptsT[j], ptsT[(j + 1) % 4], cv::Scalar(0, 0, 255, 255), 1);
} }
} }
#endif #endif
...@@ -4892,10 +4900,10 @@ int eyemCountObjectIrregularPartsE(EyemImage tpImage, EyemRect tpRoi, const char ...@@ -4892,10 +4900,10 @@ int eyemCountObjectIrregularPartsE(EyemImage tpImage, EyemRect tpRoi, const char
} }
trackEnd = (!found); trackEnd = (!found);
} while (!trackEnd); } while (!trackEnd);
} }
#if OPTIMIZE_ON #if OPTIMIZE_ON
); );
#endif #endif
} }
...@@ -4942,7 +4950,7 @@ int eyemCountObjectIrregularPartsE(EyemImage tpImage, EyemRect tpRoi, const char ...@@ -4942,7 +4950,7 @@ int eyemCountObjectIrregularPartsE(EyemImage tpImage, EyemRect tpRoi, const char
int eyemAchvTemplateImage(EyemImage tpImage, EyemRect tpRoi, EyemImage *tpDstImg) int eyemAchvTemplateImage(EyemImage tpImage, EyemRect tpRoi, EyemImage *tpDstImg)
{ {
cv::Mat image = cv::Mat(tpImage.iHeight, tpImage.iWidth, MAKETYPE(tpImage.iDepth, tpImage.iChannels), tpImage.vpImage); cv::Mat image = cv::Mat(tpImage.iHeight, tpImage.iWidth, MAKETYPE(tpImage.iDepth, tpImage.iChannels), tpImage.vpImage).clone();
//检查文件是否存在 //检查文件是否存在
if (image.empty()) if (image.empty())
...@@ -5025,7 +5033,7 @@ int eyemAchvTemplateImage(EyemImage tpImage, EyemRect tpRoi, EyemImage *tpDstImg ...@@ -5025,7 +5033,7 @@ int eyemAchvTemplateImage(EyemImage tpImage, EyemRect tpRoi, EyemImage *tpDstImg
int eyemCreateTemplateModel(EyemImage tpImage, EyemRect tpRoi, double dMinScore, const char *ccTplName) int eyemCreateTemplateModel(EyemImage tpImage, EyemRect tpRoi, double dMinScore, const char *ccTplName)
{ {
cv::Mat image = cv::Mat(tpImage.iHeight, tpImage.iWidth, MAKETYPE(tpImage.iDepth, tpImage.iChannels), tpImage.vpImage); cv::Mat image = cv::Mat(tpImage.iHeight, tpImage.iWidth, MAKETYPE(tpImage.iDepth, tpImage.iChannels), tpImage.vpImage).clone();
if (image.empty()) if (image.empty())
return FUNC_IMAGE_NOT_EXIST; return FUNC_IMAGE_NOT_EXIST;
...@@ -5074,6 +5082,186 @@ int eyemCreateTemplateModel(EyemImage tpImage, EyemRect tpRoi, double dMinScore, ...@@ -5074,6 +5082,186 @@ int eyemCreateTemplateModel(EyemImage tpImage, EyemRect tpRoi, double dMinScore,
return FUNC_OK; return FUNC_OK;
} }
int eyemInitModel(const char *ccTplName, IntPtr *hModelID)
{
//获取文件路径
std::vector<std::string> fileNames;
cv::glob(ccTplName, fileNames);
//判断文件
if (fileNames.size() <= 0)
return FUNC_CANNOT_CALC;
//载入所有模板
std::vector<EyemModelID> *tpModelID = new std::vector<EyemModelID>();
for (std::vector<std::string>::iterator it = fileNames.begin(); it != fileNames.end(); ++it)
{
std::string fileName = (*it);
cv::Mat tplMat; double matchDeg;
loadTrackModel(fileName.c_str(), tplMat, cv::Point(), matchDeg);
if (!tplMat.empty()) {
cv::Mat _tplMat;
cv::bitwise_not(tplMat, _tplMat);
//载入模板
EyemModelID modelID;
int _Size = tplMat.cols*tplMat.rows * sizeof(unsigned char);
//数据
modelID.vpImage = (unsigned char *)malloc(_Size);
if (NULL == modelID.vpImage)
return FUNC_NOT_ENOUGH_MEM;
//拷贝数据
memcpy(modelID.vpImage, _tplMat.data, _Size);
//位置
modelID.iXs = modelID.iYs = 0;
//宽、高
modelID.iWidth = tplMat.cols; modelID.iHeight = tplMat.rows;
//匹配分数
modelID.dMatchDeg = matchDeg;
//名称
modelID.lpszName = (char *)CoTaskMemAlloc(128);
if (NULL != modelID.lpszName)
{
char file[128] = { 0 };
sprintf_s(file, "%s", fileName.c_str());
strcpy(modelID.lpszName, file);
}
else return FUNC_NOT_ENOUGH_MEM;
//压入堆栈
tpModelID->push_back(modelID);
}
}
//输出
*hModelID = reinterpret_cast<IntPtr>(tpModelID);
return FUNC_OK;
}
int eyemAchvModelByName(const char *ccTplName, IntPtr hModelID, EyemModelID &tpModelID)
{
if (NULL == hModelID)
return FUNC_CANNOT_CALC;
std::vector<EyemModelID> *tpModelIDs = reinterpret_cast<std::vector<EyemModelID>*>(hModelID);
for (std::vector<EyemModelID>::iterator it = tpModelIDs->begin(); it != tpModelIDs->end(); ++it) {
if (std::strcmp((*it).lpszName, ccTplName) == 0) {
EyemModelID modelID = (*it);
tpModelID.dMatchDeg = modelID.dMatchDeg; tpModelID.iHeight = modelID.iHeight; tpModelID.iWidth = modelID.iWidth;
tpModelID.iXs = tpModelID.iYs = 0;
tpModelID.lpszName = modelID.lpszName;
tpModelID.vpImage = modelID.vpImage;
break;
}
}
return FUNC_OK;
}
int eyemInsertModel(IntPtr &hModelID, const char *ccTplName)
{
if (NULL == hModelID)
return FUNC_CANNOT_CALC;
std::vector<EyemModelID> *tpModelID = reinterpret_cast<std::vector<EyemModelID>*>(hModelID);
//加载指定模板
cv::Mat tplMat; double matchDeg;
loadTrackModel(ccTplName, tplMat, cv::Point(), matchDeg);
//插到容器末尾
if (!tplMat.empty()) {
cv::Mat _tplMat;
cv::bitwise_not(tplMat, _tplMat);
//载入模板
EyemModelID modelID;
int _Size = tplMat.cols*tplMat.rows * sizeof(unsigned char);
//数据
modelID.vpImage = (unsigned char *)malloc(_Size);
if (NULL == modelID.vpImage)
return FUNC_NOT_ENOUGH_MEM;
//拷贝数据
memcpy(modelID.vpImage, _tplMat.data, _Size);
//位置
modelID.iXs = modelID.iYs = 0;
//宽、高
modelID.iWidth = tplMat.cols; modelID.iHeight = tplMat.rows;
//匹配分数
modelID.dMatchDeg = matchDeg;
//名称
modelID.lpszName = (char *)CoTaskMemAlloc(128);
if (NULL != modelID.lpszName)
{
char file[128] = { 0 };
sprintf_s(file, "%s", ccTplName);
strcpy(modelID.lpszName, file);
}
else {
return FUNC_NOT_ENOUGH_MEM;
}
//压入堆栈
tpModelID->push_back(modelID);
}
else {
return FUNC_CANNOT_CALC;
}
return FUNC_OK;
}
int eyemRemoveModelByName(IntPtr &hModelID, const char *ccTplName)
{
if (NULL == hModelID)
return FUNC_CANNOT_CALC;
std::vector<EyemModelID> *tpModelID = reinterpret_cast<std::vector<EyemModelID>*>(hModelID);
//遍历移除
for (std::vector<EyemModelID>::iterator it = tpModelID->begin(); it != tpModelID->end(); ++it) {
EyemModelID *modelID = &(*it);
if (std::strcmp((const char *)modelID->lpszName, ccTplName) == 0) {
//释放资源
modelID->dMatchDeg = modelID->iHeight = modelID->iWidth = modelID->iXs = modelID->iYs = 0;
//释放内容
CoTaskMemFree((LPVOID)modelID->lpszName);
free(modelID->vpImage);
//
modelID->vpImage = NULL;
//删除
tpModelID->erase(it);
break;
}
}
return FUNC_OK;
}
int eyemMatchTemplateModel(EyemImage tpImage, IntPtr hModelID, LPSTR *lpszTplName) int eyemMatchTemplateModel(EyemImage tpImage, IntPtr hModelID, LPSTR *lpszTplName)
{ {
cv::Mat image = cv::Mat(tpImage.iHeight, tpImage.iWidth, MAKETYPE(tpImage.iDepth, tpImage.iChannels), tpImage.vpImage).clone(); cv::Mat image = cv::Mat(tpImage.iHeight, tpImage.iWidth, MAKETYPE(tpImage.iDepth, tpImage.iChannels), tpImage.vpImage).clone();
...@@ -5182,88 +5370,8 @@ int eyemMatchTemplateModel(EyemImage tpImage, IntPtr hModelID, LPSTR *lpszTplNam ...@@ -5182,88 +5370,8 @@ int eyemMatchTemplateModel(EyemImage tpImage, IntPtr hModelID, LPSTR *lpszTplNam
{ {
strcpy(*lpszTplName, bestMatch.c_str()); strcpy(*lpszTplName, bestMatch.c_str());
} }
else return FUNC_NOT_ENOUGH_MEM; else {
return FUNC_OK;
}
int eyemInitModel(const char *ccTplName, IntPtr *hModelID)
{
//获取文件路径
std::vector<std::string> fileNames;
cv::glob(ccTplName, fileNames);
//载入所有模板
std::vector<EyemModelID> *tpModelIDs = new std::vector<EyemModelID>();
for (std::vector<std::string>::iterator it = fileNames.begin(); it != fileNames.end(); ++it)
{
std::string fileName = (*it);
cv::Mat tplMat; double matchDeg;
loadTrackModel(fileName.c_str(), tplMat, cv::Point(), matchDeg);
if (!tplMat.empty()) {
cv::Mat _tplMat;
cv::bitwise_not(tplMat, _tplMat);
//载入模板
EyemModelID modelID;
int _Size = tplMat.cols*tplMat.rows * sizeof(unsigned char);
//数据
modelID.vpImage = (unsigned char *)malloc(_Size);
if (NULL == modelID.vpImage)
return FUNC_NOT_ENOUGH_MEM; return FUNC_NOT_ENOUGH_MEM;
//拷贝数据
memcpy(modelID.vpImage, _tplMat.data, _Size);
//位置
modelID.iXs = modelID.iYs = 0;
//宽、高
modelID.iWidth = tplMat.cols; modelID.iHeight = tplMat.rows;
//匹配分数
modelID.dMatchDeg = matchDeg;
//名称
modelID.lpszName = (char *)CoTaskMemAlloc(128);
if (NULL != modelID.lpszName)
{
char file[128] = { 0 };
sprintf_s(file, "%s", fileName.c_str());
strcpy(modelID.lpszName, file);
}
else return FUNC_NOT_ENOUGH_MEM;
//压入堆栈
tpModelIDs->push_back(modelID);
}
}
//输出
*hModelID = reinterpret_cast<IntPtr>(tpModelIDs);
return FUNC_OK;
}
int eyemAchvModelByName(const char *ccTplName, IntPtr hModelID, EyemModelID &tpModelID)
{
std::vector<EyemModelID> *tpModelIDs = reinterpret_cast<std::vector<EyemModelID>*>(hModelID);
for (std::vector<EyemModelID>::iterator it = tpModelIDs->begin(); it != tpModelIDs->end(); ++it) {
if (std::strcmp((*it).lpszName, ccTplName) == 0) {
EyemModelID modelID = (*it);
tpModelID.dMatchDeg = modelID.dMatchDeg; tpModelID.iHeight = modelID.iHeight; tpModelID.iWidth = modelID.iWidth;
tpModelID.iXs = tpModelID.iYs = 0;
tpModelID.lpszName = modelID.lpszName;
tpModelID.vpImage = modelID.vpImage;
break;
}
} }
return FUNC_OK; return FUNC_OK;
...@@ -5274,22 +5382,24 @@ int eyemReleaseModel(IntPtr &hModelID) ...@@ -5274,22 +5382,24 @@ int eyemReleaseModel(IntPtr &hModelID)
if (NULL == hModelID) if (NULL == hModelID)
return FUNC_OK; return FUNC_OK;
std::vector<EyemModelID> *tpResults = reinterpret_cast<std::vector<EyemModelID>*>(hModelID); std::vector<EyemModelID> *tpModelID = reinterpret_cast<std::vector<EyemModelID>*>(hModelID);
for (std::vector<EyemModelID>::iterator it = tpResults->begin(); it != tpResults->end(); ++it) for (std::vector<EyemModelID>::iterator it = tpModelID->begin(); it != tpModelID->end(); ++it) {
{
(*it).dMatchDeg = (*it).iHeight = (*it).iWidth = (*it).iXs = (*it).iYs = 0; EyemModelID *modelID = &(*it);
modelID->dMatchDeg = modelID->iHeight = modelID->iWidth = modelID->iXs = modelID->iYs = 0;
//释放内容 //释放内容
CoTaskMemFree((LPVOID)(*it).lpszName); CoTaskMemFree((LPVOID)modelID->lpszName);
free((*it).vpImage); free(modelID->vpImage);
} }
//清空容器 //清空容器
tpResults->clear(); tpModelID->clear();
//释放容器 //释放容器
delete tpResults; delete tpModelID;
tpResults = NULL; hModelID = NULL; tpModelID = NULL; hModelID = NULL;
return FUNC_OK; return FUNC_OK;
} }
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
constexpr double c = PI / 180.; constexpr double c = PI / 180.;
//std::mutex mtx; std::mutex mtx;
#endif/* __EYEM_MISC_H */ #endif/* __EYEM_MISC_H */
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!