实现灰度图像和彩色图像的迭代阈值分割算法opencv c++代码实现
时间: 2024-05-15 12:12:36 浏览: 116
以下是基于OpenCV的灰度图像和彩色图像迭代阈值分割算法的C++代码实现:
灰度图像迭代阈值分割:
```cpp
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
double thresholdValue(Mat& img) {
double T = 0;
double T_last = -1;
int rows = img.rows;
int cols = img.cols;
int total = rows * cols;
while(abs(T - T_last) > 1) {
double sum1 = 0, sum2 = 0, count1 = 0, count2 = 0;
for(int i = 0; i < rows; ++i) {
for(int j = 0; j < cols; ++j) {
if(img.at<uchar>(i, j) > T) {
sum1 += img.at<uchar>(i, j);
count1++;
} else {
sum2 += img.at<uchar>(i, j);
count2++;
}
}
}
T_last = T;
T = (sum1 / count1 + sum2 / count2) / 2;
}
return T;
}
int main() {
Mat img = imread("lena.jpg", IMREAD_GRAYSCALE);
if(img.empty()) {
cout << "Could not read the image" << endl;
return -1;
}
double T = thresholdValue(img);
Mat dst;
threshold(img, dst, T, 255, THRESH_BINARY);
namedWindow("Original Image", WINDOW_NORMAL);
namedWindow("Thresholded Image", WINDOW_NORMAL);
imshow("Original Image", img);
imshow("Thresholded Image", dst);
waitKey(0);
return 0;
}
```
彩色图像迭代阈值分割:
```cpp
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
double thresholdValue(Mat& img) {
double T = 0;
double T_last = -1;
int rows = img.rows;
int cols = img.cols;
int total = rows * cols;
while(abs(T - T_last) > 1) {
double sum1 = 0, sum2 = 0, count1 = 0, count2 = 0;
for(int i = 0; i < rows; ++i) {
for(int j = 0; j < cols; ++j) {
Vec3b pixel = img.at<Vec3b>(i, j);
int gray = (pixel[0] + pixel[1] + pixel[2]) / 3;
if(gray > T) {
sum1 += gray;
count1++;
} else {
sum2 += gray;
count2++;
}
}
}
T_last = T;
T = (sum1 / count1 + sum2 / count2) / 2;
}
return T;
}
int main() {
Mat img = imread("lena.jpg");
if(img.empty()) {
cout << "Could not read the image" << endl;
return -1;
}
double T = thresholdValue(img);
Mat dst;
cvtColor(img, dst, COLOR_BGR2GRAY);
threshold(dst, dst, T, 255, THRESH_BINARY);
namedWindow("Original Image", WINDOW_NORMAL);
namedWindow("Thresholded Image", WINDOW_NORMAL);
imshow("Original Image", img);
imshow("Thresholded Image", dst);
waitKey(0);
return 0;
}
```
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)