add color line detection code

This commit is contained in:
lxm 2023-06-30 10:49:29 +08:00
parent 3eea68e822
commit e358c825d3
6 changed files with 386 additions and 0 deletions

View File

@ -69,6 +69,7 @@ include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/algorithm/common_det/cuda
${CMAKE_CURRENT_SOURCE_DIR}/algorithm/landing_det/cuda
${CMAKE_CURRENT_SOURCE_DIR}/algorithm/tracking/ocv470
${CMAKE_CURRENT_SOURCE_DIR}/algorithm/color_line
${CMAKE_CURRENT_SOURCE_DIR}/video_io
${CMAKE_CURRENT_SOURCE_DIR}/algorithm/ellipse_det
${CMAKE_CURRENT_SOURCE_DIR}/utils
@ -108,6 +109,7 @@ set(
include/sv_common_det.h
include/sv_landing_det.h
include/sv_tracking.h
include/sv_color_line.h
include/sv_video_input.h
include/sv_video_output.h
include/sv_world.h
@ -148,10 +150,13 @@ set(spirecv_SRCS
algorithm/common_det/sv_common_det.cpp
algorithm/landing_det/sv_landing_det.cpp
algorithm/tracking/sv_tracking.cpp
algorithm/color_line/sv_color_line.cpp
)
file(GLOB ALG_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/algorithm/tracking/ocv470/*.cpp)
list(APPEND spirecv_SRCS ${ALG_SRC_FILES})
file(GLOB ALG_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/algorithm/color_line/*.cpp)
list(APPEND spirecv_SRCS ${ALG_SRC_FILES})
file(GLOB ALG_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/video_io/*.cpp)
list(APPEND spirecv_SRCS ${ALG_SRC_FILES})
file(GLOB ALG_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/utils/*.cpp)
@ -252,6 +257,8 @@ add_executable(LandingMarkerDetection samples/demo/landing_marker_detection.cpp)
target_link_libraries(LandingMarkerDetection sv_world)
add_executable(SingleObjectTracking samples/demo/single_object_tracking.cpp)
target_link_libraries(SingleObjectTracking sv_world)
add_executable(ColorLineDetection samples/demo/color_line_detect.cpp)
target_link_libraries(ColorLineDetection sv_world)
add_executable(UdpDetectionInfoReceiver samples/demo/udp_detection_info_receiver.cpp)
target_link_libraries(UdpDetectionInfoReceiver sv_world)
add_executable(UdpDetectionInfoSender samples/demo/udp_detection_info_sender.cpp)

View File

@ -0,0 +1,252 @@
#include "sv_color_line.h"
#include "gason.h"
#include "sv_util.h"
#include <cmath>
#include <fstream>
namespace sv {
ColorLineDetector::ColorLineDetector()
{
this->is_load_parameter = false;
}
ColorLineDetector::~ColorLineDetector()
{
}
void ColorLineDetector::_load()
{
JsonValue all_value;
JsonAllocator allocator;
_load_all_json(this->alg_params_fn, all_value, allocator);
JsonValue colorliner_params_value;
_parser_algorithm_params("ColorLineDetector", all_value, colorliner_params_value);
for (auto i : colorliner_params_value) {
if ("line_color" == std::string(i->key)) {
this->line_color = i->value.toString();
std::cout << "line_color: " << this->line_color << std::endl;
}
else if ("line_location" == std::string(i->key)) {
this->line_location = i->value.toNumber();
}
else if ("line_location_a1" == std::string(i->key)) {
this->line_location_a1 = i->value.toNumber();
}
else if ("line_location_a2" == std::string(i->key)) {
this->line_location_a2 = i->value.toNumber();
}
}
}
void ColorLineDetector::get_line_area(cv::Mat &frame, cv::Mat &line_area, cv::Mat &line_area_a1, cv::Mat &line_area_a2) {
int h = frame.rows;
half_h = h / 2.0;
half_w = frame.cols / 2.0;
int l1 = int(h * (1 - line_location - 0.05));
int l2 = int(h * (1 - line_location));
line_area = frame(cv::Range(l1, l2), cv::Range::all());
l1 = int(h * (1 - line_location_a1 - 0.05));
l2 = int(h * (1 - line_location_a1));
line_area_a1 = frame(cv::Range(l1, l2), cv::Range::all());
cy_a1 = l1;
l1 = int(h * (1 - line_location_a2 - 0.05));
l2 = int(h * (1 - line_location_a2));
cy_a2 = l1;
line_area_a2 = frame(cv::Range(l1, l2), cv::Range::all());
}
float ColorLineDetector::cnt_area(std::vector<cv::Point> cnt) {
float area = cv::contourArea(cnt);
return area;
}
void ColorLineDetector::seg(cv::Mat line_area, cv::Mat line_area_a1, cv::Mat line_area_a2, std::string _line_color, cv::Point &center, int &area, cv::Point &center_a1, cv::Point &center_a2) {
int hmin, smin, vmin, hmax, smax, vmax;
if (_line_color == "black") {
hmin = 0;
smin = 0;
vmin = 0;
hmax = 180;
smax = 255;
vmax = 46;
}
else if (_line_color == "red") {
hmin = 0;
smin = 43;
vmin = 46;
hmax = 10;
smax = 255;
vmax = 255;
}
else if (_line_color == "yellow") {
hmin = 26;
smin = 43;
vmin = 46;
hmax = 34;
smax = 255;
vmax = 255;
}
else if (_line_color == "green") {
hmin = 35;
smin = 43;
vmin = 46;
hmax = 77;
smax = 255;
vmax = 255;
}
else if (_line_color == "blue") {
hmin = 100;
smin = 43;
vmin = 46;
hmax = 124;
smax = 255;
vmax = 255;
}
else {
hmin = 0;
smin = 0;
vmin = 0;
hmax = 180;
smax = 255;
vmax = 46;
}
cv::cvtColor(line_area, line_area, cv::COLOR_BGR2HSV);
cv::inRange(line_area, cv::Scalar(hmin, smin, vmin), cv::Scalar(hmax, smax, vmax), line_area);
cv::Mat kernel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(5, 5));
cv::morphologyEx(line_area, line_area, cv::MORPH_OPEN, kernel);
std::vector<std::vector<cv::Point>> contours;
std::vector<cv::Vec4i> hierarchy;
cv::findContours(line_area, contours, hierarchy, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE);
if (contours.size() > 0) {
cv::Rect rect = cv::boundingRect(contours[0]);
int cx = rect.x + rect.width / 2;
int cy = rect.y + rect.height / 2;
std::sort(contours.begin(), contours.end(),[](const std::vector<cv::Point>& a, const std::vector<cv::Point>& b) {return cv::contourArea(a) > cv::contourArea(b);});
area = cnt_area(contours[0]);
center = cv::Point(cx, cy);
}
cv::cvtColor(line_area_a1, line_area_a1, cv::COLOR_BGR2HSV);
cv::inRange(line_area_a1, cv::Scalar(hmin, smin, vmin), cv::Scalar(hmax, smax, vmax), line_area_a1);
//cv2.MORPH_CLOSE 先进行膨胀,再进行腐蚀操作
kernel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(5, 5));
cv::morphologyEx(line_area_a1, line_area_a1, cv::MORPH_CLOSE, kernel);
std::vector<std::vector<cv::Point>> contours_a1;
cv::findContours(line_area_a1, contours_a1, hierarchy, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE);
if (contours_a1.size() > 0) {
cv::Rect rect = cv::boundingRect(contours_a1[0]);
int cx = rect.x + rect.width / 2;
int cy = rect.y + rect.height / 2 + cy_a1;
center_a1 = cv::Point(cx - half_w, cy - half_h);
}
cv::cvtColor(line_area_a2, line_area_a2, cv::COLOR_BGR2HSV);
cv::inRange(line_area_a2, cv::Scalar(hmin, smin, vmin), cv::Scalar(hmax, smax, vmax), line_area_a2);
kernel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(5, 5));
cv::morphologyEx(line_area_a2, line_area_a2, cv::MORPH_CLOSE, kernel);
std::vector<std::vector<cv::Point>> contours_a2;
cv::findContours(line_area_a2, contours_a2, hierarchy, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE);
if (contours_a2.size() > 0) {
cv::Rect rect = cv::boundingRect(contours_a2[0]);
int cx = rect.x + rect.width / 2;
int cy = rect.y + rect.height / 2 + cy_a2;
center_a2 = cv::Point(cx - half_w, cy - half_h);
}
}
void ColorLineDetector::detect(cv::Mat img, sv::TargetsInFrame& tgts_)
{
if(!this->is_load_parameter)
{
_load();
this->is_load_parameter = true;
}
int area_n = -1;
cv::Mat area_base, area_base_a1, area_base_a2;
cv::Point cxcy_n(0, 0), center_a1_n(0,0), center_a2_n(0, 0);
get_line_area(img, area_base, area_base_a1, area_base_a2);
seg(area_base, area_base_a1, area_base_a2, line_color, cxcy_n, area_n, center_a1_n, center_a2_n);
pose.x = 0.0;
pose.y = -1.0;
pose.z = 0.0;
if (area_n > 0)
{
circle(area_base, cv::Point(cxcy_n.x, cxcy_n.y), 4, cv::Scalar(0, 0, 255), -1);
double angle = (cxcy_n.x - this->camera_matrix.at<double>(0, 2)) / this->camera_matrix.at<double>(0, 2) * atan((double)(area_base.rows / 2) / this->fov_x);
pose.x = angle;
pose.y = 1.0;
}
else
{
cv::Point cxcy__n(0, 0), center_a1__n(0, 0), center_a2__n(0, 0);
seg(area_base, area_base_a1, area_base_a2, line_color, cxcy__n, area_n = 0, center_a1__n, center_a2__n);
if (area_n > 0)
{
circle(area_base, cv::Point(cxcy_n.x, cxcy_n.y), 4, cv::Scalar(0, 0, 255), -1);
double angle = (cxcy_n.x - this->camera_matrix.at<double>(0, 2)) / this->camera_matrix.at<double>(0, 2) * atan((double)(area_base.rows / 2) / this->fov_x);
pose.x = angle;
pose.y = 1.0;
pose.z = 0.0;
}
}
tgts_.setSize(img.cols, img.rows);
tgts_.setFOV(this->fov_x, this->fov_y);
auto t1 = std::chrono::system_clock::now();
tgts_.setFPS(1000.0/std::chrono::duration_cast<std::chrono::milliseconds>(t1- this->_t0).count());
this->_t0 = std::chrono::system_clock::now();
tgts_.setTimeNow();
if(area_n > 0)
{
Target tgt;
tgt.los_ax = pose.x;
if(cxcy_n.x !=0 || cxcy_n.y !=0)
{
tgt.cx = cxcy_n.x;
tgt.cy = cxcy_n.y;
}
else if(center_a1_n.x != 0 || center_a1_n.y != 0)
{
tgt.cx = center_a1_n.x;
tgt.cy = center_a1_n.y;
}
else if(center_a2_n.x != 0 || center_a2_n.y != 0)
{
tgt.cx = center_a2_n.x;
tgt.cy = center_a2_n.y;
}
tgts_.targets.push_back(tgt);
}
}
}

48
include/sv_color_line.h Normal file
View File

@ -0,0 +1,48 @@
#ifndef __SV_COLOR_LINE__
#define __SV_COLOR_LINE__
#include "sv_core.h"
#include <opencv2/opencv.hpp>
#include <string>
#include <chrono>
namespace sv {
class ColorLineDetector : public CameraAlgorithm
{
public:
ColorLineDetector();
~ColorLineDetector();
void detect(cv::Mat img_, TargetsInFrame& tgts_);
protected:
float cy_a1;
float cy_a2;
float half_h;
float half_w;
cv::Mat img;
cv::Point3d pose;
double line_location;
double line_location_a1;
double line_location_a2;
bool is_load_parameter;
std::string line_color;
void _load();
float cnt_area(std::vector<cv::Point> cnt);
void get_line_area(cv::Mat &frame, cv::Mat &line_area, cv::Mat &line_area_a1, cv::Mat &line_area_a2);
void seg(cv::Mat line_area, cv::Mat line_area_a1, cv::Mat line_area_a2, std::string _line_color, cv::Point &center, int &area, cv::Point &center_a1, cv::Point &center_a2);
};
}
#endif

View File

@ -5,6 +5,7 @@
#include "sv_common_det.h"
#include "sv_landing_det.h"
#include "sv_tracking.h"
#include "sv_color_line.h"
#include "sv_video_input.h"
#include "sv_video_output.h"

View File

@ -174,5 +174,14 @@
"perspectiveRemovePixelPerCell": 4,
"polygonalApproxAccuracyRate": 0.03,
"useAruco3Detection": false
},
"ColorLineDetector": {
"line_color": "black",
"line_location": 0.5,
"line_location_a1": 0.3,
"line_location_a2": 0.7,
}
}

View File

@ -0,0 +1,69 @@
#include <iostream>
#include <string>
// 包含SpireCV SDK头文件
#include <sv_world.h>
using namespace std;
using namespace sv;
int main(int argc, char *argv[]) {
// 实例化 color line detection 检测器类
sv::ColorLineDetector cld;
// 手动导入相机参数如果使用Amov的G1等吊舱或相机则可以忽略该步骤将自动下载相机参数文件
cld.loadCameraParams(sv::get_home() + "/SpireCV/calib_webcam_640x480.yaml");
// 打开摄像头
sv::Camera cap;
cap.setWH(640, 480);
//cap.setFps(30);
cap.open(sv::CameraType::WEBCAM, 0); // CameraID 0
// 实例化OpenCV的Mat类用于内存单帧图像
cv::Mat img;
int frame_id = 0;
while (1)
{
// 实例化SpireCV的 单帧检测结果 接口类 TargetsInFrame
sv::TargetsInFrame tgts(frame_id++);
// 读取一帧图像到img
cap.read(img);
cv::resize(img, img, cv::Size(cld.image_width, cld.image_height));
// 执行 color line detection 检测
cld.detect(img, tgts);
// 可视化检测结果叠加到img上
sv::drawTargetsInFrame(img, tgts);
// 控制台打印 color line detection 检测结果
printf("Frame-[%d]\n", frame_id);
// 打印当前检测的FPS
printf(" FPS = %.2f\n", tgts.fps);
// 打印当前相机的视场角degree
printf(" FOV (fx, fy) = (%.2f, %.2f)\n", tgts.fov_x, tgts.fov_y);
// 打印当前输入图像的像素宽度和高度
printf(" Frame Size (width, height) = (%d, %d)\n", tgts.width, tgts.height);
for (int i=0; i<tgts.targets.size(); i++)
{
// 打印每个 color_line 的中心位置cxcy的值域为[0, 1]以及cxcy的像素值
printf(" Color Line detect Center (cx, cy) = (%.3f, %.3f), in Pixels = ((%d, %d))\n",
tgts.targets[i].cx, tgts.targets[i].cy);
// 打印每个color_line的x_方向反正切值跟相机视场相关
printf(" Color Line detect Line-of-sight (ax, ay) = (%.3f, %.3f)\n", tgts.targets[i].los_ax, tgts.targets[i].los_ay);
}
// 显示检测结果img
cv::imshow("img", img);
cv::waitKey(10);
}
return 0;
}