Skip to content

Commit

Permalink
feat: improve logic of drop area recognition
Browse files Browse the repository at this point in the history
  • Loading branch information
KumoSiunaus committed Nov 4, 2022
1 parent ce76ebe commit 54b84c4
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ class Widget_Stage : public WidgetWithCandidate<std::vector<int>, int>
q(comp);
auto last_pop = _stage_chrs;
size_t length = _stage_chrs.size();
while (_candidates.size() < _CANDIDATES_COUNT)
while (_candidates.size() <= _CANDIDATES_COUNT)
{
for (size_t i = 0; i < length; ++i)
{
Expand Down Expand Up @@ -882,6 +882,10 @@ class Widget_DropArea : public Widget
: Widget("dropArea", parent_widget) {}
Widget_DropArea(const cv::Mat& img, Widget* const parent_widget = nullptr)
: Widget(img, "dropArea", parent_widget) {}
void set_item_diameter(int diameter)
{
_item_diameter = diameter;
}
Widget_DropArea& analyze(const std::string& stage)
{
if (!_img.empty())
Expand Down Expand Up @@ -940,6 +944,7 @@ class Widget_DropArea : public Widget
}

private:
int _item_diameter = 0;
dict _drops_data;
std::vector<Drop> _drop_list;
std::vector<Widget_Droptype> _droptype_list;
Expand Down Expand Up @@ -975,12 +980,11 @@ class Widget_DropArea : public Widget
}
int baseline_h = row + offset;
auto sp = separate(img_bin(cv::Rect(0, row, width, 1)), DirectionFlags::LEFT);
int item_diameter = static_cast<int>(height / DROP_AREA_HEIGHT_PROP * ITEM_DIAMETER_PROP);
for (auto it = sp.cbegin(); it != sp.cend();)
{
const auto& range = *it;
if (const auto length = range.end - range.start;
length < item_diameter)
length < _item_diameter)
{
it = sp.erase(it);
}
Expand Down Expand Up @@ -1057,7 +1061,6 @@ class Widget_DropArea : public Widget
{
return;
}
int item_diameter = static_cast<int>(height / DROP_AREA_HEIGHT_PROP * ITEM_DIAMETER_PROP);
ItemTemplates templs {stage};
for (const auto& droptype : _droptype_list)
{
Expand Down Expand Up @@ -1096,7 +1099,7 @@ class Widget_DropArea : public Widget
cv::Range(droptype.x - x + length * i,
droptype.x - x + length * (i + 1));
auto dropimg = _img(cv::Range(0, droptype.y - y), range);
Widget_Item drop {dropimg, item_diameter, label, this};
Widget_Item drop {dropimg, _item_diameter, label, this};
drop.analyze(templs);
_drop_list.emplace_back(drop, type);
_drops_data.push_back({{"dropType", Droptype2Str[type]},
Expand Down Expand Up @@ -1289,6 +1292,7 @@ class Result_New : public Widget
Result_New& analyze()
{
_get_baseline_v();
_drop_area.set_item_diameter(static_cast<int>(round(_baseline_v.height * ITEM_DIAMETER_PROP)));
_get_result_label();
_get_stars();
_get_stage();
Expand Down Expand Up @@ -1572,12 +1576,13 @@ class Result_New : public Widget
cv::Range(bv.x + bv.width, static_cast<int>(0.2 * width)));
cv::cvtColor(img_bin, img_bin, cv::COLOR_BGR2GRAY);
cv::threshold(img_bin, img_bin, 127, 255, cv::THRESH_BINARY);
auto sp = separate(img_bin, DirectionFlags::TOP, 2);
auto sp = separate(img_bin, DirectionFlags::TOP);
// int top_margin = bv.y + bv.height + sp[1].start;
int top_margin = bv.y + bv.height + static_cast<int>(height * 0.12);
int top_line = bv.y + bv.height + sp.front().start;
int bottom_line = bv.y + bv.height + sp.back().end;
// int top_margin = bv.y + bv.height + static_cast<int>(height * 0.12);
auto drop_area_img = _img(
cv::Range(top_margin + static_cast<int>(bv.height * DROP_AREA_Y_PROP),
top_margin + bv.height),
cv::Range(top_line + static_cast<int>((bottom_line - top_line) * 0.375), bottom_line),
cv::Range(bv.x + bv.width, width));
_drop_area.set_img(drop_area_img);
_drop_area.analyze(_stage.stage_code());
Expand Down

0 comments on commit 54b84c4

Please sign in to comment.