1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
#include "pch.h" namespace cv_dnn { namespace {
template <typename T> static inline bool SortScorePairDescend(const std::pair<float, T>& pair1, const std::pair<float, T>& pair2) { return pair1.first > pair2.first; }
}
inline void GetMaxScoreIndex(const std::vector<float>& scores, const float threshold, const int top_k,std::vector<std::pair<float, int> >& score_index_vec) { for (size_t i = 0; i < scores.size(); ++i) { if (scores[i] > threshold) { score_index_vec.push_back(std::make_pair(scores[i], i)); } } std::stable_sort(score_index_vec.begin(), score_index_vec.end(), SortScorePairDescend<int>); if (top_k > 0 && top_k < (int)score_index_vec.size()) { score_index_vec.resize(top_k); } }
template <typename BoxType> inline void NMSFast_(const std::vector<BoxType>& bboxes, const std::vector<float>& scores, const float score_threshold, const float nms_threshold, const float eta, const int top_k, std::vector<int>& indices, float (*computeOverlap)(const BoxType&, const BoxType&)) { CV_Assert(bboxes.size() == scores.size()); std::vector<std::pair<float, int> > score_index_vec; GetMaxScoreIndex(scores, score_threshold, top_k, score_index_vec);
float adaptive_threshold = nms_threshold; indices.clear(); for (size_t i = 0; i < score_index_vec.size(); ++i) { const int idx = score_index_vec[i].second; bool keep = true; for (int k = 0; k < (int)indices.size() && keep; ++k) { const int kept_idx = indices[k]; float overlap = computeOverlap(bboxes[idx], bboxes[kept_idx]); keep = overlap <= adaptive_threshold; } if (keep) indices.push_back(idx); if (keep && eta < 1 && adaptive_threshold > 0.5) { adaptive_threshold *= eta; } } }
template<typename _Tp> static inline double jaccardDistance__(const Rect_<_Tp>& a, const Rect_<_Tp>& b) { _Tp Aa = a.area(); _Tp Ab = b.area();
if ((Aa + Ab) <= std::numeric_limits<_Tp>::epsilon()) { return 0.0; }
double Aab = (a & b).area(); return 1.0 - Aab / (Aa + Ab - Aab); }
template <typename T> static inline float rectOverlap(const T& a, const T& b) { return 1.f - static_cast<float>(jaccardDistance__(a, b)); }
void NMSBoxes(const std::vector<Rect>& bboxes, const std::vector<float>& scores, const float score_threshold, const float nms_threshold, std::vector<int>& indices, const float eta = 1, const int top_k = 0) { NMSFast_(bboxes, scores, score_threshold, nms_threshold, eta, top_k, indices, rectOverlap); }
}
void train(string imgLocation, string saveTemplateDir, int num_feature, Rect roi, int padding, string class_id) { line2Dup::Detector detector(num_feature, { 4, 8 });
Mat img = imread(imgLocation); assert(!img.empty() && "check your img path"); img = img(roi).clone();
Mat mask = Mat(img.size(), CV_8UC1, { 255 });
cv::Mat padded_img = cv::Mat(img.rows + 2 * padding, img.cols + 2 * padding, img.type(), cv::Scalar::all(0)); img.copyTo(padded_img(Rect(padding, padding, img.cols, img.rows))); cv::Mat padded_mask = cv::Mat(mask.rows + 2 * padding, mask.cols + 2 * padding, mask.type(), cv::Scalar::all(0)); mask.copyTo(padded_mask(Rect(padding, padding, img.cols, img.rows)));
shape_based_matching::shapeInfo_producer shapes(padded_img, padded_mask); shapes.angle_range = { 0, 360 }; shapes.angle_step = 1; shapes.scale_range = { 1.0f }; shapes.produce_infos(); std::vector<shape_based_matching::shapeInfo_producer::Info> infos_have_templ;
bool is_first = true; int first_id = 0; float first_angle = 0; float first_scale = 0;
for (auto& info : shapes.infos) { Mat to_show = shapes.src_of(info); int templ_id = detector.addTemplate(shapes.src_of(info), class_id, shapes.mask_of(info), int(num_feature * info.scale));
if (templ_id != -1) { auto templ = detector.getTemplates(class_id, templ_id); for (int i = 0; i < templ[0].features.size(); i++) { auto feat = templ[0].features[i]; cv::circle(to_show, { feat.x + templ[0].tl_x, feat.y + templ[0].tl_y }, 3, { 0, 0, 255 }, -1); } infos_have_templ.push_back(info); }
if (fabs(info.scale - first_scale) > 0.002f) { is_first = true; } detector.writeClasses(saveTemplateDir + class_id + "_templ.yaml"); shapes.save_infos(infos_have_templ, saveTemplateDir + class_id + "_info.yaml"); } }
vector<recognizedObjectLocation> test(string testImgLocation, string loadTemplateDir, int num_feature, Rect train_roi, int train_padding, string class_id, int score_thershold = 90, int nms_thershold = 0) { vector<recognizedObjectLocation> ObjLocations;
std::vector<std::string> ids; ids.push_back(class_id); line2Dup::Detector detector(num_feature, { 4, 8 }); detector.readClasses(ids, loadTemplateDir + class_id + "_templ.yaml"); auto infos = shape_based_matching::shapeInfo_producer::load_infos(loadTemplateDir + class_id + "_info.yaml");
Mat test_img = imread(testImgLocation); assert(!test_img.empty() && "check your img path"); int padding = 0; cv::Mat padded_img = cv::Mat(test_img.rows + 2 * padding, test_img.cols + 2 * padding, test_img.type(), cv::Scalar::all(0)); test_img.copyTo(padded_img(Rect(padding, padding, test_img.cols, test_img.rows))); int stride = 32; int n = padded_img.rows / stride; int m = padded_img.cols / stride; Rect roi(0, 0, stride * m, stride * n); Mat img = padded_img(roi).clone(); assert(img.isContinuous());
auto matches = detector.match(img, score_thershold, ids);
vector<Rect> boxes; vector<float> scores; vector<int> idxs;
for (auto match : matches) { Rect box; box.x = match.x; box.y = match.y;
auto templ = detector.getTemplates(class_id, match.template_id);
box.width = templ[0].width; box.height = templ[0].height; boxes.push_back(box); scores.push_back(match.similarity); }
cv_dnn::NMSBoxes(boxes, scores, score_thershold, nms_thershold, idxs);
for (auto idx : idxs) { auto match = matches[idx]; auto templ = detector.getTemplates(class_id, match.template_id);
recognizedObjectLocation obj;
obj.topleft.x = match.x; obj.topleft.y = match.y;
float train_img_half_width = train_roi.x / 2.0f + train_padding; float train_img_half_height = train_roi.y / 2.0f + train_padding; obj.center.x = match.x - templ[0].tl_x + train_img_half_width; obj.center.y = match.y - templ[0].tl_y + train_img_half_height;
obj.angle = 360 - infos[match.template_id].angle; obj.scale = infos[match.template_id].scale;
ObjLocations.push_back(obj); }
return ObjLocations; }
|