首页 > 解决方案 > 在c中水平翻转BMP图像

问题描述

我的功能是获取图像,我正在尝试向右或向左翻转它。水平翻转。我试图做类似的事情,但不知道如何前进

图像的大小是 int height 和 int width ,函数知道像素值。

这是我的代码:

void flip_hrizntal_pixels(struct Pixel **pixels, int height, int width)
{
    //Stuck here don't know how flip those pixels 
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            //pixels[i][j].red = 
            //pixels[i][j].green = 
            //pixels[i][j].blue = 
        }
    }

}

这是结构数据:

struct Pixel
{
    unsigned char red;
    unsigned char green;
    unsigned char blue;
};
struct RGB_Image
{
    long height;
    long width;
    long size;
    struct Pixel **pixels;
};

我这样调用这个函数:

struct RGB_Image image;
int status = load_image(&image); 
flip_hrizntal_pixels(image.pixels, image.height, image.width);

标签: cimage-processing

解决方案


想象一下,您的图片被排列为像素的行和列,每个像素具有 R、G 和 B。每行将具有“宽度”数量的像素,并且会有这样的“高度”数量的行。

因此,要在水平方向上翻转,即一行中最右边的像素转到最左边,反之亦然,然后是同一行中的第二个像素,它与倒数第二个像素交换,依此类推,代码将类似于这。(PS:这只是一个快速代码,让您了解如何继续。我还没有编译/运行我的代码)

希望这可以帮助

void flip_hrizntal_pixels(struct Pixel **pixels, int height, int width)
{
Pixel tempPixel;

for (int i = 0; i < height; i++)
{
    for (int j = 0; j < width/2; j++)
    {
    //make a temp copy of the 'j-th' Pixel  
    tempPixel.red = pixels[i][j].red; 
    tempPixel.green = pixels[i][j].green;  
    tempPixel.blue = pixels[i][j].blue; 


    //copy the corresponding last Pixel to the j-th pixel 
    pixels[i][j].red = pixels[i][width-j].red; 
    pixels[i][j].green = pixels[i][width-j].green; 
    pixels[i][j].blue = pixels[i][width-j].blue;


    //copy the temp copy that we made earlier of j-th Pixel to the corresponding last Pixel
    pixels[i][width-j].red = tempPixel.red;
    pixels[i][width-j].green = tempPixel.green;
    pixels[i][width-j].blue = tempPixel.blue;

    }
}

}


推荐阅读