Commit 29630f64 张士柳

1 个父辈 b2c1dc1c
...@@ -983,6 +983,13 @@ namespace eyemLib_Sharp ...@@ -983,6 +983,13 @@ namespace eyemLib_Sharp
private static extern int eyemNNDetector(EyemImage tpImage, out int ipNum, ref BboxContainer container); private static extern int eyemNNDetector(EyemImage tpImage, out int ipNum, ref BboxContainer container);
#endregion #endregion
#region 模板匹配
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
private static extern int eyemMakeShapeModel(EyemImage tpImage, double dContrast, double dMinContrast, int iApertureSize = 3, bool bL2gradient = false);
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
private static extern int eyemFindShapeModel(EyemImage tpImage, int iNumLevels, double dAngleStart, double dAngleExtent, double dAngleStep, double dMinContrast, double dContrast, double dGreediness, double dMinScore, bool bOptimization = true);
#endregion
#region 项目 #region 项目
//普通器件(仍采用旧的算法) //普通器件(仍采用旧的算法)
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)] [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
...@@ -1144,12 +1151,34 @@ namespace eyemLib_Sharp ...@@ -1144,12 +1151,34 @@ namespace eyemLib_Sharp
//flag = eyemLibImpl(image, tpHsvModel, out tpDstImg); //flag = eyemLibImpl(image, tpHsvModel, out tpDstImg);
EyemImage templ, search;
flag = eyemImageRead("D://批量测试图像//template.png", -1, out templ);
if (flag != 0)
{
Console.WriteLine("读图失败!");
return;
}
flag = eyemImageRead("D://批量测试图像//search1.png", -1, out search);
if (flag != 0)
{
Console.WriteLine("读图失败!");
return;
}
flag = eyemMakeShapeModel(templ, 100, 10);
flag = eyemFindShapeModel(search, 10, 0, 90, 1, 10, 100, 0.9, 0.9);
//Bitmap bitmap = eyemCvtToBitmap(tpDstImg); //Bitmap bitmap = eyemCvtToBitmap(tpDstImg);
//if (bitmap != null) //if (bitmap != null)
//{ //{
// bitmap.Save(System.Windows.Forms.Application.StartupPath + "\\ResOut\\" + file); // bitmap.Save(System.Windows.Forms.Application.StartupPath + "\\ResOut\\" + file);
//} //}
//return; sw.Stop();
Console.WriteLine("时间花费" + sw.ElapsedMilliseconds.ToString());
eyemImageFree(ref templ);
eyemImageFree(ref search);
return;
//flag = eyemNormalize(ref image); //flag = eyemNormalize(ref image);
...@@ -1170,14 +1199,14 @@ namespace eyemLib_Sharp ...@@ -1170,14 +1199,14 @@ namespace eyemLib_Sharp
//flag = eyemShockFilter(image, 9, 1.5, 0.5, 10, out tpDstImg); //flag = eyemShockFilter(image, 9, 1.5, 0.5, 10, out tpDstImg);
flag = eyemNonLocalMeansFilter(image, 7, 21, 3.0, -1); //flag = eyemNonLocalMeansFilter(image, 7, 21, 3.0, -1);
Bitmap bitmap = eyemCvtToBitmap(tpDstImg); //Bitmap bitmap = eyemCvtToBitmap(tpDstImg);
if (bitmap != null) //if (bitmap != null)
{ //{
bitmap.Save(System.Windows.Forms.Application.StartupPath + "\\ResOut\\" + file); // bitmap.Save(System.Windows.Forms.Application.StartupPath + "\\ResOut\\" + file);
} //}
return; //return;
#region Test Blob #region Test Blob
//sw.Restart(); //sw.Restart();
......
...@@ -21,6 +21,7 @@ namespace eyemLib_Sharp ...@@ -21,6 +21,7 @@ namespace eyemLib_Sharp
foreach (var item in fileNames) foreach (var item in fileNames)
{ {
EyemLib.eyemReadImageTool(item); EyemLib.eyemReadImageTool(item);
break;
} }
EyemLib.Free(); EyemLib.Free();
Console.Write("请按任意键继续。。。"); Console.Write("请按任意键继续。。。");
......
...@@ -701,6 +701,23 @@ extern "C" { ...@@ -701,6 +701,23 @@ extern "C" {
} }
#endif #endif
//////////////////////////////////////////////////////////////////////////////////////////////
// 模板匹配(eyemMatchShapes.cpp)
//
#ifdef __cplusplus
extern "C" {
#endif
// 函数接口
EXPORTS int eyemMakeShapeModel(EyemImage tpImage, double dContrast, double dMinContrast, int iApertureSize = 3, bool bL2gradient = false);
EXPORTS int eyemFindShapeModel(EyemImage tpImage, int iNumLevels, double dAngleStart, double dAngleExtent, double dAngleStep, double dMinContrast, double dContrast, double dGreediness, double dMinScore, bool bOptimization = true);
#ifdef __cplusplus
}
#endif
////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////
// 点云匹配(eyemMatch.cpp) // 点云匹配(eyemMatch.cpp)
......
#include "eyemMatchShapes.h" #include "eyemMatchShapes.h"
shape_based_matching::~shape_based_matching(void) {}
shape_based_matching::~shape_based_matching(void) int shape_based_matching::create_shape_model(cv::Mat tpTemplate, double maxContrast, double minContrast, double apertureSize, bool L2gradient)
{ {
delete[] resultPoints; int incn = tpTemplate.channels();
delete[] edgeMagnitude; if (incn != 1) {
delete[] edgeDerivativeX; cv::cvtColor(tpTemplate, tpTemplate, cv::COLOR_BGR2GRAY);
delete[] edgeDerivativeY; }
} //获取边缘
cv::Mat edges;
int shape_based_matching::create_shape_model(cv::Mat tpTemplate, double maxContrast, double minContrast) cv::Canny(tpTemplate, edges, minContrast, maxContrast, (int)apertureSize, L2gradient);
{
cv::Mat gx;
cv::Mat gy;
cv::Mat nmsEdges;
cv::Size Ssize;
cv::Mat src(tpTemplate); //Above step replicated
// set width and height
Ssize.width = src.cols; //src->width;
Ssize.height = src.rows; //src->height;
modelHeight = src.rows; //src->height; //Save Template height
modelWidth = src.cols; //src->width; //Save Template width
noOfCordinates = 0; //initialize
resultPoints = new cv::Point[modelWidth * modelHeight]; //Allocate memory for coorinates of selected points in template image
edgeMagnitude = new double[modelWidth * modelHeight]; //Allocate memory for edge magnitude for selected points
edgeDerivativeX = new double[modelWidth * modelHeight]; //Allocate memory for edge X derivative for selected points
edgeDerivativeY = new double[modelWidth * modelHeight]; ////Allocate memory for edge Y derivative for selected points
// Calculate gradient of Template
gx = cv::Mat(Ssize.height, Ssize.width, CV_16SC1); //create Matrix to store X derivative
gy = cv::Mat(Ssize.height, Ssize.width, CV_16SC1); //create Matrix to store Y derivative
Sobel(src, gx, CV_16S, 1, 0, 3); //gradient in X direction
Sobel(src, gy, CV_16S, 0, 1, 3); //gradient in Y direction
std::cout << "Sobel Done" << std::endl;
nmsEdges = cv::Mat(Ssize.height, Ssize.width, CV_32F); //create Matrix to store Final nmsEdges
const short* _sdx;
const short* _sdy;
double fdx, fdy;
double MagG, DirG;
double MaxGradient = -99999.99;
double direction;
int *orients = new int[Ssize.height * Ssize.width];
// count variable;
int count = 0,
i,
j;
double **magMat;
create_double_matrix(magMat, Ssize);
std::cout << "Create Double Mat Done" << std::endl;
for (i = 1; i < Ssize.height - 1; i++)
{
for (j = 1; j < Ssize.width - 1; j++)
{
fdx = gx.at<short>(i, j);
fdy = gy.at<short>(i, j);
MagG = sqrt((float)(fdx * fdx) + (float)(fdy * fdy)); //Magnitude = Sqrt(gx^2 +gy^2)
direction = atan2f((float)fdy, (float)fdx);//cvFastArctan((float)fdy,(float)fdx); //Direction = invtan (Gy / Gx)
magMat[i][j] = MagG;
if (MagG > MaxGradient) auto contours = std::vector<std::vector<cv::Point>>();
MaxGradient = MagG; // get maximum gradient value for normalizing. cv::findContours(edges, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE);
// get closest angle from 0, 45, 90, 135 set //计算偏导
if ((direction > 0 && direction < 22.5) || (direction > 157.5 && direction < 202.5) || (direction > 337.5 && direction < 360)) cv::Mat gx, gy;
direction = 0; cv::Sobel(tpTemplate, gx, CV_64F, 1, 0, 3);
else if ((direction > 22.5 && direction < 67.5) || (direction > 202.5 && direction < 247.5)) cv::Sobel(tpTemplate, gy, CV_64F, 0, 1, 3);
direction = 45;
else if ((direction > 67.5 && direction < 112.5) || (direction > 247.5 && direction < 292.5))
direction = 90;
else if ((direction > 112.5 && direction < 157.5) || (direction > 292.5 && direction < 337.5))
direction = 135;
else
direction = 0;
orients[count] = (int)direction; //计算梯度以及方向
count++; cv::Mat magnitude, direction;
} cv::cartToPolar(gx, gy, magnitude, direction);
}
std::cout << "Ist fdx fdy Done" << std::endl;
count = 0; // init count
// non maximum suppression
double leftPixel, rightPixel;
for (i = 1; i < Ssize.height - 1; i++) auto sum = cv::Point2d(0, 0);
{ for (int i = 0; i < contours.size(); i++)
for (j = 1; j < Ssize.width - 1; j++)
{ {
switch (orients[count]) for (int j = 0; j < contours[i].size(); j++)
{ {
case 0: auto cur = contours[i][j];
leftPixel = magMat[i][j - 1]; auto fdx = gx.ptr<double>(cur.y)[cur.x];
rightPixel = magMat[i][j + 1]; auto fdy = gy.ptr<double>(cur.y)[cur.x];
break; auto der = cv::Point2d(fdx, fdy);
case 45: auto mag = magnitude.ptr<double>(cur.y)[cur.x];
leftPixel = magMat[i - 1][j + 1]; auto dir = direction.ptr<double>(cur.y)[cur.x];
rightPixel = magMat[i + 1][j - 1]; results.push_back(PointInfo(cur, der, mag, dir));
break; sum.x += cur.x; sum.y += cur.y;
case 90:
leftPixel = magMat[i - 1][j];
rightPixel = magMat[i + 1][j];
break;
case 135:
leftPixel = magMat[i - 1][j - 1];
rightPixel = magMat[i + 1][j + 1];
break;
} }
// compare current pixels value with adjacent pixels
if ((magMat[i][j] < leftPixel) || (magMat[i][j] < rightPixel))
nmsEdges.at<float>(i, j) = 0.0f;//(nmsEdges->data.ptr + nmsEdges->step*i)[j]=0;
else
nmsEdges.at<float>(i, j) = (uchar)(magMat[i][j] / MaxGradient * 255);//(nmsEdges->data.ptr + nmsEdges->step*i)[j]=(uchar)(magMat[i][j]/MaxGradient*255);
count++;
} }
//模板尺寸
templSize = cv::Size(tpTemplate.cols, tpTemplate.rows);
//中心
auto Center = cv::Point2d(sum.x / results.size(), sum.y / results.size());
//更新位置
for (auto&item : results) {
item.update(Center);
} }
std::cout << "Non max Done" << std::endl; return FUNC_OK;
}
int RSum = 0, CSum = 0;
int curX, curY;
int flag = 1;
// double shape_based_matching::find_shape_model(cv::Mat tpImage, double minScore, double greediness, cv::Point& resultPoint)
cv::Mat cc; {
cv::cvtColor(tpTemplate, cc, cv::COLOR_GRAY2BGR); int incn = tpImage.channels();
//Hysterisis threshold if (incn != 1) {
for (i = 1; i < Ssize.height - 1; i++) cv::cvtColor(tpImage, tpImage, cv::COLOR_BGR2GRAY);
{ }
for (j = 1; j < Ssize.width; j++) //尺寸信息
const int X = tpImage.cols, Y = tpImage.rows;
//用于显示
cv::Mat showMat;
cv::cvtColor(tpImage, showMat, cv::COLOR_GRAY2BGR);
//计算偏导
cv::Mat gx, gy;
cv::Sobel(tpImage, gx, CV_64F, 1, 0, 3);
cv::Sobel(tpImage, gy, CV_64F, 0, 1, 3);
//计算梯度以及方向
cv::Mat magnitude, direction;
cv::cartToPolar(gx, gy, magnitude, direction);
cv::Mat scoreMap(cv::Size(tpImage.cols, tpImage.rows), CV_64FC1, cv::Scalar(0));
//基于ncc匹配
long noOfCordinates = static_cast<int>(results.size());
double normMinScore = minScore / noOfCordinates;
double normGreediness = (1 - greediness*minScore) / ((1.0 - greediness) / noOfCordinates);
double partialScore = .0, resultScore = .0;
for (int i = 0; i < tpImage.rows; i++) {
for (int j = 0; j < tpImage.cols; j++) {
double partialSum = .0;
for (int m = 0; m < noOfCordinates; m++)
{ {
//_sdx = (short*)(gx->data.ptr + gx->step*i); auto item = results[m];
//_sdy = (short*)(gy->data.ptr + gy->step*i); auto curX = (int)(j + item.Offset.x);
//fdx = _sdx[j]; fdy = _sdy[j]; auto curY = (int)(i + item.Offset.y);
auto iTx = item.Derivative.x;
//std::cout << gx.elemSize1(); auto iTy = item.Derivative.y;
fdx = gx.at<short>(i, j); if (curX < 0 || curY < 0 || curY > tpImage.rows - 1 || curX > tpImage.cols - 1)
fdy = gy.at<short>(i, j); continue;
MagG = sqrt(fdx * fdx + fdy * fdy); //Magnitude = Sqrt(gx^2 +gy^2) auto iSx = gx.ptr<double>(curY)[curX];
DirG = atan2f((float)fdy, (float)fdx); //cvFastArctan((float)fdy, (float)fdx); //Direction = tan(y/x) auto iSy = gy.ptr<double>(curY)[curX];
////((uchar*)(imgGDir->imageData + imgGDir->widthStep*i))[j]= MagG; if ((iSx != 0 || iSy != 0) && (iTx != 0 || iTy != 0))
flag = 1;
double val = nmsEdges.at<float>(i, j);
//std::cout << "nmsEdge float Done" << std::endl;
//if(((double)((nmsEdges->data.ptr + nmsEdges->step*i))[j]) < maxContrast)
if (val < maxContrast)
{
//if(((double)((nmsEdges->data.ptr + nmsEdges->step*i))[j])< minContrast)
if (val < minContrast)
{ {
auto mag = magnitude.ptr<double>(curY)[curX];
auto matGradMag = (mag == 0) ? 0 : 1.0 / mag;
partialSum += ((iSx * iTx) + (iSy * iTy)) * (item.Magnitude * matGradMag);
auto sumOfCoords = m + 1;
partialScore = partialSum / sumOfCoords;
//(nmsEdges->data.ptr + nmsEdges->step*i)[j] = 0; if (partialScore < MIN((minScore - 1) + normGreediness * sumOfCoords, normMinScore * sumOfCoords))
nmsEdges.at<float>(i, j) = 0; break;
flag = 0; // remove from edge
////((uchar*)(imgGDir->imageData + imgGDir->widthStep*i))[j]=0;
} }
else
{
if ((nmsEdges.at<float>(i - 1, j - 1) < maxContrast) &&
(nmsEdges.at<float>(i - 1, j) < maxContrast) &&
(nmsEdges.at<float>(i - 1, j + 1) < maxContrast) &&
(nmsEdges.at<float>(i, j - 1) < maxContrast) &&
(nmsEdges.at<float>(i, j + 1) < maxContrast) &&
(nmsEdges.at<float>(i + 1, j - 1) < maxContrast) &&
(nmsEdges.at<float>(i + 1, j) < maxContrast) &&
(nmsEdges.at<float>(i + 1, j + 1) < maxContrast)
)
{
nmsEdges.at<float>(i, j) = 0;
flag = 0;
} }
scoreMap.ptr<double>(i)[j] = partialScore;
} }
} }
//归一化
// save selected edge information cv::normalize(scoreMap, scoreMap, 1.0, 0, cv::NORM_MINMAX);
curX = i; curY = j; //计算结果非极大值抑制处理
if (flag != 0) std::vector<int> indices;
std::vector<cv::Rect> bboxes; std::vector<float> scoreIndex;
for (int y = 0; y < Y; y++)
{ {
if (fdx != 0 || fdy != 0) for (int x = 0; x < X; x++)
{ {
// Row sum and column sum for center of gravity if (scoreMap.ptr<double>(y)[x]>minScore) {
RSum = RSum + curX; bboxes.push_back(cv::Rect(x - templSize.width / 2, y - templSize.height / 2, templSize.width, templSize.height));
CSum = CSum + curY; scoreIndex.push_back((float)scoreMap.ptr<double>(y)[x]);
resultPoints[noOfCordinates].x = curX;
resultPoints[noOfCordinates].y = curY;
edgeDerivativeX[noOfCordinates] = fdx;
edgeDerivativeY[noOfCordinates] = fdy;
//handle divide by zero
if (MagG != 0)
edgeMagnitude[noOfCordinates] = 1 / MagG; // gradient magnitude
else
edgeMagnitude[noOfCordinates] = 0;
noOfCordinates++;
}
} }
} }
} }
centerOfGravity.x = RSum / noOfCordinates; // center of gravity cv::dnn::NMSBoxes(bboxes, scoreIndex, (float)minScore, 0.25f, indices);
centerOfGravity.y = CSum / noOfCordinates; // center of gravity
// change coordinates to reflect center of gravity for (int i = 0; i < indices.size(); i++) {
for (int m = 0; m < noOfCordinates; m++) resultPoint = (bboxes[indices[i]].tl() + bboxes[indices[i]].br()) / 2;
//画图显示
auto contours = std::vector<cv::Point>();
for (int i = 0; i < results.size(); i++)
{ {
int temp; contours.push_back(results[i].Offset + cv::Point2d(resultPoint));
//cc.at<cv::Vec3b>(resultPoints[m].x, resultPoints[m].y) = cv::Vec3b(0, 0, 255); }
temp = resultPoints[m].x; draw_match_shapes(showMat, resultPoint, cv::Scalar(151, 243, 121), 1);
resultPoints[m].x = temp - centerOfGravity.x;
temp = resultPoints[m].y;
resultPoints[m].y = temp - centerOfGravity.y;
} }
// free alocated memories cv::imwrite("result.png", showMat);
delete[] orients; return 0;
////cvReleaseImage(&imgGDir);
gx.release();
gy.release();
nmsEdges.release();
release_double_matrix(magMat, Ssize.height);
modelDefined = true;
return FUNC_OK;
} }
double shape_based_matching::find_shape_model(cv::Mat tpImage, double minScore, double greediness, cv::Point* resultPoint) void shape_based_matching::draw_match_shapes(cv::Mat &showMat, cv::Point cog, cv::Scalar color, int linew)
{ {
cv::Mat Sdx, Sdy; //绘图显示
cv::circle(showMat, cog, 2, cv::Scalar(0, 0, 255), -1);
double resultScore = 0; auto contours = std::vector<cv::Point>();
double partialSum = 0; for (int i = 0; i < results.size(); i++)
double sumOfCoords = 0;
double partialScore;
const short* _Sdx;
const short* _Sdy;
int i, j, m; // count variables
double iTx, iTy, iSx, iSy;
double gradMag;
int curX, curY;
double **matGradMag; //Gradient magnitude matrix
cv::Mat src(tpImage);
// source image size
cv::Size Ssize;
Ssize.width = src.cols;
Ssize.height = src.rows;
create_double_matrix(matGradMag, Ssize); // create image to save gradient magnitude values
Sdx = cv::Mat(Ssize.height, Ssize.width, CV_16SC1); // X derivatives
Sdy = cv::Mat(Ssize.height, Ssize.width, CV_16SC1); // y derivatives
Sobel(src, Sdx, CV_16S, 1, 0, 3); // find X derivatives
Sobel(src, Sdy, CV_16S, 0, 1, 3); // find Y derivatives
// stoping criterias to search for model
double normMinScore = minScore / noOfCordinates; // precompute minumum score
double normGreediness = ((1 - greediness * minScore) / (1 - greediness)) / noOfCordinates; // precompute greedniness
for (i = 0; i < Ssize.height; i++)
{
for (j = 0; j < Ssize.width; j++)
{
iSx = Sdx.at<short>(i, j);
iSy = Sdy.at<short>(i, j);
gradMag = sqrt((iSx * iSx) + (iSy * iSy)); //Magnitude = Sqrt(dx^2 +dy^2)
if (gradMag != 0) // hande divide by zero
matGradMag[i][j] = 1 / gradMag; // 1/Sqrt(dx^2 +dy^2)
else
matGradMag[i][j] = 0;
}
}
for (i = 0; i < Ssize.height; i++)
{
for (j = 0; j < Ssize.width; j++)
{
partialSum = 0; // initilize partialSum measure
for (m = 0; m < noOfCordinates; m++)
{
curX = i + resultPoints[m].x; // template X coordinate
curY = j + resultPoints[m].y; // template Y coordinate
iTx = edgeDerivativeX[m]; // template X derivative
iTy = edgeDerivativeY[m]; // template Y derivative
if (curX < 0 || curY < 0 || curX > Ssize.height - 1 || curY > Ssize.width - 1)
continue;
iSx = Sdx.at<short>(curX, curY); //CHECK IF curX AND curY NEED TO BE SWITCHED
iSy = Sdy.at<short>(curX, curY);
if ((iSx != 0 || iSy != 0) && (iTx != 0 || iTy != 0))
{
//partial Sum = Sum of(((Source X derivative* Template X drivative) + Source Y derivative * Template Y derivative)) / Edge magnitude of(Template)* edge magnitude of(Source))
partialSum = partialSum + ((iSx * iTx) + (iSy * iTy)) * (edgeMagnitude[m] * matGradMag[curX][curY]);
}
sumOfCoords = m + 1;
partialScore = partialSum / sumOfCoords;
// check termination criteria
// if partial score score is less than the score than needed to make the required score at that position
// break serching at that coordinate.
if (partialScore < (MIN((minScore - 1) + normGreediness * sumOfCoords, normMinScore * sumOfCoords)))
break;
}
if (partialScore > resultScore)
{ {
std::cout << "Partial Score is :" << partialScore << std::endl; contours.push_back(results[i].Offset + cv::Point2d(cog));
resultScore = partialScore; // Match score
resultPoint->x = i; // result coordinate X
resultPoint->y = j; // result coordinate Y
std::cout << "Result Point is :" << resultPoint->x << resultPoint->y << std::endl;
} }
for (auto&point : contours) {
showMat.at<cv::Vec3b>(point) = cv::Vec3b((uchar)color[0], (uchar)color[1], (uchar)color[2]);
} }
}
// free used resources and return score
release_double_matrix(matGradMag, Ssize.height);
Sdx.release();
Sdy.release();
return resultScore;
} }
void shape_based_matching::create_double_matrix(double** &matrix, cv::Size size) { int eyemMakeShapeModel(EyemImage tpImage, double dContrast, double dMinContrast, int iApertureSize, bool bL2gradient)
matrix = new double*[size.height]; {
for (int iInd = 0; iInd < size.height; iInd++) CV_Assert(NULL != tpImage.vpImage);
matrix[iInd] = new double[size.width];
}
cv::Mat image = cv::Mat(tpImage.iHeight, tpImage.iWidth, MAKETYPE(tpImage.iDepth, tpImage.iChannels), tpImage.vpImage).clone();
void shape_based_matching::release_double_matrix(double** &matrix, int size) { if (image.empty())
for (int i = 0; i < size; i++) return FUNC_IMAGE_NOT_EXIST;
delete[] matrix[i]; //创建模板
sbm.create_shape_model(image, dContrast, dMinContrast, iApertureSize, bL2gradient);
return FUNC_OK;
} }
void shape_based_matching::draw_match_shapes(cv::Mat showMat, cv::Point cog, cv::Scalar color, int linew) int eyemFindShapeModel(EyemImage tpImage, int iNumLevels, double dAngleStart, double dAngleExtent, double dAngleStep, double dMinContrast, double dContrast, double dGreediness, double dMinScore, bool bOptimization)
{ {
cv::Point point; CV_Assert(NULL != tpImage.vpImage);
point.y = cog.x;
point.x = cog.y;
for (int i = 0; i < noOfCordinates; i++)
{
point.y = resultPoints[i].x + cog.x;
point.x = resultPoints[i].y + cog.y;
line(showMat, point, point, color, linew);
}
}
void shape_based_matching::draw_match_shapes(cv::Mat showMat, cv::Scalar color, int linew) cv::Mat image = cv::Mat(tpImage.iHeight, tpImage.iWidth, MAKETYPE(tpImage.iDepth, tpImage.iChannels), tpImage.vpImage).clone();
{
cv::Point point; if (image.empty())
for (int i = 0; i < noOfCordinates; i++) return FUNC_IMAGE_NOT_EXIST;
{
point.y = resultPoints[i].x + centerOfGravity.x; //匹配模板
point.x = resultPoints[i].y + centerOfGravity.y; cv::Point result;
line(showMat, point, point, color, linew); sbm.find_shape_model(image, dMinScore, dGreediness, result);
} return FUNC_OK;
} }
\ No newline at end of file \ No newline at end of file
...@@ -6,7 +6,6 @@ ...@@ -6,7 +6,6 @@
#define __EYEM_MATCHSHAPES_H #define __EYEM_MATCHSHAPES_H
#include "eyemLib.h" #include "eyemLib.h"
#include <algorithm>
class shape_based_matching class shape_based_matching
{ {
...@@ -14,29 +13,35 @@ public: ...@@ -14,29 +13,35 @@ public:
shape_based_matching() {}; shape_based_matching() {};
~shape_based_matching(void); ~shape_based_matching(void);
int create_shape_model(cv::Mat tpTemplate, double maxContrast, double minContrast); int create_shape_model(cv::Mat tpTemplate, double maxContrast, double minContrast, double apertureSize = 3, bool L2gradient = false);
double find_shape_model(cv::Mat tpImage, double minScore, double greediness, cv::Point* resultPoint); double find_shape_model(cv::Mat tpImage, double minScore, double greediness, cv::Point& resultPoint);
void draw_match_shapes(cv::Mat, cv::Point, cv::Scalar, int); void draw_match_shapes(cv::Mat&, cv::Point, cv::Scalar, int);
void draw_match_shapes(cv::Mat, cv::Scalar, int);
private: private:
int noOfCordinates; struct PointInfo
cv::Point* resultPoints; {
int modelHeight; cv::Point Point;
int modelWidth; cv::Point2d Center;
double* edgeMagnitude; cv::Point2d Offset;
double* edgeDerivativeX; cv::Point2d Derivative;
double* edgeDerivativeY; double Magnitude, Direction;
cv::Point centerOfGravity;
PointInfo() {}
bool modelDefined; PointInfo(cv::Point Point, cv::Point2d Derivative, double Magnitude, double Direction) :Point(Point), Derivative(Derivative), Magnitude(Magnitude), Direction(Direction) {}
void create_double_matrix(double** &matrix, cv::Size size); void update(cv::Point2d Center)
{
void release_double_matrix(double** &matrix, int size); this->Center = Center;
this->Offset = cv::Point2d((double)this->Point.x - Center.x, (double)this->Point.y - Center.y);
}
};
cv::Size templSize;
std::vector<PointInfo> results;
}; };
shape_based_matching sbm;
#endif/* __EYEM_MATCHSHAPES_H */ #endif/* __EYEM_MATCHSHAPES_H */
\ No newline at end of file \ No newline at end of file
...@@ -7941,7 +7941,6 @@ int eyemMulFuncTool(EyemImage tpImage, EyemRect tpRoi, const char *funcName, dou ...@@ -7941,7 +7941,6 @@ int eyemMulFuncTool(EyemImage tpImage, EyemRect tpRoi, const char *funcName, dou
return FUNC_OK; return FUNC_OK;
} }
#include "eyemMatchShapes.h"
int eyemLibImpl(EyemImage tpImage, EyemHSVModel tpHSVModel, EyemImage *tpDstImg) int eyemLibImpl(EyemImage tpImage, EyemHSVModel tpHSVModel, EyemImage *tpDstImg)
{ {
CV_Assert(NULL != tpImage.vpImage); CV_Assert(NULL != tpImage.vpImage);
...@@ -7956,109 +7955,6 @@ int eyemLibImpl(EyemImage tpImage, EyemHSVModel tpHSVModel, EyemImage *tpDstImg) ...@@ -7956,109 +7955,6 @@ int eyemLibImpl(EyemImage tpImage, EyemHSVModel tpHSVModel, EyemImage *tpDstImg)
(tpHSVModel.dpRangeUExt[0] + tpHSVModel.dpRangeUExt[1] + tpHSVModel.dpRangeUExt[2]) != 0) { (tpHSVModel.dpRangeUExt[0] + tpHSVModel.dpRangeUExt[1] + tpHSVModel.dpRangeUExt[2]) != 0) {
std::cout << "红色" << std::endl; std::cout << "红色" << std::endl;
} }
return FUNC_OK;
//shape_based_matching GM; // object to implent geometric matching
//int lowThreshold = 10; //deafult value
//int highThreashold = 100; //deafult value
//double minScore = 0.25; //deafult value
//double greediness = 0.8; //deafult value
//double total_time = 0;
//double score = 0;
//cv::Point result;
//cv::Mat templateImage = cv::imread("D://批量测试图像//template2.png", cv::IMREAD_GRAYSCALE);
//if (templateImage.data == NULL)
//{
// return 0;
//}
////Load Search Image
//cv::Mat searchImage = cv::imread("D://批量测试图像//rings_01.png", cv::IMREAD_GRAYSCALE);
//if (searchImage.data == NULL)
//{
// return 0;
//}
//lowThreshold = atoi("10");
//highThreashold = atoi("100");//get high threshold
//minScore = atof("0.25");
//greediness = atof("0.9");
//cv::Mat grayTemplateImg; //(templateImage.size(), CV_8U, 1);
//templateImage.copyTo(grayTemplateImg);
//if (GM.create_shape_model(grayTemplateImg, lowThreshold, highThreashold) != FUNC_OK)
//{
// std::cout << "ERROR: could not create model...";
// return 0;
//}
//else {
// cv::Mat cc;
// cv::cvtColor(templateImage, cc, cv::COLOR_GRAY2BGR);
//}
////测试用833
////测试用
//cv::Mat cc;
//cv::cvtColor(templateImage, cc, cv::COLOR_GRAY2BGR);
//for (int i = 0; i < 833; i++)
//{
// //cc.ptr<cv::Vec3b>(resultPoints)[resultPoints[i].y] = cv::Vec3b(0, 0, 255);
//}
////CvSize searchSize = cvSize(searchImage->width, searchImage->height);
//cv::Mat graySearchImg; //= cvCreateImage(searchSize, IPL_DEPTH_8U, 1);
// // Convert color image to gray image.
//searchImage.copyTo(graySearchImg);
//clock_t start_time1 = clock();
//score = GM.find_shape_model(graySearchImg, minScore, greediness, &result);
//clock_t finish_time1 = clock();
//total_time = ((double)finish_time1 - (double)start_time1) / CLOCKS_PER_SEC;
//cv::Mat rgb;
//if (score > minScore) // if score is atleast 0.4
//{
// std::cout << " Found at [" << result.x << ", " << result.y << "]\n Score = " << score << "\n Searching Time = " << total_time * 1000 << "ms";
// cvtColor(searchImage, rgb, cv::COLOR_GRAY2BGR);
// GM.draw_match_shapes(rgb, result, cv::Scalar(0, 255, 0), 1);
//}
//else
// std::cout << " Object Not found";
////////////////////////////////////
//std::cout << "\n ------------------------------------\n\n";
//std::cout << "\n Press any key to exit!";
////Display result
//cv::Mat dispTemplate;
//cvtColor(templateImage, dispTemplate, cv::COLOR_GRAY2BGR);
//GM.draw_match_shapes(dispTemplate, CV_RGB(255, 0, 0), 1);
//cv::namedWindow("Template", cv::WINDOW_AUTOSIZE);
//cv::imshow("Template", dispTemplate);
//cv::namedWindow("Search Image", cv::WINDOW_AUTOSIZE);
//imshow("Search Image", rgb);
//cv::waitKey(0);
//cv::destroyWindow("Search Image");
//cv::destroyWindow("Template");
//return 0;
#pragma region resize img #pragma region resize img
//const int minInputSize = 832; //const int minInputSize = 832;
......
...@@ -387,10 +387,16 @@ int eyemNonLocalMeansFilter(EyemImage tpImage, int iCMPSize, int iSearchSize, do ...@@ -387,10 +387,16 @@ int eyemNonLocalMeansFilter(EyemImage tpImage, int iCMPSize, int iSearchSize, do
//mvs[2] = cv::imread("C:\\Users\\nzslw\\OneDrive\\程序\\VSProject\\eyemLib\\x64\\Debug\\Portada_paper_r.png", 0); //mvs[2] = cv::imread("C:\\Users\\nzslw\\OneDrive\\程序\\VSProject\\eyemLib\\x64\\Debug\\Portada_paper_r.png", 0);
cv::Mat dest; cv::Mat dest;
//cv::blur(image, dest, cv::Size(3, 3));
//cv::merge(mvs, dest); //cv::merge(mvs, dest);
nonLocalMeansFilter_SSE(image, dest, cv::Size(3, 3), cv::Size(5, 5), 21.0, -1, 0); //for (int i = 0; i < 10; i++)
//{
//nonLocalMeansFilter_SSE(image, dest, cv::Size(3, 3), cv::Size(5, 5), 10.0, -1, 0);
//image = dest;
//}
cv::imwrite("Portada_paper5.png", dest);
//cv::imwrite("Portada_paper5.png", dest);
return FUNC_OK; return FUNC_OK;
} }
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!