我有一张我试图模糊的图像,但它最终看起来确实很奇怪:
我所做的是:我获取每个像素,并在逐个颜色的基础上将其值与所有相邻像素的值进行平均。
或者我认为。但是那里显然有一个错误,我怀疑我的 for 循环有问题,但对于我的生活,我无法找出实际出了什么问题。
特别是,第五步(输出步骤)显示图像仍然是有序的——如果我在右侧输出左侧图像,而不是模糊蒙版的图像,像素仍然是正确的顺序。
try
{
// STEP ONE: MAKE MEMORY AVAILABLE FOR IMAGE
int ***image;
image = new int**[m_nSizeX];
for(int i = 0; i < m_nSizeX; ++i)
{
image[i] = new int*[m_nSizeY];
for(int j = 0; j < m_nSizeY; ++j)
{
image[i][j] = new int[nrPixels];// image[x][y][z] is now a pointer to an int
}
}
// STEP TWO: MAKE MEMORY AVAILABLE FOR IMAGE MASK
int ***mask;
mask = new int**[m_nSizeX];
for(int i = 0; i < m_nSizeX; ++i)
{
mask[i] = new int*[m_nSizeY];
for(int j = 0; j < m_nSizeY; ++j)
{
mask[i][j] = new int[nrPixels];// mask[x][y][z] is now a pointer to an int
}
}
//STEP THREE: COPY IMAGE INTO MEMORY
unsigned long lOffset = 0;
for(long i=0; i<m_nSizeX ; i++)
{
for(long j=0; j<m_nSizeY ; j++)
{
for(int k=0; k<(nrPixels) ; k++)
{
image[i][j][k] = *(reinterpret_cast<unsigned char*>(m_pcMemOrg + lOffset) );
lOffset++;
}
}
}
// STEP FOUR: BLUR IMAGE
for(long i=0; i<m_nSizeX ; i++)
{
for(long j=0; j<m_nSizeY ; j++)
{
for(int k=0; k<(nrPixels) ; k++)
{
// INSERT BLURRING FUNCTION HERE (New value = Old value averaged with adjacent pixels)
if(k != 2) // 0 = blue, 1 = green, 2 = red;
{
mask[i][j][k] = 0;
}
else
if(i==0 && j==0)// (0,0) Corner Pixel
{
mask[i][j][k] = (image[i][j][k]+image[i+1][j][k]+image[i][j+1][k]+image[i+1][j+1][k])/4;
}
else if(i==0 && j==(m_nSizeY-1))// (0,yMax) Corner Pixel
{
mask[i][j][k] = (image[i][j][k]+image[i+1][j][k]+image[i][j-1][k]+image[i+1][j-1][k])/4;
}
else if(i==(m_nSizeX-1) && j==0)// (xMax,0) Corner Pixel
{
mask[i][j][k] = (image[i][j][k]+image[i-1][j][k]+image[i][j+1][k]+image[i-1][j+1][k])/4;
}
else if(i==(m_nSizeX-1) && j==(m_nSizeY-1))// (xMax,yMax) Corner Pixel
{
mask[i][j][k] = (image[i][j][k]+image[i-1][j][k]+image[i][j-1][k]+image[i-1][j-1][k])/4;
}
else if(i==0)// (0,---) Edge Pixels
{
mask[i][j][k] = (image[i][j][k]+image[i][j+1][k]+image[i+1][j+1][k]+image[i+1][j][k]+image[i+1][j-1][k]+image[i][j-1][k])/6;
}
else if(j==0)// (---,0) Edge Pixels
{
mask[i][j][k] = (image[i][j][k]+image[i-1][j][k]+image[i-1][j+1][k]+image[i][j+1][k]+image[i+1][j+1][k]+image[i+1][j][k])/6;
}
else if(i==(m_nSizeX-1))// (xMax,---) Edge Pixels
{
mask[i][j][k] = (image[i][j][k]+image[i][j-1][k]+image[i-1][j-1][k]+image[i-1][j][k]+image[i-1][j+1][k]+image[i][j+1][k])/6;
}
else if(j==(m_nSizeY-1))// (---,yMax) Edge Pixels
{
mask[i][j][k] = (image[i][j][k]+image[i+1][j][k]+image[i+1][j-1][k]+image[i][j-1][k]+image[i-1][j-1][k]+image[i-1][j][k])/6;
}
else // Mid-Image Pixels
{
mask[i][j][k] = (image[i][j][k]+image[i][j+1][k]+image[i+1][j+1][k]+image[i+1][j][k]+image[i+1][j-1][k]+image[i][j-1][k]+image[i-1][j-1][k]+image[i-1][j][k]+image[i-1][j+1][k])/9;
}
}
}
}
//STEP FIVE: OUTPUT BLURRED IMAGE
lOffset = 0;
for(long i=0; i<m_nSizeX ; i++)
{
for(long j=0; j<m_nSizeY ; j++)
{
for(int k=0; k<(nrPixels) ; k++)
{
*(reinterpret_cast<unsigned char*>(m_pcMemInv + lOffset) ) = mask[i][j][k];
//*(reinterpret_cast<unsigned char*>(m_pcMemInv + lOffset) ) = image[i][j][k];
lOffset++;
}
}
}
// STOP USING IMAGE MEMORY NOW
for (int i = 0; i < m_nSizeX; ++i) {
for (int j = 0; j < m_nSizeY; ++j)
delete [] image[i][j];
delete [] image[i];
}
delete [] image;
// STOP USING MASK MEMORY NOW
for (int i = 0; i < m_nSizeX; ++i) {
for (int j = 0; j < m_nSizeY; ++j)
delete [] mask[i][j];
delete [] mask[i];
}
delete [] mask;
}
catch( ... )
{
}