Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

スキル・継承タブの結合処理改善 #94

Merged
merged 4 commits into from
Jul 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions UmaUmaChecker/CombineImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ void CombineImage::StartCapture()
Images.clear();
DetectedYLines.clear();
BarLength = 0;
Error = L"";

LOG_DEBUG << L"[CombineImage::StartCapture] ウマ娘詳細結合開始";

Expand Down Expand Up @@ -131,10 +132,11 @@ bool CombineImage::Combine()
return false;
}

void CombineImage::_EndCapture()
void CombineImage::_EndCapture(const std::wstring& error)
{
if (!IsCapture) return;

Error = error;
IsCapture = false;
status = Stop;
}
Expand All @@ -158,9 +160,9 @@ void CombineImage::Capture()

if (BarLength == 0 && scroll.GetBarLength() > 0) BarLength = scroll.GetBarLength();

if (!IsScanStarted && scroll.GetBarLength() == 0) {
if (!IsScanStarted && (!scroll.IsValid() || scroll.GetBarLength() == 0)) {
LOG_DEBUG << L"[CombineImage::Capture] 停止, !IsScanStarted && scroll.GetBarLength() == 0";
_EndCapture();
_EndCapture(L"スクロールバーを検出できませんでした。");
// 通常キャプチャ
delete image;
return;
Expand All @@ -173,18 +175,17 @@ void CombineImage::Capture()
status = WaitForMovingScrollbarOnTop;
}

if (BarLength > 0 && std::abs(BarLength - scroll.GetBarLength()) > 1) {
if (IsManualStop) {
LOG_DEBUG << L"[CombineImage::Capture] 停止, BarLength > 0 && std::abs(BarLength - scroll.GetBarLength()) > 1) && IsManualStop";
_EndCapture();
}
if (!PrevImage.empty() && (PrevImage.size().width != mat.size().width || PrevImage.size().height != mat.size().height)) {
LOG_DEBUG << L"[CombineImage::Capture] 停止, !PrevImage.empty() && (PrevImage.size().width != mat.size().width || PrevImage.size().height != mat.size().height)";
_EndCapture(L"ウィンドウサイズが変更されました。");
delete image;
return;
}

if (!PrevImage.empty() && (PrevImage.size().width != mat.size().width || PrevImage.size().height != mat.size().height)) {
LOG_DEBUG << L"[CombineImage::Capture] 停止, !PrevImage.empty() && (PrevImage.size().width != mat.size().width || PrevImage.size().height != mat.size().height)";
_EndCapture();
else if (BarLength > 0 && std::abs(BarLength - scroll.GetBarLength()) > 1) {
if (IsManualStop) {
LOG_DEBUG << L"[CombineImage::Capture] 停止, BarLength > 0 && std::abs(BarLength - scroll.GetBarLength()) > 1) && IsManualStop";
_EndCapture(L"スクロールバーの大きさが変わっています。");
}
delete image;
return;
}
Expand Down Expand Up @@ -250,6 +251,7 @@ void CombineImage::Capture()
DetectedYLines.emplace_back(0, y);
IsFirstScan = false;
CurrentScrollPos = scroll.GetPos();
PrevImage = mat.clone();
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion UmaUmaChecker/CombineImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <vector>
#include <opencv2/opencv.hpp>
#include <string>

struct CombineImageInfo {
int StartY;
Expand Down Expand Up @@ -29,6 +30,7 @@ class CombineImage
int GetProgressTime() const { return msec; }
CombineStatus GetStatus() const { return status; }
bool IsImageSaved() const { return IsSavedImage; }
std::wstring GetError() const { return Error; }

bool IsCapturing() const { return IsCapture; }
void StartCapture();
Expand All @@ -37,7 +39,7 @@ class CombineImage
bool Combine();

private:
void _EndCapture();
void _EndCapture(const std::wstring& error = L"");
void Capture();

int GetTemplateImage(const cv::Mat& mat, cv::Mat& cut);
Expand All @@ -61,6 +63,8 @@ class CombineImage
bool IsManualStop;
bool IsSavedImage;

std::wstring Error;

std::vector<CombineImageInfo> DetectedYLines;
std::vector<cv::Mat> Images;

Expand Down
13 changes: 7 additions & 6 deletions UmaUmaChecker/MainFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,6 @@ MainFrame::MainFrame(wxWindow* parent, const wxPoint& pos, const wxSize& size, l

m_comboPopup = new wxComboBoxPopup(this);

this->SetSizer(bSizerTop);
this->Fit();
this->Layout();
this->Centre(wxBOTH);

// イベントバインド
this->Bind(wxEVT_CLOSE_WINDOW, &MainFrame::OnClose, this);
m_toggleBtnStart->Bind(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, &MainFrame::OnClickStart, this);
Expand Down Expand Up @@ -167,6 +162,10 @@ MainFrame::MainFrame(wxWindow* parent, const wxPoint& pos, const wxSize& size, l
if (!config->IsShowStatusBar) m_statusBar->Hide();
else timer.Start(1000);

this->SetSizer(bSizerTop);
this->Fit();
this->Layout();

Init();
}

Expand Down Expand Up @@ -343,7 +342,9 @@ void MainFrame::OnClickCombine(wxCommandEvent& event)
CombineTimer.Stop();
this->SetTitle(app_title);
if (!combine.IsImageSaved()) {
wxMessageBox(wxT("キャプチャに失敗しました。"), app_title, wxICON_ERROR);
std::wstring error = combine.GetError();
if (!error.empty()) wxMessageBox(error, app_title, wxICON_ERROR);
else wxMessageBox(wxT("キャプチャに失敗しました。"), app_title, wxICON_ERROR);
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions UmaUmaChecker/Point.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

template<typename T>
class Point
{
public:
Point() {}
Point(T x, T y) : x_(x), y_(y) {}

T x() const { return x_; }
T y() const { return y_; }

void x(const T& x) { x_ = x; }
void y(const T& y) { y_ = y; }

private:
T x_, y_;
};

111 changes: 83 additions & 28 deletions UmaUmaChecker/ScrollbarDetector.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#include "ScrollbarDetector.h"

#include <mutex>

#include <opencv2/opencv.hpp>


ScrollbarDetector::ScrollbarDetector(cv::Mat& img)
Point<double> ScrollbarDetector::Start(0.967, 0.08);
Point<double> ScrollbarDetector::End(0.967, 1.0 - 0.08);

ScrollbarDetector::ScrollbarDetector(cv::Mat& img) : valid(false)
{
InitScrollInfo(img);
}
Expand All @@ -13,73 +17,124 @@ ScrollbarDetector::~ScrollbarDetector()
{
}

bool ScrollbarDetector::IsValid() const
{
return valid;
}

int ScrollbarDetector::GetPos() const
{
return StartY;
return valid ? ScrollBarStartY : 0;
}

int ScrollbarDetector::GetTotalLength() const
{
return TotalLength;
return valid ? TotalLength : 0;
}

int ScrollbarDetector::GetBarLength() const
{
return Length;
return valid ? Length : 0;
}

int ScrollbarDetector::GetBarLengthRatio() const
{
return (double)Length / TotalLength * 100;
return valid ? (double)Length / TotalLength * 100 : 0;
}

bool ScrollbarDetector::IsBegin() const
{
return StartY == 0;
return valid && ScrollBarStartY == MinY;
}

bool ScrollbarDetector::IsEnd() const
{
return EndY + 1 == TotalLength;
return valid && ScrollBarEndY == MaxY;
}

void ScrollbarDetector::InitScrollInfo(cv::Mat& img)
{
TotalLength = img.size().height;
TotalLength = 0;
Length = 0;

StartY = EndY = -1;
ScrollBarStartY = ScrollBarEndY = -1;

cv::Mat scr;
cv::inRange(img, cv::Scalar(140, 121, 123), cv::Scalar(180, 179, 189), scr);
cv::Mat bar, scr;
cv::inRange(img, cv::Scalar(140, 121, 123), cv::Scalar(180, 179, 189), bar);
cv::inRange(img, cv::Scalar(140, 121, 123), cv::Scalar(217, 210, 211), scr);

MinY = -1;
MaxY = -1;
for (int y = 0; y < scr.size().height; y++) {
cv::uint8_t c = scr.at<cv::uint8_t>(y, 2);
if (c == 255 && StartY == -1) {
/*
bool hasBlack = false;
for (int x = 0; x < scr.size().width; x++) {
cv::uint8_t bw = scr.at<cv::uint8_t>(y, x);
if (bw == 0) {
hasBlack = true;
break;
}
for (int x = 0; x < scr.size().width; x++) {
cv::uint8_t b = scr.at<cv::uint8_t>(y, x);
if (b == 255) {
if (MinY == -1) MinY = y;
if (y > MaxY) MaxY = y;
}
}
}

if (MinY == -1 || MaxY == -1) return;

TotalLength = MaxY - MinY;

for (int y = MinY; y <= MaxY; y++) {
cv::uint8_t c = 0;

if (!hasBlack) */
StartY = y;
for (int x = 0; x < bar.size().width && c != 255; x++) c = bar.at<cv::uint8_t>(y, x);

if (c == 255 && ScrollBarStartY == -1) {
ScrollBarStartY = y;
}
else if (c == 0 && StartY != -1 && EndY == -1) {
EndY = y - 1;
else if (c == 0 && ScrollBarStartY != -1 && ScrollBarEndY == -1) {
ScrollBarEndY = y - 1;
break;
}
}

if (StartY == -1 && EndY == -1) {
if (ScrollBarStartY == -1 && ScrollBarEndY == -1) {
return;
}

if (EndY == -1) EndY = scr.size().height - 1;
if (ScrollBarEndY == -1) ScrollBarEndY = MaxY;

Length = ScrollBarEndY - ScrollBarStartY;
valid = true;
}

std::list<Point<int>> ScrollbarDetector::GetMargin(cv::Mat& img)
{
auto start = Point<int>(img.size().width * Start.x(), img.size().height * Start.y());
auto end = Point<int>(img.size().width * End.x(), img.size().height * End.y());

cv::Mat scr;
cv::inRange(img, cv::Scalar(140, 121, 123), cv::Scalar(217, 210, 211), scr);

cv::Mat test;
cv::inRange(img, cv::Scalar(170, 151, 153), cv::Scalar(255, 255, 255), scr);
cv::inRange(img, cv::Scalar(170, 151, 153), cv::Scalar(255, 255, 255), scr);

std::once_flag once_start;
Point<int> pos_start;
Point<int> pos_end;

for (int y = start.y(); y < end.y(); y++) {
auto c = scr.at<cv::uint8_t>(y, start.x());
if (c == 255) {
std::call_once(once_start, [&] {
pos_start.x(start.x());
pos_start.y(y);
});
pos_end.x(start.x());
pos_end.y(y);
}
else
break;
}

Length = EndY - StartY;
return {
pos_start,
pos_end
};
}
21 changes: 19 additions & 2 deletions UmaUmaChecker/ScrollbarDetector.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#pragma once

#include <list>

#include "Point.h"

namespace cv {
class Mat;
}
Expand All @@ -10,6 +14,7 @@ class ScrollbarDetector
ScrollbarDetector(cv::Mat& img);
~ScrollbarDetector();

bool IsValid() const;
int GetPos() const;
int GetTotalLength() const;
int GetBarLength() const;
Expand All @@ -20,10 +25,22 @@ class ScrollbarDetector
private:
void InitScrollInfo(cv::Mat& img);

std::list<Point<int>> GetMargin(cv::Mat& img);

private:
static Point<double> Start;
static Point<double> End;

private:
bool valid;

int TotalLength;
int Length;
int StartY;
int EndY;

int ScrollBarStartY;
int ScrollBarEndY;

int MinY;
int MaxY;
};

1 change: 1 addition & 0 deletions UmaUmaChecker/UmaUmaChecker.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@
<ClInclude Include="GrandLiveMusicListFrame.h" />
<ClInclude Include="Log.h" />
<ClInclude Include="object_pool.hpp" />
<ClInclude Include="Point.h" />
<ClInclude Include="ScenarioData.h" />
<ClInclude Include="ScrollbarDetector.h" />
<ClInclude Include="Tesseract.h" />
Expand Down
3 changes: 3 additions & 0 deletions UmaUmaChecker/UmaUmaChecker.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,9 @@
<ClInclude Include="DebugImageCombineFrame.h">
<Filter>ヘッダー ファイル</Filter>
</ClInclude>
<ClInclude Include="Point.h">
<Filter>ヘッダー ファイル</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="UmaUmaChecker.rc">
Expand Down