【问题标题】:Converting a PPM from RGB to HSL in C__在 C__ 中将 PPM 从 RGB 转换为 HSL
【发布时间】:2017-09-28 02:08:59
【问题描述】:

我有一些 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;
}

`

I've uploaded an image that shows my failures. I hope somebody can help me about that. That warning made me hopeless for the solution.

【问题讨论】:

  • @Piglet 你可以通过这篇文章看到我的挣扎。
  • @NominalAnimal 你可以通过这篇文章看到我的挣扎。
  • 看到这比你糟糕的上一篇文章要好得多。离How to Ask 还很远,但你走在了一个好路上。错误信息告诉你一切。您正在使用编译器期望浮点数的指针。它甚至告诉你在哪一行。您不能将 unsigned char 指针转换为浮点数。请做一个c教程
  • 您在 2 天前问了很多相同的问题。如果已关闭,请勿重新发布问题!编辑它,解决 cmets 给出的问题和关闭原因并要求重新打开!

标签: c image-processing histogram rgb ppm


【解决方案1】:

“错误消息说“指针值在预期的浮点值处使用”。这条消息是什么意思?”

该消息的含义与它所说的完全一样。您在编译器期望浮点值的地方使用了指针值。

根据你的定义

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 指针转换为浮点数,将其除以 255 并将其分配给浮点变量。那是行不通的。

首先你必须取消对 unsigned char 指针的引用。然后可以将生成的 unsigned char 值转换为浮点值,然后您可以按计划使用它。

float fr = (float) *rgb.rdata / 255;

应该可以解决问题。

在继续之前,请确保您对 C 及其类型有更好的了解。有很多好书和教程可供使用。

【讨论】:

  • 如果你还记得我的其他函数,即 write_image 和 get_image_data,那么我想添加一个函数,它使用这些结构将文件从 RGB 转换为 YCbCr。你能帮我解决这个问题吗?我的意思是我会对此做一些研究。不过,我需要你的帮助......而且,在编程和算法技能方面,没有人能比你更好。 PS:我无法回答我自己的问题。因为我对上一个问题采取了否定的态度。我不怪你。我为此责备自己。所以,我会等你的答复。
  • @raskolnikov:我们不是编码,更不是“欺骗我的班级”服务!如果你想通过claas,你必须自学。如果您“没有时间”或不想这样做,最好将课程改为“初学者针织”或难度较低的课程。
猜你喜欢
  • 2017-09-25
  • 1970-01-01
  • 1970-01-01
  • 2014-09-21
  • 1970-01-01
  • 1970-01-01
  • 2021-06-29
  • 2012-11-04
  • 2016-08-11
相关资源
最近更新 更多