From 50222dddb605b90bd9d8ba4e5cad25b8d9dadfc5 Mon Sep 17 00:00:00 2001 From: Cilda Date: Mon, 3 Jul 2023 21:02:06 +0900 Subject: [PATCH 1/4] =?UTF-8?q?[update]=20=E3=82=B9=E3=82=AF=E3=83=AD?= =?UTF-8?q?=E3=83=BC=E3=83=AB=E3=83=90=E3=83=BC=E3=81=AE=E6=A4=9C=E5=87=BA?= =?UTF-8?q?=E5=87=A6=E7=90=86=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit スクロールバー範囲の開始位置と終了位置取得するように変更 --- UmaUmaChecker/CombineImage.cpp | 15 ++++---- UmaUmaChecker/MainFrame.cpp | 9 +++-- UmaUmaChecker/ScrollbarDetector.cpp | 55 ++++++++++++++++------------- UmaUmaChecker/ScrollbarDetector.h | 9 +++-- 4 files changed, 49 insertions(+), 39 deletions(-) diff --git a/UmaUmaChecker/CombineImage.cpp b/UmaUmaChecker/CombineImage.cpp index 045a6ea..5c3b93d 100644 --- a/UmaUmaChecker/CombineImage.cpp +++ b/UmaUmaChecker/CombineImage.cpp @@ -173,7 +173,13 @@ void CombineImage::Capture() status = WaitForMovingScrollbarOnTop; } - if (BarLength > 0 && std::abs(BarLength - scroll.GetBarLength()) > 1) { + 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(); + delete image; + return; + } + 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(); @@ -182,13 +188,6 @@ void CombineImage::Capture() 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(); - delete image; - return; - } - if (RecognizePoint.x == 0 && RecognizePoint.y == 0) { RecognizePoint.x = 0.02452830188679245283018867924528 * mat.size().width; RecognizePoint.y = std::round(0.46235418875927889713679745493107 * mat.size().height); diff --git a/UmaUmaChecker/MainFrame.cpp b/UmaUmaChecker/MainFrame.cpp index 2600d71..1ea757e 100644 --- a/UmaUmaChecker/MainFrame.cpp +++ b/UmaUmaChecker/MainFrame.cpp @@ -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); @@ -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(); } diff --git a/UmaUmaChecker/ScrollbarDetector.cpp b/UmaUmaChecker/ScrollbarDetector.cpp index c661683..52e6f99 100644 --- a/UmaUmaChecker/ScrollbarDetector.cpp +++ b/UmaUmaChecker/ScrollbarDetector.cpp @@ -15,7 +15,7 @@ ScrollbarDetector::~ScrollbarDetector() int ScrollbarDetector::GetPos() const { - return StartY; + return ScrollBarStartY; } int ScrollbarDetector::GetTotalLength() const @@ -35,51 +35,58 @@ int ScrollbarDetector::GetBarLengthRatio() const bool ScrollbarDetector::IsBegin() const { - return StartY == 0; + return ScrollBarStartY == MinY; } bool ScrollbarDetector::IsEnd() const { - return EndY + 1 == TotalLength; + return 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(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(y, x); - if (bw == 0) { - hasBlack = true; - break; - } + for (int x = 0; x < scr.size().width; x++) { + cv::uint8_t b = scr.at(y, x); + if (b == 255) { + if (MinY == -1) MinY = y; + if (y > MaxY) MaxY = y; } + } + } + + TotalLength = MaxY - MinY; + + for (int y = MinY; y < MaxY; y++) { + cv::uint8_t c = 0; + + for (int x = 0; x < bar.size().width && c != 255; x++) c = bar.at(y, x); - if (!hasBlack) */ - StartY = y; + 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 = EndY - StartY; + Length = ScrollBarEndY - ScrollBarStartY; } diff --git a/UmaUmaChecker/ScrollbarDetector.h b/UmaUmaChecker/ScrollbarDetector.h index 636a760..28299b3 100644 --- a/UmaUmaChecker/ScrollbarDetector.h +++ b/UmaUmaChecker/ScrollbarDetector.h @@ -19,11 +19,16 @@ class ScrollbarDetector private: void InitScrollInfo(cv::Mat& img); + private: int TotalLength; int Length; - int StartY; - int EndY; + + int ScrollBarStartY; + int ScrollBarEndY; + + int MinY; + int MaxY; }; From c19329ce8a3ed218d2ff00d5e65e5444d677ce72 Mon Sep 17 00:00:00 2001 From: Cilda Date: Thu, 6 Jul 2023 21:12:53 +0900 Subject: [PATCH 2/4] =?UTF-8?q?[fix]=20=E3=82=B9=E3=82=AF=E3=83=AD?= =?UTF-8?q?=E3=83=BC=E3=83=AB=E3=83=90=E3=83=BC=E6=A4=9C=E5=87=BA=E5=87=A6?= =?UTF-8?q?=E7=90=86=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit スクロールバーを正しく検出しないことがあったので修正 --- UmaUmaChecker/ScrollbarDetector.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/UmaUmaChecker/ScrollbarDetector.cpp b/UmaUmaChecker/ScrollbarDetector.cpp index 52e6f99..160c7bd 100644 --- a/UmaUmaChecker/ScrollbarDetector.cpp +++ b/UmaUmaChecker/ScrollbarDetector.cpp @@ -66,9 +66,11 @@ void ScrollbarDetector::InitScrollInfo(cv::Mat& img) } } + if (MinY == -1 || MaxY == -1) return; + TotalLength = MaxY - MinY; - for (int y = MinY; y < MaxY; y++) { + for (int y = MinY; y <= MaxY; y++) { cv::uint8_t c = 0; for (int x = 0; x < bar.size().width && c != 255; x++) c = bar.at(y, x); @@ -77,7 +79,7 @@ void ScrollbarDetector::InitScrollInfo(cv::Mat& img) ScrollBarStartY = y; } else if (c == 0 && ScrollBarStartY != -1 && ScrollBarEndY == -1) { - ScrollBarEndY = y - 1; + ScrollBarEndY = y; break; } } From 4504906afca87b5be9d7494106a8c8a07e7ad3b3 Mon Sep 17 00:00:00 2001 From: Cilda Date: Mon, 10 Jul 2023 20:02:43 +0900 Subject: [PATCH 3/4] =?UTF-8?q?[update]=20=E7=B5=90=E5=90=88=E5=87=A6?= =?UTF-8?q?=E7=90=86=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit スクロールバー検出処理の改善 エラーメッセージを分かりやすく --- UmaUmaChecker/CombineImage.cpp | 13 ++++++++----- UmaUmaChecker/CombineImage.h | 6 +++++- UmaUmaChecker/MainFrame.cpp | 4 +++- UmaUmaChecker/ScrollbarDetector.cpp | 20 +++++++++++++------- UmaUmaChecker/ScrollbarDetector.h | 3 +++ 5 files changed, 32 insertions(+), 14 deletions(-) diff --git a/UmaUmaChecker/CombineImage.cpp b/UmaUmaChecker/CombineImage.cpp index 5c3b93d..4ba8ed1 100644 --- a/UmaUmaChecker/CombineImage.cpp +++ b/UmaUmaChecker/CombineImage.cpp @@ -56,6 +56,7 @@ void CombineImage::StartCapture() Images.clear(); DetectedYLines.clear(); BarLength = 0; + Error = L""; LOG_DEBUG << L"[CombineImage::StartCapture] ウマ娘詳細結合開始"; @@ -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; } @@ -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; @@ -175,14 +177,14 @@ void CombineImage::Capture() 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(); + _EndCapture(L"ウィンドウサイズが変更されました。"); delete image; return; } 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(); + _EndCapture(L"スクロールバーの大きさが変わっています。"); } delete image; return; @@ -249,6 +251,7 @@ void CombineImage::Capture() DetectedYLines.emplace_back(0, y); IsFirstScan = false; CurrentScrollPos = scroll.GetPos(); + PrevImage = mat.clone(); } } } diff --git a/UmaUmaChecker/CombineImage.h b/UmaUmaChecker/CombineImage.h index d567c89..cd6d3ae 100644 --- a/UmaUmaChecker/CombineImage.h +++ b/UmaUmaChecker/CombineImage.h @@ -2,6 +2,7 @@ #include #include +#include struct CombineImageInfo { int StartY; @@ -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(); @@ -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); @@ -61,6 +63,8 @@ class CombineImage bool IsManualStop; bool IsSavedImage; + std::wstring Error; + std::vector DetectedYLines; std::vector Images; diff --git a/UmaUmaChecker/MainFrame.cpp b/UmaUmaChecker/MainFrame.cpp index 1ea757e..3ab6c3b 100644 --- a/UmaUmaChecker/MainFrame.cpp +++ b/UmaUmaChecker/MainFrame.cpp @@ -342,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); } } } diff --git a/UmaUmaChecker/ScrollbarDetector.cpp b/UmaUmaChecker/ScrollbarDetector.cpp index 160c7bd..63ea472 100644 --- a/UmaUmaChecker/ScrollbarDetector.cpp +++ b/UmaUmaChecker/ScrollbarDetector.cpp @@ -4,7 +4,7 @@ #include -ScrollbarDetector::ScrollbarDetector(cv::Mat& img) +ScrollbarDetector::ScrollbarDetector(cv::Mat& img) : valid(false) { InitScrollInfo(img); } @@ -13,34 +13,39 @@ ScrollbarDetector::~ScrollbarDetector() { } +bool ScrollbarDetector::IsValid() const +{ + return valid; +} + int ScrollbarDetector::GetPos() const { - return ScrollBarStartY; + 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 ScrollBarStartY == MinY; + return valid && ScrollBarStartY == MinY; } bool ScrollbarDetector::IsEnd() const { - return ScrollBarEndY == MaxY; + return valid && ScrollBarEndY == MaxY; } void ScrollbarDetector::InitScrollInfo(cv::Mat& img) @@ -91,4 +96,5 @@ void ScrollbarDetector::InitScrollInfo(cv::Mat& img) if (ScrollBarEndY == -1) ScrollBarEndY = MaxY; Length = ScrollBarEndY - ScrollBarStartY; + valid = true; } diff --git a/UmaUmaChecker/ScrollbarDetector.h b/UmaUmaChecker/ScrollbarDetector.h index 28299b3..62c5d65 100644 --- a/UmaUmaChecker/ScrollbarDetector.h +++ b/UmaUmaChecker/ScrollbarDetector.h @@ -10,6 +10,7 @@ class ScrollbarDetector ScrollbarDetector(cv::Mat& img); ~ScrollbarDetector(); + bool IsValid() const; int GetPos() const; int GetTotalLength() const; int GetBarLength() const; @@ -22,6 +23,8 @@ class ScrollbarDetector private: + bool valid; + int TotalLength; int Length; From ff9619bd2bb90d887d5a22775bf15d6e2d28b48c Mon Sep 17 00:00:00 2001 From: Cilda Date: Wed, 12 Jul 2023 21:59:35 +0900 Subject: [PATCH 4/4] =?UTF-8?q?[update]=20=E3=82=B9=E3=82=AF=E3=83=AD?= =?UTF-8?q?=E3=83=BC=E3=83=AB=E3=83=90=E3=83=BC=E6=A4=9C=E5=87=BA=E5=87=A6?= =?UTF-8?q?=E7=90=86=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit スクロールバー終了位置のオフセットを-1するように GetMargin関数追加 --- UmaUmaChecker/Point.h | 19 ++++++++++ UmaUmaChecker/ScrollbarDetector.cpp | 42 ++++++++++++++++++++- UmaUmaChecker/ScrollbarDetector.h | 9 +++++ UmaUmaChecker/UmaUmaChecker.vcxproj | 1 + UmaUmaChecker/UmaUmaChecker.vcxproj.filters | 3 ++ 5 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 UmaUmaChecker/Point.h diff --git a/UmaUmaChecker/Point.h b/UmaUmaChecker/Point.h new file mode 100644 index 0000000..30b74b7 --- /dev/null +++ b/UmaUmaChecker/Point.h @@ -0,0 +1,19 @@ +#pragma once + +template +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_; +}; + diff --git a/UmaUmaChecker/ScrollbarDetector.cpp b/UmaUmaChecker/ScrollbarDetector.cpp index 63ea472..58c148e 100644 --- a/UmaUmaChecker/ScrollbarDetector.cpp +++ b/UmaUmaChecker/ScrollbarDetector.cpp @@ -1,9 +1,13 @@ #include "ScrollbarDetector.h" +#include #include +Point ScrollbarDetector::Start(0.967, 0.08); +Point ScrollbarDetector::End(0.967, 1.0 - 0.08); + ScrollbarDetector::ScrollbarDetector(cv::Mat& img) : valid(false) { InitScrollInfo(img); @@ -84,7 +88,7 @@ void ScrollbarDetector::InitScrollInfo(cv::Mat& img) ScrollBarStartY = y; } else if (c == 0 && ScrollBarStartY != -1 && ScrollBarEndY == -1) { - ScrollBarEndY = y; + ScrollBarEndY = y - 1; break; } } @@ -98,3 +102,39 @@ void ScrollbarDetector::InitScrollInfo(cv::Mat& img) Length = ScrollBarEndY - ScrollBarStartY; valid = true; } + +std::list> ScrollbarDetector::GetMargin(cv::Mat& img) +{ + auto start = Point(img.size().width * Start.x(), img.size().height * Start.y()); + auto end = Point(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 pos_start; + Point pos_end; + + for (int y = start.y(); y < end.y(); y++) { + auto c = scr.at(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; + } + + return { + pos_start, + pos_end + }; +} diff --git a/UmaUmaChecker/ScrollbarDetector.h b/UmaUmaChecker/ScrollbarDetector.h index 62c5d65..1842295 100644 --- a/UmaUmaChecker/ScrollbarDetector.h +++ b/UmaUmaChecker/ScrollbarDetector.h @@ -1,5 +1,9 @@ #pragma once +#include + +#include "Point.h" + namespace cv { class Mat; } @@ -20,7 +24,12 @@ class ScrollbarDetector private: void InitScrollInfo(cv::Mat& img); + + std::list> GetMargin(cv::Mat& img); +private: + static Point Start; + static Point End; private: bool valid; diff --git a/UmaUmaChecker/UmaUmaChecker.vcxproj b/UmaUmaChecker/UmaUmaChecker.vcxproj index 2f479bf..f7aed32 100644 --- a/UmaUmaChecker/UmaUmaChecker.vcxproj +++ b/UmaUmaChecker/UmaUmaChecker.vcxproj @@ -273,6 +273,7 @@ + diff --git a/UmaUmaChecker/UmaUmaChecker.vcxproj.filters b/UmaUmaChecker/UmaUmaChecker.vcxproj.filters index c477dc4..13d0203 100644 --- a/UmaUmaChecker/UmaUmaChecker.vcxproj.filters +++ b/UmaUmaChecker/UmaUmaChecker.vcxproj.filters @@ -227,6 +227,9 @@ ヘッダー ファイル + + ヘッダー ファイル +