Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve finding the origin of a modified circle grid #104

Merged
merged 3 commits into from
May 13, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 64 additions & 13 deletions rct_image_tools/src/rct_image_tools/modified_circle_grid_finder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ static std::vector<cv::Point2d> extractKeyPoints(const cv::Mat& image, const std
std::vector<cv::KeyPoint> keypoints;
cv::Point large_point;

std::size_t start_first_row = 0;
std::size_t end_first_row = cols - 1;
std::size_t start_last_row = rows * cols - cols;
std::size_t end_last_row = rows * cols - 1;

for (double alpha = 1.0; alpha <= 3.0; alpha += 0.01)
{
bool found = false;
Expand Down Expand Up @@ -106,38 +101,94 @@ static std::vector<cv::Point2d> extractKeyPoints(const cv::Mat& image, const std
std::size_t temp_rows = flipped ? cols : rows;
std::size_t temp_cols = flipped ? rows : cols;

std::size_t start_first_row = 0;
std::size_t end_first_row = cols - 1;
std::size_t start_last_row = rows * cols - cols;
std::size_t end_last_row = rows * cols - 1;

// Determine which circle is the largest
double start_first_row_size = -1.0;
double start_last_row_size = -1.0;
double end_first_row_size = -1.0;
double end_last_row_size = -1.0;

// Determine size of corners relative to their adjacent circles
double start_first_row_avg_rel_size = 0;
double start_last_row_avg_rel_size = 0;
double end_first_row_avg_rel_size = 0;
double end_last_row_avg_rel_size = 0;
marrts marked this conversation as resolved.
Show resolved Hide resolved

cv::Point2d start_last_row_pt = centers[start_last_row];
cv::Point2d end_last_row_pt = centers[end_last_row];
cv::Point2d start_first_row_pt = centers[start_first_row];
cv::Point2d end_first_row_pt = centers[end_first_row];
marrts marked this conversation as resolved.
Show resolved Hide resolved

// Get the size of every circle
std::vector<double> center_sizes(centers.size(), 1.0);
marip8 marked this conversation as resolved.
Show resolved Hide resolved
marip8 marked this conversation as resolved.
Show resolved Hide resolved

for (std::size_t i = 0; i < keypoints.size(); i++)
{
double x = keypoints[i].pt.x;
double y = keypoints[i].pt.y;
double ksize = keypoints[i].size;

for (std::size_t j = 0; j < centers.size(); j++)
{
auto center = centers[j];
marrts marked this conversation as resolved.
Show resolved Hide resolved
if (center.x == x && center.y == y)
marrts marked this conversation as resolved.
Show resolved Hide resolved
{
center_sizes[j] = ksize;
break;
marip8 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

// Iterate over every center, when it is a corner then compare its size to adjacent circles
for (std::size_t i = 0; i < centers.size(); i++)
{
double x = centers[i].x;
double y = centers[i].y;
double ksize = center_sizes[i];

if (x == start_last_row_pt.x && y == start_last_row_pt.y)
{
start_last_row_size = ksize;
if (i + 1 < center_sizes.size() && i - cols < center_sizes.size())
{
double start_last_row_rel_row_size = start_last_row_size / center_sizes[i+1];
double start_last_row_rel_col_size = start_last_row_size / center_sizes[i-cols];
start_last_row_avg_rel_size = (start_last_row_rel_row_size + start_last_row_rel_col_size)/2.0;
}
}
if (x == end_last_row_pt.x && y == end_last_row_pt.y)
{
end_last_row_size = ksize;
if (i - 1 < center_sizes.size() && i - cols < center_sizes.size())
{
double end_last_row_rel_row_size = end_last_row_size / center_sizes[i-1];
double end_last_row_rel_col_size = end_last_row_size / center_sizes[i-cols];
end_last_row_avg_rel_size = (end_last_row_rel_row_size + end_last_row_rel_col_size)/2.0;
}
}
if (x == start_first_row_pt.x && y == start_first_row_pt.y)
{
start_first_row_size = ksize;
if (i + 1 < center_sizes.size() && i + cols < center_sizes.size())
{
double start_first_row_rel_row_size = start_first_row_size / center_sizes[i+1];
double start_first_row_rel_col_size = start_first_row_size / center_sizes[i+cols];
start_first_row_avg_rel_size = (start_first_row_rel_row_size + start_first_row_rel_col_size)/2.0;
}
}
if (x == end_first_row_pt.x && y == end_first_row_pt.y)
{
end_first_row_size = ksize;
if (i - 1 < center_sizes.size() && i + cols < center_sizes.size())
{
double end_first_row_rel_row_size = end_first_row_size / center_sizes[i-1];
double end_first_row_rel_col_size = end_first_row_size / center_sizes[i+cols];
end_first_row_avg_rel_size = (end_first_row_rel_row_size + end_first_row_rel_col_size)/2.0;
}
marip8 marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down Expand Up @@ -178,8 +229,8 @@ static std::vector<cv::Point2d> extractKeyPoints(const cv::Mat& image, const std
......
o.....
*/
if (start_last_row_size > start_first_row_size && start_last_row_size > end_first_row_size &&
start_last_row_size > end_last_row_size)
if (start_last_row_avg_rel_size > start_first_row_avg_rel_size && start_last_row_avg_rel_size > end_first_row_avg_rel_size &&
start_last_row_avg_rel_size > end_last_row_avg_rel_size)
{
large_point.x = start_last_row_pt.x;
large_point.y = start_last_row_pt.y;
Expand Down Expand Up @@ -209,8 +260,8 @@ static std::vector<cv::Point2d> extractKeyPoints(const cv::Mat& image, const std
......
......
*/
else if (end_first_row_size > end_last_row_size && end_first_row_size > start_last_row_size &&
end_first_row_size > start_first_row_size)
else if (end_first_row_avg_rel_size > end_last_row_avg_rel_size && end_first_row_avg_rel_size > start_last_row_avg_rel_size &&
end_first_row_avg_rel_size > start_first_row_avg_rel_size)
{
large_point.x = end_first_row_pt.x;
large_point.y = end_first_row_pt.y;
Expand Down Expand Up @@ -240,8 +291,8 @@ static std::vector<cv::Point2d> extractKeyPoints(const cv::Mat& image, const std
......
.....o
*/
else if (end_last_row_size > start_last_row_size && end_last_row_size > end_first_row_size &&
end_last_row_size > start_first_row_size)
else if (end_last_row_avg_rel_size > start_last_row_avg_rel_size && end_last_row_avg_rel_size > end_first_row_avg_rel_size &&
end_last_row_avg_rel_size > start_first_row_avg_rel_size)
{
large_point.x = end_last_row_pt.x;
large_point.y = end_last_row_pt.y;
Expand Down Expand Up @@ -275,8 +326,8 @@ static std::vector<cv::Point2d> extractKeyPoints(const cv::Mat& image, const std
......
......
*/
else if (start_first_row_size > end_last_row_size && start_first_row_size > end_first_row_size &&
start_first_row_size > start_last_row_size)
else if (start_first_row_avg_rel_size > end_last_row_avg_rel_size && start_first_row_avg_rel_size > end_first_row_avg_rel_size &&
start_first_row_avg_rel_size > start_last_row_avg_rel_size)
{
large_point.x = start_first_row_pt.x;
large_point.y = start_first_row_pt.y;
Expand Down