0

我正在尝试将图像下采样 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();
}
4

2 回答 2

1

我看到您正在使用 Qt,所以以防万一您不需要重新发明轮子,QImage 有一个方便的功能,可以为您调整大小(有效地进行下采样)。

QImage smallImage = bigImage.scaled(bigImage.width() / 2, bigImage.heigth() / 2, Qt::KeepAspectRatio, Qt::SmoothTransformation);

如果 QImage 对您来说太慢,您也可以尝试使用通常更快的 QPixmap。

省略Qt::SmoothTransformation将退回到使用默认值Qt::FastTransformation,这将更快。

于 2013-03-14T10:54:41.270 回答
1

您的代码并没有太多错误——基本上只需正确跟踪读/写指针的位置(记得用步幅更新)。这需要以一种或另一种方式使用 2 个嵌套循环。(+ 将分隔线固定为 4)。

我发现以下方法很有用:一次处理一行并没有太大的速度损失,但可以更轻松地集成各种内核

iptr=input_image;  in_stride = in_width;
optr=output_image; out_stride = out_width;
for (j=0;j<out_height;j++) {
    process_row(iptr, optr, in_width);  // in_stride is needed
    // as the function requires access to iptr+in_stride
    iptr+=in_stride * 2;
    optr+=out_stride;
}
于 2013-03-14T11:09:19.810 回答