【发布时间】:2015-02-05 17:44:27
【问题描述】:
我正在编写一个处理图像的 c++ 程序,这是缩小图像的函数。有一个“像素”指针数组和一个定义了图像颜色的类。除了 Visual Studio 中包含的用于此图像程序的库之外,我不能使用任何其他库。我遇到了这个函数的问题,我需要遍历图像中的像素并将其分割成块;用户将输入块的宽度/高度。创建块后,需要从每个块中获取平均 RGB 值(该平均值将成为一个新的像素),并且所有这些都被安排好后,它将“缩小”图像。到目前为止,似乎是因为图像变小而创建了块,但我的图像完全变灰了。 RGB 像素的总数添加正确,但代码的其余部分一定有问题,我无法查明它。这是我的代码:
//creates a block of average colors based on range of pixels given
pixel CreateBlock(int start, int stop, pixel** currpix, int blockHeight, int blockWidth)
{
pixel** block; //Problem? might have to be a single pointer pixel* block[];
block = new pixel*[blockHeight];
for (int i = 0; i < blockHeight; i++)
block[i] = new pixel[blockWidth];
pixel newPix;
float totred = 0, totblue = 0, totgreen = 0;
int redav = 0.0, blueav = 0.0, greenav = 0.0;
for (int i = 0; i < blockHeight; i++)
{
for (int j = 0; j < blockWidth; j++)
{
totred = totred + block[i][j].red;
totblue = totblue + block[i][j].blue;
totgreen = totgreen + block[i][j].green;
}
}
redav = totred / (blockHeight*blockWidth);
blueav = totblue / (blockHeight* blockWidth);
greenav = totgreen / (blockHeight*blockWidth);
newPix.red = redav;
newPix.blue = blueav;
newPix.green = greenav;
return newPix;
}
//make a new image that is a smaller resampling of the bigger image
void averageRegions(int blockWidth, int blockHeight)
{
int height = displayed->getHeight(), width = displayed->getWidth();
int i = 0, j = 0;
pixel** currpix = displayed->getPixels(); //PROBLEM
image* shrunk = displayed;
//shrunk->getPixels();
shrunk->createNewImage(width / blockWidth, height / blockHeight);
while (i < height)
{
while (j < width)
{
int start = i, stop = i + 10;
shrunk->getPixels()[i][j] = CreateBlock(start, stop, currpix, blockHeight, blockWidth);
j = j + blockWidth;
}
i = i + blockHeight;
}
return;
}
这是图像类:
class image {
public:
image(); //the image constructor (initializes everything)
image(string filename); //a image constructor that directly loads an image from disk
~image(); //the image destructor (deletes the dynamically created pixel array)
void createNewImage(int width, int height); //this function deletes any current image data and creates a new blank image
//with the specified width/height and allocates the needed number of pixels
//dynamically.
bool loadImage(string filename); //load an image from the specified file path. Return true if it works, false if it is not a valid image.
//Note that we only accept images of the RGB 8bit colorspace!
void saveImage(string filename); //Save an image to the specified path
pixel** getPixels(); //return the 2-dimensional pixels array
int getWidth(); //return the width of the image
int getHeight(); //return the height of the image
void viewImage(CImage* myImage); //This function is called by the windows GUI. It returns the image in format the GUI understands.
private:
void pixelsToCImage(CImage* myImage); //this function is called internally by the image class.
//it converts our pixel struct array to a standard BGR uchar array with word spacing.
//(Don't worry about what this does)
pixel** pixels; // pixel data array for image
int width, height; // stores the image dimensions
};
这里是像素类:
class pixel
{
public:
unsigned char red; //the red component
unsigned char green; //the green component
unsigned char blue; //the blue component
};
【问题讨论】:
-
我们对
displayed知之甚少,尤其是被调用的方法及其作用。另外,使用的其他名称呢? -
除非我的眼睛在欺骗我,否则你的
CreateBlock函数中存在巨大的内存泄漏。 -
你用
CreateBlock中的block做什么?你分配内存,然后什么也不做。无论哪种方式,CreateBlock似乎都是一个误导性的名称,如果您只是从该块中创建一个像素,正如返回值所暗示的那样。至于其余部分,displayed类究竟包含什么? -
你在哪里释放这个语句中分配的内存:
block = new pixel*[blockHeight];? -
@Deduplicator 显示是从图像类创建的 -> image* currImage; (当前图像)图像*显示; (对于新图像,当我需要私有数据时抓取像素和高度/宽度)
标签: c++ image visual-c++ pixel