利用c语言编写出与opencv中的mean函数相同功能的程序,并且不使用mean函数 重新编写
时间: 2024-02-17 21:04:57 浏览: 139
下面是一个利用C语言编写的计算像素均值的程序,不使用OpenCV库中的mean函数,利用指针遍历像素进行计算,实现与OpenCV中的mean函数相同的功能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv )
{
if ( argc != 2 )
{
printf("usage: ./mean image_file\n");
return -1;
}
Mat image;
image = imread( argv[1], 1 );
if ( !image.data )
{
printf("No image data \n");
return -1;
}
int rows = image.rows;
int cols = image.cols;
int channels = image.channels();
unsigned char *data = image.data;
double sum = 0;
int count = 0;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
for (int k = 0; k < channels; k++)
{
sum += data[i * cols * channels + j * channels + k];
count++;
}
}
}
double mean_val = sum / count;
printf("The mean value of pixels in the image is: %f\n", mean_val);
return 0;
}
```
该程序同样需要输入一张图片的文件名作为命令行参数,读取图片并计算像素均值。代码中使用了指针遍历像素,计算出像素均值并输出,实现了与OpenCV中的mean函数相同的功能。
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.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/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.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://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)