您好,登錄后才能下訂單哦!
OpenCV是一個開源的計算機視覺庫,它提供了大量的圖像處理功能。在C++中使用OpenCV進行圖像閾值處理時,可以利用其內(nèi)置的函數(shù)來實現(xiàn)多種閾值處理技巧。
#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進行不同類型的圖像閾值處理。你可以根據(jù)需要選擇合適的閾值處理技巧。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。