我的学术课程需要RGB图像文件中的直方图均衡帮助。
我检查了我之前关于直方图均衡的代码示例,但我没有找到任何有关此问题的线索。我从未练习过直方图均衡示例,即RGB图像。
图像是PPM文件。因此,我们需要将文件从RGB转换为YCbCr,从RGB转换为HSI。
然后,我们需要在图像为YCbCr和HSI格式时进行直方图均衡。
之后,我们需要再次将PPM文件转换为RGB格式。而已。
*void write_image function is writing the data to the pnr.ppm*
*void get_image_data function is getting the image that is mandrill1.ppm*
我们只需要指定代码:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<ctype.h>
#include<string.h>
#include <fcntl.h>
#include <malloc.h>
#include <math.h>
#define PI 3.1415926535897932384626433832795
struct ppm_header
{
char pgmtype1;
char pgmtype2;
int pwidth;
int pheight;
int pmax;
};
struct ppm_file
{
struct ppm_header *pheader;
unsigned char *rdata,*gdata,*bdata;
};
void get_image_data(char *filename,struct ppm_file *image);
void write_image(char *filename,struct ppm_file *image);
main()
{
struct ppm_file resim;
get_image_data("mandrill1.ppm",&resim);
printf("pgmtype...=%c%c\n",resim.pheader->pgmtype1,resim.pheader->pgmtype2);
printf("width...=%d\n",resim.pheader->pwidth);
printf("height...=%d\n",resim.pheader->pheight);
printf("max gray level...=%d\n",resim.pheader->pmax);
write_image("pnr.ppm",&resim);
return 0;
}
void write_image(char *filename,struct ppm_file *image)
{
FILE *fp;
int i,max=0;
fp=fopen(filename,"wb");
fputc(image->pheader->pgmtype1,fp);
fputc(image->pheader->pgmtype2,fp);
fputc('\n',fp);
fprintf(fp,"%d %d\n",image->pheader->pwidth,image->pheader->pheight);
fprintf(fp,"%d\n",255/*max*/);
for(i=0;i<image->pheader->pwidth*image->pheader->pheight;i++)
{
fwrite(&image->rdata[i],1,1,fp);
fwrite(&image->gdata[i],1,1,fp);
fwrite(&image->bdata[i],1,1,fp);
}
fclose(fp);
}
void get_image_data(char *filename, struct ppm_file *image )
{
FILE* fp;
int i=0;
char temp[256];
image->pheader=(struct ppm_header *)malloc(sizeof(struct ppm_header));
fp = fopen(filename, "rb" );
if (fp==NULL)
{
printf("Dosya acilamadi: %s.\n\n", filename);
exit(1);
}
printf ("Okunan PPM dosyasi : %s...\n", filename);
fscanf (fp, "%s", temp);
if (strcmp(temp, "P6") == 0)
{
image->pheader->pgmtype1=temp[0];
image->pheader->pgmtype2=temp[1];
fscanf (fp, "%s", temp);
if (temp[0]=='#')
{
while(fgetc(fp)!='\n');
fscanf (fp, "%d %d\n",&image->pheader->pwidth,&image->pheader->pheight);
fscanf (fp, "%d\n", &image->pheader->pmax);
}
else
{
sscanf (temp, "%d", &image->pheader->pwidth);
fscanf (fp, "%d", &image->pheader->pheight);
fscanf (fp, "%d", &image->pheader->pmax);
}
image->rdata=(unsigned char *)malloc(image->pheader->pheight*image->pheader->pwidth*sizeof(unsigned char));
image->gdata=(unsigned char *)malloc(image->pheader->pheight*image->pheader->pwidth*sizeof(unsigned char));
image->bdata=(unsigned char *)malloc(image->pheader->pheight*image->pheader->pwidth*sizeof(unsigned char));
if (image->rdata==NULL) printf("bellek problemi...\n");
for(i=0;i<image->pheader->pwidth*image->pheader->pheight;i++)
{
fread(&image->rdata[i],1,1,fp);
fread(&image->gdata[i],1,1,fp);
fread(&image->bdata[i],1,1,fp);
}
}
else
{
printf ("\nHata Resim dosyasi PGM P6 formatinda degil");
exit(1);
}
fclose(fp);
}