【发布时间】: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 专家不太可能有助于回答您的问题,您不应该使用该标签。