C二维数组输出为灰度图
时间: 2024-06-02 21:07:41 浏览: 16
要输出C语言中的二维数组为灰度图,你需要用到图像处理库,比如OpenCV。具体步骤如下:
1. 加载图像:使用OpenCV的imread函数读取图片,并将其转换为灰度图像(如果原始图像不是灰度图)。
2. 创建二维数组:根据灰度图像的尺寸,创建一个二维数组。
3. 遍历像素:使用OpenCV的Mat对象中的at方法遍历每个像素,并将其灰度值存储到二维数组中。
4. 输出灰度图:使用OpenCV的imwrite函数将二维数组转换为灰度图像。
注意:在将像素值存储到数组中时,需要注意图像的行列顺序,以及像素值与数组索引的对应关系。
希望这些信息能够帮助你解决问题,如果你有任何其他问题,请随时问我。
相关问题
用c语言实现一个对一个BMP灰度图片文件读取,将它保存到一个二维数组里面
#include <stdio.h>
#include <stdlib.h>
// 定义BMP文件头结构体
typedef struct {
unsigned short bfType; // BMP文件类型,必须是0x424D,即字符“BM”
unsigned int bfSize; // BMP文件大小
unsigned short bfReserved1; // 保留字段1
unsigned short bfReserved2; // 保留字段2
unsigned int bfOffBits; // BMP文件数据偏移量,即文件头+位图信息头大小
} __attribute__((packed)) BMPFileHeader;
// 定义BMP位图信息头结构体
typedef struct {
unsigned int biSize; // 位图信息头大小,即40字节
int biWidth; // 位图宽度
int biHeight; // 位图高度,正数表示正向,负数表示倒置
unsigned short biPlanes; // 总是为1
unsigned short biBitCount; // 位图颜色位数,即每个像素占用位数
unsigned int biCompression; // 采用的压缩方法,0为不压缩
unsigned int biSizeImage; // 数据区大小,0表示没有压缩
int biXPelsPerMeter; // 水平分辨率
int biYPelsPerMeter; // 垂直分辨率
unsigned int biClrUsed; // 颜色表中实际使用的颜色数,0表示使用全部颜色
unsigned int biClrImportant;// 针对重要的颜色数,0表示所有颜色都重要
} __attribute__((packed)) BMPInfoHeader;
int main() {
// 打开BMP文件
FILE *fp = fopen("test.bmp", "rb");
if (fp == NULL) {
printf("Failed to open file!\n");
exit(1);
}
// 读取BMP文件头
BMPFileHeader bmpFileHeader;
fread(&bmpFileHeader, sizeof(BMPFileHeader), 1, fp);
// 判断是否为BMP文件
if (bmpFileHeader.bfType != 0x4D42) {
printf("This is not a BMP file!\n");
fclose(fp);
exit(2);
}
// 读取BMP位图信息头
BMPInfoHeader bmpInfoHeader;
fread(&bmpInfoHeader, sizeof(BMPInfoHeader), 1, fp);
// 判断是否为灰度图像
if (bmpInfoHeader.biBitCount != 8) {
printf("This is not a grayscale BMP file!\n");
fclose(fp);
exit(3);
}
// 读取颜色表
unsigned char colorTable[1024];
fread(colorTable, sizeof(unsigned char), 1024, fp);
// 分配二维数组存储图像数据
unsigned char **imageData = (unsigned char **)malloc(bmpInfoHeader.biHeight * sizeof(unsigned char *));
for (int i = 0; i < bmpInfoHeader.biHeight; i++) {
imageData[i] = (unsigned char *)malloc(bmpInfoHeader.biWidth * sizeof(unsigned char));
fread(&imageData[i][0], sizeof(unsigned char), bmpInfoHeader.biWidth, fp);
}
// 关闭文件
fclose(fp);
// 输出图像数据
for (int i = 0; i < bmpInfoHeader.biHeight; i++) {
for (int j = 0; j < bmpInfoHeader.biWidth; j++) {
printf("%d ", imageData[i][j]);
}
printf("\n");
}
// 释放内存
for (int i = 0; i < bmpInfoHeader.biHeight; i++) {
free(imageData[i]);
}
free(imageData);
return 0;
}
用大津法处理120x188的二维灰度数组用c语言实现
好的,以下是使用C语言实现大津法处理120x188的二维灰度数组的示例代码,供参考:
```c
#include <stdio.h>
#define ROWS 120
#define COLS 188
int main()
{
int image[ROWS][COLS]; // 二维灰度数组
int hist[256] = {0}; // 灰度直方图
int threshold = 0; // 大津法计算得到的阈值
int sum = 0; // 灰度总和
int sumB = 0; // 类间方差中的背景部分灰度总和
int wB = 0; // 类间方差中的背景部分像素数量
int wF = 0; // 类间方差中的前景部分像素数量
int varMax = 0; // 最大类间方差
int threshold1, threshold2; // 大津法计算过程中使用的两个阈值
// 读取灰度图像数据到二维数组中
// ...
// 计算灰度直方图
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
hist[image[i][j]]++;
}
}
// 计算灰度总和
for (int i = 0; i < 256; i++) {
sum += i * hist[i];
}
// 大津法计算阈值
for (int i = 0; i < 256; i++) {
wB += hist[i];
if (wB == 0) continue;
wF = ROWS * COLS - wB;
if (wF == 0) break;
sumB += i * hist[i];
double mB = (double) sumB / wB;
double mF = (double) (sum - sumB) / wF;
double varBetween = (double) wB * wF * (mB - mF) * (mB - mF);
if (varBetween > varMax) {
varMax = varBetween;
threshold1 = i;
}
}
threshold2 = threshold1;
while (true) {
wB = 0;
sumB = 0;
for (int i = 0; i <= threshold2; i++) {
wB += hist[i];
sumB += i * hist[i];
}
if (wB == 0) break;
wF = ROWS * COLS - wB;
if (wF == 0) break;
double mB = (double) sumB / wB;
double mF = (double) (sum - sumB) / wF;
double varBetween = (double) wB * wF * (mB - mF) * (mB - mF);
if (varBetween > varMax) {
varMax = varBetween;
threshold1 = threshold2;
}
threshold2++;
}
threshold = threshold1;
// 对图像进行二值化处理
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (image[i][j] > threshold) {
image[i][j] = 255;
} else {
image[i][j] = 0;
}
}
}
// 输出二值化后的图像数据
// ...
return 0;
}
```
这里的代码中,我们首先定义了120x188的二维灰度数组`image`和256个元素的灰度直方图数组`hist`。之后,我们通过读取图像数据计算灰度直方图,并计算灰度总和。然后,我们使用大津法计算阈值,对图像进行二值化处理,并输出处理后的图像数据。具体的大津法实现过程请参考代码中的注释。
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)