問題:一維陣列儲存直方圖計數,需判斷有幾個峰 (局部最大值)。
峰值定義:`hist[i] > hist[i-1]` 且 `hist[i] > hist[i+1]` (嚴格) 或 `>=` (寬鬆)。
#include
#include
int countPeaks(const std::vector& hist, int threshold = 0) {
if (hist.size() < 3) return 0;
int peaks = 0;
for (size_t i = 1; i + 1 < hist.size(); ++i) {
if (hist[i] <= threshold) continue; // 忽略過小計數
bool leftUp = hist[i] > hist[i-1];
bool rightDown = hist[i] > hist[i+1];
if (leftUp && rightDown) ++peaks;
}
return peaks;
}
// 進階:允許平頂峰 (連續相等最大值視為一峰)
int countPeaksPlateau(const std::vector& hist, int threshold = 0) {
int peaks = 0;
for (size_t i = 1; i + 1 < hist.size(); ) {
if (hist[i] <= threshold) { ++i; continue; }
if (hist[i] > hist[i-1] && hist[i] >= hist[i+1]) {
++peaks;
// 跳過平頂
size_t j = i + 1;
while (j + 1 < hist.size() && hist[j] == hist[i]) ++j;
i = j;
} else {
++i;
}
}
return peaks;
}
// 一階差分
std::vector diff(hist.size()-1);
for (size_t i=0; i 0 && diff[i] < 0) ++peaks;
cv::Mat hist; // 1D histogram
std::vector peaks;
cv::findPeaks(hist, peaks, threshold, minDist); // OpenCV 4.5+
核心口訣:**一階差分正→負即峰值,加上平滑、閾值、最小峰距三大濾波器,工程可用、抗雜訊強。**