【问题标题】:How do I convert a struct to an unsigned char and vice versa?如何将结构转换为无符号字符,反之亦然?
【发布时间】:2021-03-15 19:34:28
【问题描述】:

我目前正在尝试编写一个函数,允许用户读取图像,他们可以水平或垂直翻转图像,或者将图像转换为灰度。我无法让灰度功能正常工作。

错误提示“操作数[]”不匹配(操作数类型为“图像”和“整数”)。 另一个错误是“operator=”不匹配(操作数类型是“Image”和“unsigned char”)。

我怎样才能得到它,这样代码才能正确运行?

void toGrayScale(Image image, Pixel pixel, int width, int height)
{
    Image newImage[width][height];
    for (int i = 0; i < width; i++){
        for (int j = 0; j < height; j++) {
            unsigned char color[] = image[width][height];
            color[0] = i % 256;
            unsigned char red = color[0];
            color[1] = j % 256;
            unsigned char green = color[1];
            color[2] = (i * j) % 256;
            unsigned char blue = color[2];
            unsigned char gray = round(0.299*red + 0.587*green + 0.114*blue);
            newImage[i][j] = gray;
        }
    }
}

这是我正在使用的头文件:

struct Pixel {    
    int numRows;
    int numCols;
    char color[];
    int red;
    int green;
    int blue;
};

struct Image {
    int width;
    int height;
    int size;
    Pixel pixel;
};

 // loads a "P3" PPM  formatted image from a file
void loadImage();

 // saves an image to a text file as a "P3" PPM formatted image
void saveImage();

 // filters an image by converting it to grayscale
void toGrayscale(struct Image);

 // manipulates an image by flipping it either vertically or horizontally
void flipImage(struct Image, bool horizontal);

【问题讨论】:

  • 这完全取决于Image 类型的功能,您没有显示。您正在尝试将Image 对象分配给unsigned char[] 数组,然后将单个unsigned char 分配回Image。据推测,Image 提供了读取/设置其像素的方法。我们看不到这些方法是什么。
  • 在旁注中,Image newImage[width][height];not standard C++。使用可变长度数组时,请改用std::vector。除了我不认为你真的想要一个 Image[][] 数组开头,只是一个带有索引像素访问的 Image 。在不知道 Image 究竟是什么的情况下,我们无法帮助您修复代码语法。请edit您的问题提供Image的定义,或至少提供其文档的链接。
  • 请不要使用错误的标签。这个问题与jEdit无关。 :-)
  • 我是用jedit来写代码的。
  • @bigups610:作为 jEdit 专家不太可能有助于回答您的问题,您不应该使用该标签。

标签: c++ c unix


【解决方案1】:

你的代码中有一些对我来说没有多大意义的东西

void toGrayScale(Image image, Pixel pixel, int width, int height)

声明一个函数返回NOTHING并获取一个图像,一个像素,一个宽度和一个高度

Image newImage[width][height];

创建一个图像数组的数组

color[0] = i % 256;
unsigned char red = color[0];

color[0] 写入一个值(可能会或可能不会做某事,具体取决于您是否有正确的复制构造函数),然后您直接将该值复制到red 以进行进一步处理。

我建议重新考虑您的图像结构,因为在当前状态下它无法存储图像(至少不是您可能想要的方式。)

一种方法是将像素定义为一个像素:

struct Pixel {
    uint8_t r, g, b;
};

图像有一个宽度、一个高度和很多像素。

struct Image {
    int width, height;
    std::vector<Pixel> pixels;
};

toGrayscale 应该有签名:

Image toGrayscale(const Image& image);

意思是一个函数返回一个图像并获取一个对现有图像的常量(不可修改)引用。

然后你可以使用创建一个新图像(命名为newImage)

Image newImage{ WIDTH, HEIGHT, std::vector<Pixels>( WIDTH * HEIGHT )};

要获取循环中的颜色值,您可以使用:

uint8_t red = image.pixels[j * width + i].r;

并将灰度值写入新图像,您可以使用

newImage.pixels[j * width + i].r = newImage.pixels[j * width + i].g = newImage.pixels[j * width + i].b = gray;

最后你可以用return newImage返回你的图片了。

您可能还应该重新考虑其他函数的签名,例如

void loadImage();

表示该函数不接受任何参数并且不返回任何内容,而它可能至少应该获取保存图像的位置(可能是const std::filesystem::path&amp;const std::string&amp;const char*)并返回一个图像。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-27
    • 2017-03-10
    相关资源
    最近更新 更多