I'm trying to downsample an image by 2, the image i assumed that it is greyscale, so I will work only with one channel, I tried to average 4 pixels, then put the resultant in the destImage. I don't know how to fill the destImage correctly. Kindly find the code here:

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();
}
有帮助吗?

解决方案

There's not much wrong in your code -- basically just keep proper track of the read/write pointer locations (remember to update with strides). This requires using 2 nested loops one way or another. (+ fix the divider to 4).

I've found the following approach useful: processing one row at a time has not much speed penalty, but allows easier integration of various kernels.

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;
}

其他提示

I see you are using Qt, so just in case you don't need to reinvent the wheel, QImage has a convenience function that will do resizing (effectively down-sampling) for you.

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

In case QImage is too slow for you, you can also try using QPixmap which is generally faster.

Omitting Qt::SmoothTransformation will fall back to using the default Qt::FastTransformation which will be even faster.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top