Skip to content
This repository has been archived by the owner on Jul 13, 2022. It is now read-only.

Commit

Permalink
Adds corner detection to find_rects
Browse files Browse the repository at this point in the history
  • Loading branch information
cfangmeier committed Feb 23, 2021
1 parent bf70895 commit 299e3f7
Show file tree
Hide file tree
Showing 36 changed files with 116 additions and 15 deletions.
Binary file added gVisionLV/Sample Images/MH0111/fid_bl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gVisionLV/Sample Images/MH0111/fid_br.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gVisionLV/Sample Images/MH0111/fid_tl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gVisionLV/Sample Images/MH0111/fid_tr.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gVisionLV/Sample Images/MH0112/fid_bl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gVisionLV/Sample Images/MH0112/fid_br.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gVisionLV/Sample Images/MH0112/fid_tl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gVisionLV/Sample Images/MH0112/fid_tr.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gVisionLV/Sample Images/MH0113/fid_bl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gVisionLV/Sample Images/MH0113/fid_br.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gVisionLV/Sample Images/MH0113/fid_tl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gVisionLV/Sample Images/MH0113/fid_tr.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gVisionLV/Sample Images/MH0114/fid_bl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gVisionLV/Sample Images/MH0114/fid_br.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gVisionLV/Sample Images/MH0114/fid_tl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gVisionLV/Sample Images/MH0114/fid_tr.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gVisionLV/Sample Images/MH0115/fid_bl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gVisionLV/Sample Images/MH0115/fid_br.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gVisionLV/Sample Images/MH0115/fid_tl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gVisionLV/Sample Images/MH0115/fid_tr.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gVisionLV/Sample Images/TFPX_bond_pad_rot2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified gVisionLV/gVision/find_rects.vi
Binary file not shown.
Binary file modified gVisionLV/gVision/find_rects_parameters.ctl
Binary file not shown.
2 changes: 1 addition & 1 deletion gVisionLV/gVisionLV.lvlps
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[ProjectWindow_Data]
ProjectExplorer.ClassicPosition[String] = "694,1314,1343,1731"
ProjectExplorer.ClassicPosition[String] = "590,709,1239,1126"

[AB_Cache_{77228E7A-B344-462E-98A7-B727431AA691}]
Cache_ProjSymbolChksum[String] = "7dea362b3fac8e00956a4952a3d4f474"
Expand Down
Binary file modified gVisionLV/tests/find_rects_test.vi
Binary file not shown.
104 changes: 93 additions & 11 deletions gVisionSource/gVision/gVision.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,14 @@ __declspec(dllexport) int __cdecl find_rects(
int* Nrects,
float* rectXCenters,
float* rectYCenters,
float* rectXBLCorners,
float* rectYBLCorners,
float* rectXTLCorners,
float* rectYTLCorners,
float* rectXTRCorners,
float* rectYTRCorners,
float* rectXBRCorners,
float* rectYBRCorners,
float* rectWidths,
float* rectHeights,
float* rectAngles)
Expand Down Expand Up @@ -319,17 +327,45 @@ __declspec(dllexport) int __cdecl find_rects(

//PUTTING DIMENSIONS OF ALL RECTANGLES IN VECTORS
vector<cv::Point2f> rectSizes;
vector<cv::Point2f> rectCenters;
vector<vector<cv::Point2f> > rectPoints; // center, bl, tl, tr, br
vector<float> rectAngles_;
for (auto& bRect: boundRects) {
float width_mm = bRect.size.width * (fieldOfViewX / img.cols);
float height_mm = bRect.size.height * (fieldOfViewY / img.rows);
rectSizes.push_back(cv::Point2f(width_mm, height_mm));
float centerX_mm = (bRect.center.x - img.cols / 2) * pixelSize;
float centerY_mm = (bRect.center.y - img.rows / 2) * pixelSize;
rectCenters.push_back(cv::Point2f(centerX_mm, centerY_mm));
float x, y;
vector<cv::Point2f> pts;
cv::Point2f recPoints[4];
bRect.points(recPoints);

pts.push_back({ // Center
(bRect.center.x - img.cols / 2) * pixelSize,
(bRect.center.y - img.rows / 2) * pixelSize
});

pts.push_back({ // Bottom-Left Corner
(recPoints[0].x - img.cols / 2) * pixelSize,
(recPoints[0].y - img.rows / 2) * pixelSize
});

pts.push_back({ // Top-Left Corner
(recPoints[1].x - img.cols / 2) * pixelSize,
(recPoints[1].y - img.rows / 2) * pixelSize
});

pts.push_back({ // Top-Right Corner
(recPoints[2].x - img.cols / 2) * pixelSize,
(recPoints[2].y - img.rows / 2) * pixelSize
});

pts.push_back({ // Bottom-Right Corner
(recPoints[3].x - img.cols / 2) * pixelSize,
(recPoints[3].y - img.rows / 2) * pixelSize
});
rectPoints.push_back(pts);

rectAngles_.push_back(bRect.angle);
rectSizes.push_back({
bRect.size.width * pixelSize,
bRect.size.height * pixelSize
});
}


Expand All @@ -349,8 +385,12 @@ __declspec(dllexport) int __cdecl find_rects(
bool rotated = false; // Accounts for rectangle on it's side, in which case height&width are interchanged

if ((rectSizes[i].x > minWidth && rectSizes[i].x < maxWidth) && (rectSizes[i].y > minHeight && rectSizes[i].y < maxHeight)) {
cout << "not rotated" << endl;
cout << "angle: " << rectAngles_[i] << endl;
passes = true;
} else if ((rectSizes[i].x > minHeight && rectSizes[i].x < maxHeight) && (rectSizes[i].y > minWidth && rectSizes[i].y < maxWidth)) {
cout << "rotated" << endl;
cout << "angle: " << rectAngles_[i] << endl;
passes = true;
rotated = true;
}
Expand All @@ -365,19 +405,61 @@ __declspec(dllexport) int __cdecl find_rects(
}

//Copy to output arrays
*(rectXCenters + counter) = rectCenters[i].x;
*(rectYCenters + counter) = rectCenters[i].y;
ss << " x=" << rectCenters[i].x << ", y=" << rectCenters[i].y;
*(rectXCenters + counter) = rectPoints[i][0].x;
*(rectYCenters + counter) = rectPoints[i][0].y;
ss << " x=" << rectPoints[i][0].x << ", y=" << rectPoints[i][0].y;


auto label_corner = [img](cv::Point2f& pt, int col, const char* label) {
cv::Scalar color(col*60, col*60, col*60);
cv::circle(img, pt, 15, color, -1);
cv::putText(img, label, pt, cv::FONT_HERSHEY_SIMPLEX, 2.0, color, 3);
};

if (rotated) {
*(rectWidths + counter) = rectSizes[i].y;
*(rectHeights + counter) = rectSizes[i].x;
*(rectAngles + counter) = fmodf(rectAngles_[i] + 90, 180.0);

*(rectXBLCorners + counter) = rectPoints[i][2].x;
*(rectYBLCorners + counter) = rectPoints[i][2].y;
label_corner(vertices[1], 1, "BL");

*(rectXTLCorners + counter) = rectPoints[i][3].x;
*(rectYTLCorners + counter) = rectPoints[i][3].y;
label_corner(vertices[2], 2, "TL");

*(rectXTRCorners + counter) = rectPoints[i][4].x;
*(rectYTRCorners + counter) = rectPoints[i][4].y;
label_corner(vertices[3], 3, "TR");

*(rectXBRCorners + counter) = rectPoints[i][1].x;
*(rectYBRCorners + counter) = rectPoints[i][1].y;
label_corner(vertices[0], 4, "BR");

ss << ", width=" << rectSizes[i].y << ", height=" << rectSizes[i].x;
}
else {
*(rectWidths + counter) = rectSizes[i].x;
*(rectHeights + counter) = rectSizes[i].y;
*(rectAngles + counter) = rectAngles_[i];
*(rectAngles + counter) = fmodf(rectAngles_[i], 180.0);

*(rectXBLCorners + counter) = rectPoints[i][1].x;
*(rectYBLCorners + counter) = rectPoints[i][1].y;
label_corner(vertices[0], 1, "BL");

*(rectXTLCorners + counter) = rectPoints[i][2].x;
*(rectYTLCorners + counter) = rectPoints[i][2].y;
label_corner(vertices[1], 2, "TL");

*(rectXTRCorners + counter) = rectPoints[i][3].x;
*(rectYTRCorners + counter) = rectPoints[i][3].y;
label_corner(vertices[2], 3, "TR");

*(rectXBRCorners + counter) = rectPoints[i][4].x;
*(rectYBRCorners + counter) = rectPoints[i][4].y;
label_corner(vertices[3], 4, "BR");

ss << ", width=" << rectSizes[i].x << ", height=" << rectSizes[i].y;
}
log(ss);
Expand Down
8 changes: 8 additions & 0 deletions gVisionSource/gVision/gVision.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ extern "C" __declspec(dllexport) int __cdecl find_rects(
int* Nrects,
float* rectXCenters,
float* rectYCenters,
float* rectXBLCorners,
float* rectYBLCorners,
float* rectXTLCorners,
float* rectYTLCorners,
float* rectXTRCorners,
float* rectYTRCorners,
float* rectXBRCorners,
float* rectYBRCorners,
float* rectWidths,
float* rectHeights,
float* rectAngles);
17 changes: 14 additions & 3 deletions gVisionSource/gVision_tests/gVision_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void test_find_patches(bool debug, const char* logFileDir) {
}

void test_find_rects(bool debug, const char* logFileDir) {
cv::Mat img = cv::imread("..\\..\\..\\gVisionLV\\Sample Images\\TFPX_bond_pad_rot.png",
cv::Mat img = cv::imread("..\\..\\..\\gVisionLV\\Sample Images\\TFPX_bond_pad.png",
cv::IMREAD_GRAYSCALE);
if (img.empty()) {
cout << "failed to open image" << endl;
Expand All @@ -61,6 +61,14 @@ void test_find_rects(bool debug, const char* logFileDir) {
int numRects;
float rectXCenters[50];
float rectYCenters[50];
float rectXTLCorners[50];
float rectYTLCorners[50];
float rectXTRCorners[50];
float rectYTRCorners[50];
float rectXBLCorners[50];
float rectYBLCorners[50];
float rectXBRCorners[50];
float rectYBRCorners[50];
float rectWidths[50];
float rectHeights[50];
float rectAngles[50];
Expand All @@ -79,8 +87,11 @@ void test_find_rects(bool debug, const char* logFileDir) {
debug,
logFileDir,
&numRects,
rectXCenters,
rectYCenters,
rectXCenters, rectYCenters,
rectXBLCorners, rectYBLCorners,
rectXTLCorners, rectYTLCorners,
rectXTRCorners, rectYTRCorners,
rectXBRCorners, rectYBRCorners,
rectWidths,
rectHeights,
rectAngles
Expand Down

0 comments on commit 299e3f7

Please sign in to comment.