您好,登錄后才能下訂單哦!
OpenCV是一個開源的計算機視覺庫,它提供了大量的圖像處理功能。在C++中使用OpenCV進行圖像閾值處理時,可以利用其內置的函數來實現多種閾值處理技巧。
#include <opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main() {
Mat src = imread("input.jpg", IMREAD_GRAYSCALE);
Mat dst;
double threshold_value = 128;
double max_value = 255;
int threshold_type = THRESH_BINARY;
threshold(src, dst, threshold_value, max_value, threshold_type);
imwrite("output.jpg", dst);
return 0;
}
#include <opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main() {
Mat src = imread("input.jpg", IMREAD_GRAYSCALE);
Mat dst;
int block_size = 25;
double constant = 5;
int threshold_type = THRESH_BINARY;
adaptiveThreshold(src, dst, 255, ADAPTIVE_THRESH_MEAN_C, threshold_type, block_size, constant);
imwrite("output.jpg", dst);
return 0;
}
#include <opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main() {
Mat src = imread("input.jpg", IMREAD_GRAYSCALE);
Mat dst;
double threshold_value = 0;
double max_value = 255;
int threshold_type = THRESH_BINARY | THRESH_OTSU;
threshold(src, dst, threshold_value, max_value, threshold_type);
imwrite("output.jpg", dst);
return 0;
}
#include <opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
double triangleThreshold(Mat& src) {
int hist[256] = {0};
for (int i = 0; i < src.rows; i++) {
for (int j = 0; j < src.cols; j++) {
hist[src.at<uchar>(i, j)]++;
}
}
int total = src.rows * src.cols;
int accumulator[256] = {0};
for (int i = 0; i < 256; i++) {
accumulator[i] = hist[i] + (i == 0 ? 0 : accumulator[i - 1]);
}
double max_dist = 0;
int threshold = 0;
for (int i = 0; i < 256; i++) {
if (accumulator[i] == 0 || accumulator[i] == total) continue;
double dist = (double)(total - accumulator[i]) * i - (double)accumulator[i] * (255 - i);
if (dist > max_dist) {
max_dist = dist;
threshold = i;
}
}
return threshold;
}
int main() {
Mat src = imread("input.jpg", IMREAD_GRAYSCALE);
Mat dst;
double threshold_value = triangleThreshold(src);
double max_value = 255;
int threshold_type = THRESH_BINARY;
threshold(src, dst, threshold_value, max_value, threshold_type);
imwrite("output.jpg", dst);
return 0;
}
這些示例展示了如何在C++中使用OpenCV進行不同類型的圖像閾值處理。你可以根據需要選擇合適的閾值處理技巧。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。