更新了灯条识别方法,提升了准确率。

This commit is contained in:
xinyang
2019-06-27 13:13:25 +08:00
parent 9a1e5d8882
commit 64bd93ec56
6 changed files with 81 additions and 45 deletions

View File

@@ -1,6 +1,6 @@
CMAKE_MINIMUM_REQUIRED(VERSION 3.5) CMAKE_MINIMUM_REQUIRED(VERSION 3.5)
PROJECT(RM_Auto_Aim) PROJECT(AutoAim)
SET(CMAKE_CXX_STANDARD 11) SET(CMAKE_CXX_STANDARD 11)
SET(CMAKE_BUILD_TYPE RELEASE) SET(CMAKE_BUILD_TYPE RELEASE)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3") SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")

View File

@@ -52,7 +52,7 @@ public:
double length; double length;
uint8_t BlobColor; uint8_t BlobColor;
explicit LightBlob(cv::RotatedRect &r) : rect(r) { LightBlob(cv::RotatedRect &r) : rect(r) {
length = max(rect.size.height, rect.size.width); length = max(rect.size.height, rect.size.width);
}; };
bool operator<(LightBlob &l2) { return this->rect.center.x < l2.rect.center.x; } bool operator<(LightBlob &l2) { return this->rect.center.x < l2.rect.center.x; }

View File

@@ -12,15 +12,48 @@
typedef std::vector<LightBlob> LightBlobs; typedef std::vector<LightBlob> LightBlobs;
static double lw_rate(const cv::RotatedRect &rect){ static double lw_rate(const cv::RotatedRect &rect){
return (rect.size.height > rect.size.width)? return rect.size.height>rect.size.width ?
(rect.size.height / rect.size.width): rect.size.height / rect.size.width :
(rect.size.width / rect.size.height); rect.size.width / rect.size.height ;
} }
static bool isValidLightBlob(const cv::RotatedRect &rect){ bool rectangleContainPoint(cv::RotatedRect rectangle, cv::Point2f point)
return (lw_rate(rect) > 1.2) && {
((rect.size.width * rect.size.height) < 3000) && //转化为轮廓
((rect.size.width * rect.size.height) > 1); cv::Point2f corners[4];
rectangle.points(corners);
cv::Point2f *lastItemPointer = (corners+sizeof corners/sizeof corners[0]);
vector<cv::Point2f> contour(corners,lastItemPointer);
//判断
double indicator = pointPolygonTest(contour,point,true);
return indicator >= 0;
}
/// Todo: 下面的函数可以有性能优化,暂时未做。
static double nonZeroRateOfRotateRect(const cv::Mat &bin, const cv::RotatedRect &rorect){
auto rect = rorect.boundingRect();
if(rect.x < 0 || rect.y < 0 || rect.x+rect.width > bin.cols || rect.y+rect.height > bin.rows){
// cout << "break" << endl;
return 0;
}
auto roi=bin(rect);
int cnt=0;
for(int r=0; r<roi.rows; r++){
for(int c=0; c<roi.cols; c++){
if(rectangleContainPoint(rorect, cv::Point(c+rect.x, r+rect.y))){
if(roi.at<uint8_t>(r, c)){
cnt++;
}
}
}
}
return double(cnt) / rorect.size.area();
}
static bool isValidLightBlob(const cv::Mat &bin, const cv::RotatedRect &rect){
return (lw_rate(rect) > 1.5) &&
// (rect.size.width*rect.size.height < 3000) &&
(rect.size.width*rect.size.height > 1) &&
(nonZeroRateOfRotateRect(bin, rect) > 0.8);
} }
static void pipelineLightBlobPreprocess(cv::Mat &src) { static void pipelineLightBlobPreprocess(cv::Mat &src) {
@@ -39,10 +72,11 @@ static bool findLightBlobs(const cv::Mat &src, LightBlobs &light_blobs) {
} }
std::vector<std::vector<cv::Point> > light_contours; std::vector<std::vector<cv::Point> > light_contours;
cv::findContours(src_gray, light_contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE); cv::findContours(src_gray, light_contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);
for (auto &light_contour : light_contours) { for (auto &light_contour : light_contours) {
cv::RotatedRect rect = cv::minAreaRect(light_contour); cv::RotatedRect rect = cv::minAreaRect(light_contour);
if(isValidLightBlob(rect)){ if(isValidLightBlob(src_gray, rect)){
light_blobs.emplace_back(rect); light_blobs.emplace_back(rect);
} }
} }
@@ -199,22 +233,23 @@ bool ArmorFinder::stateSearchingTarget(cv::Mat &src) {
cv::waitKey(1); cv::waitKey(1);
} }
imageColorSplit(src, split, enemy_color); // imageColorSplit(src, split, enemy_color);
// imshow("split123",split); //// imshow("split123",split);
imagePreProcess(split); // imagePreProcess(split);
// imshow("split",split); //// imshow("split",split);
cv::threshold(split, src_bin, 170, 255, CV_THRESH_BINARY); // cv::threshold(split, src_bin, 170, 255, CV_THRESH_BINARY);
if(!findLightBlobs(src_bin, light_blobs_)){ // if(!findLightBlobs(src_bin, light_blobs_)){
return false; // return false;
} // }
if(show_light_blobs){ // if(show_light_blobs){
showContours("blobs_split", src_bin, light_blobs_); // showContours("blobs_split", src_bin, light_blobs_);
cv::waitKey(1); // cv::waitKey(1);
} // }
//
if(!judge_light_color(light_blobs, light_blobs_, light_blobs_real)){ // if(!judge_light_color(light_blobs, light_blobs_, light_blobs_real)){
return false; // return false;
} // }
light_blobs_real = light_blobs;
get_blob_color(src, light_blobs_real); get_blob_color(src, light_blobs_real);
if(show_light_blobs){ if(show_light_blobs){
showContours("blobs_real", src, light_blobs_real); showContours("blobs_real", src, light_blobs_real);
@@ -225,7 +260,7 @@ bool ArmorFinder::stateSearchingTarget(cv::Mat &src) {
return false; return false;
} }
if(show_armor_boxes){ if(show_armor_boxes){
showArmorBoxVector("boxes", split, armor_boxes); showArmorBoxVector("boxes", src, armor_boxes);
cv::waitKey(1); cv::waitKey(1);
} }
if(classifier && use_classifier){ if(classifier && use_classifier){

View File

@@ -71,7 +71,8 @@ void showContours(std::string windows_name, const cv::Mat &src, const std::vecto
rectangle(image2show, light_blob.rect.boundingRect(), Scalar(0,0,255), 3); rectangle(image2show, light_blob.rect.boundingRect(), Scalar(0,0,255), 3);
if(light_blob.BlobColor == BLOB_BLUE) if(light_blob.BlobColor == BLOB_BLUE)
rectangle(image2show, light_blob.rect.boundingRect(), Scalar(255,0,0), 3); rectangle(image2show, light_blob.rect.boundingRect(), Scalar(255,0,0), 3);
else
rectangle(image2show, light_blob.rect.boundingRect(), Scalar(0,255,0), 3);
} }
imshow(windows_name, image2show); imshow(windows_name, image2show);
} }

View File

@@ -32,7 +32,7 @@ mcu_data mcuData = {
ARMOR_STATE, ARMOR_STATE,
0, 0,
1, 1,
ENEMY_RED, ENEMY_BLUE,
}; };
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
@@ -54,11 +54,11 @@ int main(int argc, char *argv[]) {
initVideoWriter(energy_video_writer, PROJECT_DIR"/energy_video/"); initVideoWriter(energy_video_writer, PROJECT_DIR"/energy_video/");
} }
WrapperHead *video_armor; WrapperHead *video_armor=nullptr;
WrapperHead *video_energy; WrapperHead *video_energy=nullptr;
if (from_camera) { if (from_camera) {
video_armor = new CameraWrapper(0, "armor"); video_armor = new CameraWrapper(0/*, "armor"*/);
video_energy = new CameraWrapper(1, "energy"); // video_energy = new CameraWrapper(1, "energy");
} else { } else {
// string armor_video, energy_video; // string armor_video, energy_video;
// lastVideo(armor_video, PROJECT_DIR"/armor_video/"); // lastVideo(armor_video, PROJECT_DIR"/armor_video/");
@@ -66,7 +66,7 @@ int main(int argc, char *argv[]) {
// lastVideo(energy_video, PROJECT_DIR"/energy_video/"); // lastVideo(energy_video, PROJECT_DIR"/energy_video/");
// video_energy = new VideoWrapper(energy_video); // video_energy = new VideoWrapper(energy_video);
video_armor = new VideoWrapper("/home/sjturm/Desktop/valid_video/armor/65.avi"); video_armor = new VideoWrapper("/home/sjturm/Desktop/valid_video/armor/65.avi");
video_energy = new VideoWrapper("/home/sjturm/Desktop/valid_video/energy/121.avi"); // video_energy = new VideoWrapper("/home/sjturm/Desktop/valid_video/energy/121.avi");
} }
if (video_armor->init()) { if (video_armor->init()) {
LOGM("video_armor source initialization successfully."); LOGM("video_armor source initialization successfully.");
@@ -75,13 +75,13 @@ int main(int argc, char *argv[]) {
delete video_armor; delete video_armor;
video_armor = nullptr; video_armor = nullptr;
} }
if (video_energy->init()) { // if (video_energy->init()) {
LOGM("video_energy source initialization successfully."); // LOGM("video_energy source initialization successfully.");
} else { // } else {
LOGW("video_energy source unavailable!"); // LOGW("video_energy source unavailable!");
delete video_energy; // delete video_energy;
video_energy = nullptr; // video_energy = nullptr;
} // }
Mat energy_src, armor_src; Mat energy_src, armor_src;
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {

View File

@@ -75,10 +75,10 @@ bool CameraWrapper::init() {
LOGM("successfully loaded %s!", filepath); LOGM("successfully loaded %s!", filepath);
#elif defined(Linux) #elif defined(Linux)
CameraSetAeState(h_camera, false); CameraSetAeState(h_camera, false);
CameraSetExposureTime(h_camera, 17*1000); CameraSetExposureTime(h_camera, 8*1000);
CameraSetAnalogGain(h_camera, 60); CameraSetAnalogGain(h_camera, 16);
if(mode == 0){ if(mode == 0){
CameraSetGain(h_camera, 140, 140, 140); CameraSetGain(h_camera, 100, 131, 113);
CameraSetLutMode(h_camera, LUTMODE_PRESET); CameraSetLutMode(h_camera, LUTMODE_PRESET);
} }
#endif #endif