【发布时间】:2017-09-25 19:31:07
【问题描述】:
我需要有关 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);
}
【问题讨论】:
-
恐怕你的问题是乞求被否决和关闭。请消除卑躬屈膝,并将您的问题范围缩小到可管理的范围内。
-
即使是 PPM 阅读器功能 (
get_image_data()) 也不正确。您确定自己应该通过课程,但没有达到课程的要求吗?我不是。 -
a) 如果我们帮助您,我们就不会互相帮助,因为您没有为我们做任何事情。 b)如果这会影响您的期末考试,我真的很想知道您最初是如何做到这一点的,因为您的帖子质量和风格令人难以置信。 c) 你甚至没有提出任何问题或提出你遇到的问题。你刚刚说了你的任务是什么并发布了一些代码。你希望我们用它做什么?你宁愿在生活中赚钱。不要乞求他们......
-
你们会坐在一起作为一个团队解决这个问题,而不是在家庭作业中发送垃圾邮件吗?现在的学生有什么问题? stackoverflow.com/questions/43667345/…stackoverflow.com/questions/43660624/…
-
@raskolnikov:我不确定是否可以帮助您。你看,你来这里是为了找到一个简单的解决方案,它可以让你以最小的努力通过。从我的立场来看,这将是最糟糕的结果。再过一段时间,你就会成为一个自称是程序员的人,在我看来,这个领域已经被不合逻辑、不理性、懒惰、缺乏创造力的非思考者所淹没;在我们的文化中,人们比癌症更糟糕。如果你在这里失败了,你实际上可能不得不重新考虑你的生活策略!这可能是这里最好的结果。
标签: c image-processing histogram ppm