Skip to content
This repository has been archived by the owner on Jan 4, 2024. It is now read-only.

Commit

Permalink
add find red point
Browse files Browse the repository at this point in the history
  • Loading branch information
Neutree committed Aug 4, 2023
1 parent 6e21628 commit 6d57869
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion examples/camera_opencv/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ This is an example of using OpenCV with libmaix.

by default code use opencv to get balck rectangle line, like this:

![black rectangle](./assets/find_rectangle.jpg)
![black rectangle](./assets/find_rectangle.jpg) ![black rectangle2](./assets/m2dock_opencv_find_rectangle_red_point.jpg)

## Build

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions examples/camera_opencv/main/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,35 @@ void opencv_ops(cv::Mat &rgb)
// 从 poly 找到凸出的点
std::vector<cv::Point> hull;
cv::convexHull(poly, hull);


// find red point on rgb image
cv::Mat hsv;
cv::cvtColor(rgb, hsv, cv::COLOR_RGB2HSV);
cv::Mat mask, mask2;
cv::inRange(hsv, cv::Scalar(0, 43, 46), cv::Scalar(10, 255, 255), mask);
cv::inRange(hsv, cv::Scalar(156, 43, 46), cv::Scalar(180, 255, 255), mask2);
mask = mask | mask2;
cv::Mat kernel = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3, 3));
cv::morphologyEx(mask, mask, cv::MORPH_OPEN, kernel);
// find biggest contour on mask
std::vector<std::vector<cv::Point>> contours2;
std::vector<cv::Vec4i> hierarchy2;
cv::findContours(mask, contours2, hierarchy2, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE, cv::Point(0, 0));
// get the largets contour
int largest_area2 = 0;
int largest_contour_index2 = 0;
for (int i = 0; i < contours2.size(); i++)
{
double area = cv::contourArea(contours2[i]);
if (area > largest_area2)
{
largest_area2 = area;
largest_contour_index2 = i;
}
}

// draw points
// if(hull.size() == 4) // 只在有4个点的时候显示
{
// 在 rgb 图上画出凸包
Expand All @@ -179,6 +208,14 @@ void opencv_ops(cv::Mat &rgb)
cv::circle(rgb, hull[i], 5, cv::Scalar(0, 0, 255), 2);
}
}

// get the center point of the largest contour
if(contours2.size() > 0)
{
cv::Moments mu = cv::moments(contours2[largest_contour_index2], false);
cv::Point2f mc = cv::Point2f(mu.m10 / mu.m00, mu.m01 / mu.m00);
cv::circle(rgb, mc, 5, cv::Scalar(255, 0, 255), 2);
}
}

#ifdef CONFIG_IMLIB_ENABLE
Expand Down

0 comments on commit 6d57869

Please sign in to comment.