From 29e1ff07547ac80a3594e6f8638fdf8e1a681dec Mon Sep 17 00:00:00 2001 From: WarmUpTill <19472752+WarmUpTill@users.noreply.github.com> Date: Sat, 21 Mar 2026 23:14:10 +0100 Subject: [PATCH] Adjust video condition temp vars --- data/locale/en-US.ini | 2 ++ plugins/video/macro-condition-video.cpp | 26 ++++++++++++++++------- plugins/video/opencv-helpers.cpp | 28 ++++++++++++------------- 3 files changed, 35 insertions(+), 21 deletions(-) diff --git a/data/locale/en-US.ini b/data/locale/en-US.ini index b02b8b5c..6d842c2f 100644 --- a/data/locale/en-US.ini +++ b/data/locale/en-US.ini @@ -2251,6 +2251,8 @@ AdvSceneSwitcher.tempVar.video.text="OCR text" AdvSceneSwitcher.tempVar.video.text.description="The text detected in a given video input frame." AdvSceneSwitcher.tempVar.video.color="Average color" AdvSceneSwitcher.tempVar.video.color.description="The average RGB color in a given video input frame in HexArgb format." +AdvSceneSwitcher.tempVar.video.dominantColor="Dominant color" +AdvSceneSwitcher.tempVar.video.dominantColor.description="The most dominant RGB color in a given video input frame in HexArgb format.\nDetermined using k-means clustering, which groups similar colors and returns the center of the largest group.\nDue to the nature of the algorithm the result may vary slightly between evaluations." AdvSceneSwitcher.tempVar.websocket.message="Received websocket message" AdvSceneSwitcher.tempVar.websocket.message.description="The received websocket message, which matched the given pattern" diff --git a/plugins/video/macro-condition-video.cpp b/plugins/video/macro-condition-video.cpp index 04746a5f..adb3db8b 100644 --- a/plugins/video/macro-condition-video.cpp +++ b/plugins/video/macro-condition-video.cpp @@ -377,13 +377,19 @@ bool MacroConditionVideo::CheckColor() _screenshotData.GetImage(), _colorParameters.color, _colorParameters.colorThreshold, _colorParameters.matchThreshold); - // Way too slow for now - //SetTempVarValue("dominantColor", GetDominantColor(_screenshotData.image, 3) - // .name(QColor::HexArgb) - // .toStdString()); - SetTempVarValue("color", GetAverageColor(_screenshotData.GetImage()) - .name(QColor::HexArgb) - .toStdString()); + + SetTempVarValue("color", [&]() { + return GetAverageColor(_screenshotData.GetImage()) + .name(QColor::HexArgb) + .toStdString(); + }); + + SetTempVarValue("dominantColor", [&]() { + return GetDominantColor(_screenshotData.GetImage(), 3) + .name(QColor::HexArgb) + .toStdString(); + }); + return ret; } @@ -479,6 +485,12 @@ void MacroConditionVideo::SetupTempVars() obs_module_text("AdvSceneSwitcher.tempVar.video.color"), obs_module_text( "AdvSceneSwitcher.tempVar.video.color.description")); + AddTempvar( + "dominantColor", + obs_module_text( + "AdvSceneSwitcher.tempVar.video.dominantColor"), + obs_module_text( + "AdvSceneSwitcher.tempVar.video.dominantColor.description")); break; case VideoCondition::MATCH: case VideoCondition::DIFFER: diff --git a/plugins/video/opencv-helpers.cpp b/plugins/video/opencv-helpers.cpp index df0f1f10..bd5ac933 100644 --- a/plugins/video/opencv-helpers.cpp +++ b/plugins/video/opencv-helpers.cpp @@ -286,9 +286,9 @@ QColor GetAverageColor(const QImage &img) auto image = QImageToMat(img); cv::Scalar meanColor = cv::mean(image); - int averageBlue = cvRound(meanColor[0]); + int averageRed = cvRound(meanColor[0]); int averageGreen = cvRound(meanColor[1]); - int averageRed = cvRound(meanColor[2]); + int averageBlue = cvRound(meanColor[2]); return QColor(averageRed, averageGreen, averageBlue); } @@ -309,8 +309,8 @@ QColor GetDominantColor(const QImage &img, int k) cv::TermCriteria criteria( cv::TermCriteria::EPS + cv::TermCriteria::MAX_ITER, 100, 0.2); cv::Mat labels, centers; - cv::kmeans(reshapedImage, k, labels, criteria, 1, - cv::KMEANS_RANDOM_CENTERS, centers); + cv::kmeans(reshapedImage, k, labels, criteria, 3, cv::KMEANS_PP_CENTERS, + centers); // Find the dominant color // Center of the cluster with the largest number of pixels @@ -321,16 +321,16 @@ QColor GetDominantColor(const QImage &img, int k) cv::Point max_loc; cv::minMaxLoc(counts, nullptr, nullptr, nullptr, &max_loc); - try { - cv::Scalar dominantColor = centers.at(max_loc.y); - const int blue = cv::saturate_cast(dominantColor.val[0]); - const int green = cv::saturate_cast(dominantColor.val[1]); - const int red = cv::saturate_cast(dominantColor.val[2]); - const int alpha = cv::saturate_cast(dominantColor.val[3]); - return QColor(red, green, blue, alpha); - } catch (...) { - } - return QColor(); + const int clusterIdx = max_loc.x; + const int red = + cv::saturate_cast(centers.at(clusterIdx, 0)); + const int green = + cv::saturate_cast(centers.at(clusterIdx, 1)); + const int blue = + cv::saturate_cast(centers.at(clusterIdx, 2)); + const int alpha = + cv::saturate_cast(centers.at(clusterIdx, 3)); + return QColor(red, green, blue, alpha); } // Assumption is that QImage uses Format_RGBA8888.