我有一些 C 代码示例。我需要进行直方图均衡。但是,我还需要一步步前进。我被困在第一步了。第一步是将文件从 RGB 转换为 YCbCr。所以,我将与您分享代码。我拥有的所有代码都不适合这些区域。另外,我还添加了一张显示我失败的图片。我想知道我哪里错了。我希望有人能给我带来光明。错误消息显示“在需要浮点值的地方使用了指针值”。这则消息意味着什么?
`
#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;
};
// The codes that I've begin from here.
/*struct RGB // In fact, this structer is not a comment. I changed it.
{
unsigned char R;
unsigned char G;
unsigned char B;
};*/
struct YCbCr
{
float Y;
float Cb;
float Cr;
};
struct YCbCr RGBToYCbCr(struct ppm_file rgb) {
float fr = (float)rgb.rdata / 255;
float fg = (float)rgb.gdata / 255;
float fb = (float)rgb.bdata / 255;
struct YCbCr ycbcr;
ycbcr.Y = (float)(0.2989 * fr + 0.5866 * fg + 0.1145 * fb);
ycbcr.Cb = (float)(-0.1687 * fr - 0.3313 * fg + 0.5000 * fb);
ycbcr.Cr = (float)(0.5000 * fr - 0.4184 * fg - 0.0816 * fb);
return ycbcr;
}
// The codes that I added end here.
void get_image_data(char *filename,struct ppm_file *image);
void write_image(char *filename,struct ppm_file *image);
// I do not have the enough space for the get_image_data and the write_image functions implementation.
// If I will a solution for the space, I'll add the functions.
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;
}
`
最佳答案
“错误消息显示“在需要浮点值的地方使用了指针值”。此消息意味着什么?”
该消息的意思与它所说的完全一样。您使用了编译器期望浮点值的指针值。
根据你的定义
struct ppm_file
{
struct ppm_header *pheader;
unsigned char *rdata,*gdata,*bdata;
};
ppm_file.rdata、ppm_file.gdata 和 ppm_file.bdata 是无符号字符指针。
但是你这样做:
float fr = (float)rgb.rdata / 255;
float fg = (float)rgb.gdata / 255;
float fb = (float)rgb.bdata / 255;
因此,您尝试将 unsigned char 指针转换为 float,将其除以 255 并将其分配给 float 变量。这是行不通的。
首先,您必须取消引用 unsigned char 指针。然后可以将生成的 unsigned char 值转换为浮点值,然后您可以按计划使用该值。
float fr = (float) *rgb.rdata / 255;
应该可以解决问题。
在继续之前,请确保您对 C 及其类型有更好的了解。有很多好书和教程可供使用。
关于在 C__ 中将 PPM 从 RGB 转换为 HSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43705908/