opencv直方图规定化库函数
时间: 2023-10-28 19:06:55 浏览: 52
opencv直方图规定化库函数是equalizeHist,它用于提高图像的质量。该函数的函数原型如下:
void equalizeHist(InputArray src, OutputArray dst)
示例代码如下:
```cpp
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int artc, char** argv) {
Mat src = imread("C:/Users/Dell/Desktop/picture/butterfly.jpg");
if (src.empty()) {
printf("could not load image...\n");
return -1;
}
// 灰度直方图规定化
Mat gray, dst;
cvtColor(src, gray, COLOR_BGR2GRAY);
equalizeHist(gray, dst);
// 输出结果
namedWindow("input", WINDOW_AUTOSIZE);
imshow("input", src);
imshow("equalizeHist", dst);
waitKey(0);
return 0;
}
```
相关问题
根据输入的灰度图像,分别计算图像的均值、方差等统计特征,并计算图像的直方图特征并以图形方式显示图像的直方图(用C或C++语言实现)不能用opencv和其他现成的库函数操作;。
好的,如果不能使用OpenCV等图像处理库函数,那么可以使用C或C++语言自己编写代码实现。以下是一种基本的实现方法:
```c++
#include <iostream>
#include <cmath>
const int MAX_VALUE = 255; // 灰度值的最大值
// 计算图像的均值和方差
void calc_mean_and_stddev(unsigned char* data, int width, int height, double& mean, double& stddev) {
double sum = 0, sum_sq = 0;
int count = width * height;
for (int i = 0; i < count; ++i) {
sum += data[i];
sum_sq += data[i] * data[i];
}
mean = sum / count;
stddev = sqrt((sum_sq / count) - (mean * mean));
}
// 计算图像的直方图
void calc_histogram(unsigned char* data, int width, int height, int* hist) {
int count = width * height;
for (int i = 0; i < count; ++i) {
hist[data[i]]++;
}
}
// 显示直方图
void show_histogram(int* hist) {
int max_count = 0;
for (int i = 0; i <= MAX_VALUE; ++i) {
if (hist[i] > max_count) {
max_count = hist[i];
}
}
for (int i = 0; i <= MAX_VALUE; ++i) {
int count = hist[i];
int height = (int)(((double)count / max_count) * 50); // 调整显示高度
std::cout << i << ": ";
for (int j = 0; j < height; ++j) {
std::cout << "*";
}
std::cout << std::endl;
}
}
int main() {
unsigned char data[] = {
10, 20, 30, 40, 50,
60, 70, 80, 90, 100,
110, 120, 130, 140, 150,
160, 170, 180, 190, 200,
210, 220, 230, 240, 250
};
int width = 5;
int height = 5;
double mean, stddev;
calc_mean_and_stddev(data, width, height, mean, stddev);
std::cout << "Mean: " << mean << std::endl;
std::cout << "Standard Deviation: " << stddev << std::endl;
int hist[MAX_VALUE + 1] = {0};
calc_histogram(data, width, height, hist);
show_histogram(hist);
return 0;
}
```
以上代码演示了如何计算图像的均值和方差,以及如何计算和显示图像的直方图。由于没有使用OpenCV等库函数,因此需要手动读取图像数据并进行处理。在这个例子中,我们使用了一个5x5的灰度图像来演示代码的功能。您可以根据需要修改图像大小和值,以及调整直方图的显示高度等参数。希望这个例子能够帮助您实现自己的代码!
根据输入的灰度图像,分别计算图像的均值、方差等统计特征,并计算图像的直方图特征并以图形方式显示图像的直方图(用C或C++语言实现)用C或C++语言写出代码,不用opencv以及其他现有的库函数
好的,以下是用C++实现的代码:
```c++
#include <iostream>
#include <fstream>
using namespace std;
const int MAX_GRAY_VALUE = 255; // 灰度最大值
const int HISTOGRAM_SIZE = 256; // 直方图大小
// 计算图像的均值
double getMean(int** image, int height, int width) {
double sum = 0.0;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
sum += image[i][j];
}
}
return sum / (height * width);
}
// 计算图像的方差
double getVariance(int** image, int height, int width, double mean) {
double sum = 0.0;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
sum += (image[i][j] - mean) * (image[i][j] - mean);
}
}
return sum / (height * width);
}
// 计算图像的直方图
void getHistogram(int** image, int height, int width, int* histogram) {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
histogram[image[i][j]]++;
}
}
}
// 显示直方图
void displayHistogram(int* histogram) {
ofstream fout("histogram.txt");
for (int i = 0; i < HISTOGRAM_SIZE; i++) {
fout << i << " " << histogram[i] << endl;
}
fout.close();
}
int main() {
int height = 512; // 图像高度
int width = 512; // 图像宽度
// 生成随机图像
int** image = new int*[height];
for (int i = 0; i < height; i++) {
image[i] = new int[width];
for (int j = 0; j < width; j++) {
image[i][j] = rand() % (MAX_GRAY_VALUE + 1);
}
}
// 计算均值和方差
double mean = getMean(image, height, width);
double variance = getVariance(image, height, width, mean);
// 计算直方图
int histogram[HISTOGRAM_SIZE] = { 0 };
getHistogram(image, height, width, histogram);
// 显示直方图
displayHistogram(histogram);
// 释放内存
for (int i = 0; i < height; i++) {
delete[] image[i];
}
delete[] image;
return 0;
}
```
代码中用到了一个随机生成灰度图像的函数,这里就不贴出来了。运行程序后,会在程序所在目录下生成一个名为`histogram.txt`的文件,里面存储了直方图数据,可以用其他软件或代码读取并显示。