Skip to content

Commit

Permalink
Adds 'find_template' pattern recognition method that uses an image te…
Browse files Browse the repository at this point in the history
…mplate to find a pattern in an input image
  • Loading branch information
cfangmeier committed Apr 24, 2023
1 parent 06d0b84 commit 3c248c4
Show file tree
Hide file tree
Showing 20 changed files with 216 additions and 4 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
135 changes: 135 additions & 0 deletions Gantry/External/.idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Gantry/External/Vision/test_Vision.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <iostream>
#include "camera.h"
#include "../Camera/capture.h"

using namespace std;

Expand Down
54 changes: 54 additions & 0 deletions Gantry/External/Vision/vision.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,60 @@ __declspec(dllexport) int __cdecl find_circles(
return 0;
}

__declspec(dllexport) int __cdecl find_template(
char *imgPtr,
int imgLineWidth,
int imgWidth,
int imgHeight,
char *templatePtr,
int templateLineWidth,
int templateWidth,
int templateHeight,
float fieldOfViewX, // for img, mm
float fieldOfViewY, // for img, mm
int templateBlurFactor, // pixels
float minConcurrence,
int *numMatch,
float *matchX,
float *matchY,
float *matchConcurrence
) {
log("Running find_template");
std::stringstream ss;

cv::Mat img(imgHeight, imgWidth, CV_8U, (void *) imgPtr, imgLineWidth);
cv::Mat template_(templateHeight, templateWidth, CV_8U, (void *) templatePtr, templateLineWidth);

cv::Mat template_blurred;
if (templateBlurFactor > 0) {
cv::GaussianBlur(template_, template_blurred, cv::Size(0, 0), templateBlurFactor);
} else {
template_blurred = template_;
}

cv::Mat out;
cv::matchTemplate(img, template_, out, cv::TM_CCOEFF_NORMED);
show(out);

double minVal, maxVal;
cv::Point2i minLoc, maxLoc;
cv::minMaxLoc(out, &minVal, &maxVal, &minLoc, &maxLoc);
if (maxVal < minConcurrence) {
*numMatch = 0;
return -1; // No match
}
float x_px = (maxLoc.x + 0.5*templateWidth) - 0.5*imgWidth;
float y_px = (maxLoc.y + 0.5*templateHeight) - 0.5*imgHeight;
float x_mm = x_px * (fieldOfViewX / imgWidth);
float y_mm = y_px * (fieldOfViewY / imgHeight);

*numMatch = 1;
*matchX = x_mm;
*matchY = y_mm;
*matchConcurrence = (float)maxVal;

return 0;
}

__declspec(dllexport) int __cdecl find_rects(
char *imgPtr,
Expand Down
18 changes: 18 additions & 0 deletions Gantry/External/Vision/vision.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,24 @@ DLLExport int __cdecl find_circles(
float *circleRadii);


DLLExport int __cdecl find_template(
char *imgPtr,
int imgLineWidth,
int imgWidth,
int imgHeight,
char *templatePtr,
int templateLineWidth,
int templateWidth,
int templateHeight,
float fieldOfViewX, // for img, mm
float fieldOfViewY, // for img, mm
int templateBlurFactor, // pixels
float minConcurrence,
int *numMatch,
float *matchX,
float *matchY,
float *matchConcurrence);

DLLExport int __cdecl find_rects(
char *imgPtr,
int imgLineWidth,
Expand Down
3 changes: 3 additions & 0 deletions Gantry/Gantry.lvproj
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,9 @@
<Item Name="XControlSupport.lvlib" Type="Library" URL="/&lt;vilib&gt;/_xctls/XControlSupport.lvlib"/>
</Item>
<Item Name="Aerotech.A3200.dll" Type="Document" URL="../../../../../../../Program Files/National Instruments/LabVIEW 2017/user.lib/aerotech/2010/Bin/Aerotech.A3200.dll"/>
<Item Name="kernel32.dll" Type="Document" URL="kernel32.dll">
<Property Name="NI.PreserveRelativePath" Type="Bool">true</Property>
</Item>
<Item Name="lvanlys.dll" Type="Document" URL="/&lt;resource&gt;/lvanlys.dll"/>
<Item Name="mscorlib" Type="VI" URL="mscorlib">
<Property Name="NI.PreserveRelativePath" Type="Bool">true</Property>
Expand Down
Binary file modified Gantry/Shared Components/FlexWorktable/Fiducial Parameters.vi
Binary file not shown.
Binary file modified Gantry/Shared Components/Gantry Class/Find Fiducials.vi
Binary file not shown.
Binary file modified Gantry/Shared Components/Vision/Find Fiducial.vi
Binary file not shown.
2 changes: 2 additions & 0 deletions Gantry/Shared Components/Vision/Vision.lvlib
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
<Item Name="find_circles parameters.ctl" Type="VI" URL="../find_circles parameters.ctl"/>
<Item Name="find_patches parameters.ctl" Type="VI" URL="../find_patches parameters.ctl"/>
<Item Name="find_patches.vi" Type="VI" URL="../find_patches.vi"/>
<Item Name="find_template.vi" Type="VI" URL="../find_template.vi"/>
<Item Name="find_template parameters.ctl" Type="VI" URL="../find_template parameters.ctl"/>
</Item>
<Item Name="image manipulation" Type="Folder">
<Item Name="fill.vi" Type="VI" URL="../fill.vi"/>
Expand Down
Binary file not shown.
Binary file added Gantry/Shared Components/Vision/find_template.vi
Binary file not shown.
Binary file modified Gantry/Shared Components/Vision/grab image.vi
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion Gantry/Shared Components/bin/Camera.dll
Git LFS file not shown
4 changes: 2 additions & 2 deletions Gantry/Shared Components/bin/Vision.dll
Git LFS file not shown
Binary file modified Gantry/gScript Application/Main gScript UI.vi
Binary file not shown.
Binary file not shown.
Binary file modified Gantry/gScript Application/gScript Interpreter/CMD_DIALOG.vi
Binary file not shown.
Binary file modified Gantry/gScript Application/gScript Interpreter/CMD_FINDFID.vi
Binary file not shown.

0 comments on commit 3c248c4

Please sign in to comment.