我正在为我的计算机科学课开展一个项目,在那里我们创建一个源图像的照片马赛克。我们已经获得了基础图像类。与标准构造函数一起,此类包含getter函数以返回私有像素数组数据和图像高度和宽度。我们被告知为所有填充图像创建派生类,需要将其存储在链表中。我已经完成了大部分计划,但我不知道如何用填充图像中的像素数据替换源图像的块。
#include "stdafx.h" //needed for the Windows display
#include <cstdlib>
#include <cmath>
#include <string>
#include <iostream>
using namespace std;
#include "globals.h" //some global variables are included here
#include "pixel.h" //includes the pixel class for storing individual pixels
#include "image.h" //includes the image class we will be using.
#include "fillerImage.h" //includes the fillerImage
#include "fillNode.h"
#include <sstream>
image* mosaic = displayed; //displayed is the current image in the gui, mosaic is the copy that will be the photomosaic
pixel** mospix = mosaic->getPixels();
pixel** headpix = head->data->pix; //I created a public member pixel** in the filler image class with the hope that I could use it to access the private pixel data of the base image class.
int mosaicWidth = displayed->getWidth() / blockWidth * FILLER_IMAGE_WIDTH;
int mosaicHeight = displayed->getHeight() / blockHeight * FILLER_IMAGE_HEIGHT;
//mosaic->createNewImage(mosaicWidth, mosaicHeight);
for (int i = 0; i < mosaicHeight; i = i + blockHeight)
{
for (int j = 0; j < mosaicWidth; j = j + blockWidth)
{
for (int blockrow = 0; blockrow < blockHeight; blockrow++)
{
for (int blockcol = 0; blockcol < blockWidth; blockcol++)
{
mospix = headpix;
}
}
}
displayed = mosaic;
}
displayed = mosaic;
我一直遇到尝试写入受保护数据的问题;如何修改马赛克的像素数据?我不允许编辑基本图像类,只允许编辑填充图像类。如果我的帖子格式不正确,我很抱歉,这是我第一次寻求帮助。提前谢谢!
编辑:这是图像类标题。我不允许以任何方式修改此内容,并且它不包含像素数据的设置器。
#include "globals.h"
#include "pixel.h"
using namespace std;
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 object 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
};
#endif
答案 0 :(得分:0)
访问像素的唯一方法是通过getPixel()
:
const
指针,并且没有文档,我认为您也可以在其上书写。 使用以下语句,您可以访问mosaic
的像素数组。
pixel** mospix = mosaic->getPixels();
从那一刻开始,只要mospix[i][j]
,就可以使用0 <= i && i < mosaic->getHeight() && 0<=j && j<mosaic->getWidth()
直接读取/写入任何像素。
但是代码中存在严重问题。在嵌套循环中,您的内部语句是:
mospix = headpix;
这并不像您期望的那样复制当前像素。它用指针headpix
的副本替换了您已正确初始化的指针。从那时起,如果您要访问mospix
数组元素,实际上您将mosaic
内的图片保持不变,但您可以访问head->data
中的像素!
我不确切地知道你想要如何制作马赛克,但我认为你的循环方式存在不匹配,并且可以想象你想要说出像:
for (int i = 0; i < mosaic->getHeight(); i = i + blockHeight)
{
for (int blockrow = 0; blockrow < blockHeight; blockrow++)
{
for (int j = 0; j < mosaic->getWidth(); j = j + blockWidth)
{
for (int blockcol = 0; blockcol < blockWidth; blockcol++)
{
mospix[i+blockrow][j+blockcol] = headpix[blockrow][blockcol];
}
}
}
}
displayed = mosaic; // Not sure it's needed: both are pointers pointing to the same object