我正在尝试将图像下采样 2,我假设它是灰度的图像,所以我将只使用一个通道,我尝试平均 4 个像素,然后将结果放入 destImage。我不知道如何正确填充 destImage。请在这里找到代码:
void downsizeRow(unsigned char *srcImage, unsigned char *dstImage, int srcWidth )
{
unsigned char *srcPtr = srcImage;
unsigned char *dstPtr = dstImage;
int stride = srcWidth;
int b;
for (int i = 0; i< 4; i++)
{
b = srcPtr[0]+srcPtr[1] + srcPtr[stride + 0] + srcPtr[stride + 1] ;
srcPtr++;
dstPtr[0] = (uint8_t)((b + 2)/4);;
dstPtr++;
}
}
void downscaleImage( unsigned char *srcImage, unsigned char *dstImage, int srcWidth, int dstHeight, int dstWidth)
{
unsigned char *srcPtr=srcImage;
unsigned char *dstPtr=dstImage;
int in_stride = dstWidth;
int out_stride = dstHeight;
for (int j=0;j<dstHeight;j++)
{
downsizeRow(srcPtr, dstPtr, srcWidth); // in_stride is needed
// as the function requires access to iptr+in_stride
srcPtr+=in_stride * 2;
dstImage+=out_stride;
}
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
unsigned char srcimage[4*4];
unsigned char dstimage[2*2];
for (int i = 0; i<4*4; i++)
{
srcimage[i] = 25;
}
std::cout<<"source Image \n"<<std::endl;
for (int i = 0; i<4*4; i++)
{
std::cout<<srcimage[i];
}
downscaleImage(srcimage, dstimage, 4,4,2);
std::cout<<"dest Image"<<std::endl;
for (int i = 0; i<2*2; i++)
{
// std::cout<<dstimage[i];
}
return a.exec();
}