在C中水平翻转BMP图像

时间:2019-06-02 13:21:43

标签: c image-processing

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

图像的大小为int高度和int宽度,并且该函数知道以像素为单位的值。

这是我的代码:

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

2 个答案:

答案 0 :(得分:2)

想象一下,您的图片按像素的行和列排列,每个像素具有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;

    }
}

}

答案 1 :(得分:1)

以下C代码向左/向右翻转RGB图像:

var a, b, rest;
[a, b] = [10, 20];

在执行//Flip RGB (or BGR) image Left/Right. //P is an array of <height> pointers. //P[0] points first pixel of first row. //P[1] points first pixel of second row. //P[height-1] points first pixel of last row. static void flip_hrizntal_pixels(Pixel **P, int height, int width) { //Allocate sketch buffer for storing single flipped row. Pixel *R = (Pixel*)malloc(width*sizeof(Pixel)); //Stuck here don't know how flip those pixels for (int i = 0; i < height; i++) { Pixel *I0 = P[i]; //Points first pixel in source image //j is destination index (index of rgb triple in source image I). for (int j = 0; j < width; j++) { //Iterate source row from end of row to beginning of row. R[j].red = I0[width - j - 1].red; R[j].green = I0[width - j - 1].green; R[j].blue = I0[width - j - 1].blue; } //Copy flipped row back to image P. memcpy(I0, R, width*sizeof(Pixel)); } free(R); } 之前,您需要准备一个指针数组。

以字节为单位计算图像步幅:
flip_hrizntal_pixels是两个连续行之间的字节数。
对于BMP格式,stride必须为4的倍数(如果宽度不是4的倍数,则必须加起来)。

stride

准备指向行的指针数组:
假设int stride = width*3; //For supporting width that is not a multiple of 4, stride in bytes is padded up to nearest multiple of 4 bytes. stride = (stride + 3) & (~3); //http://mapw.elte.hu/elek/bmpinmemory.html 是指向输入图像的指针。

I

执行//Prepare and array of pointers to rows: ////////////////////////////////////////////////////////////////////////// //P[0] -> Points beginning of first row. //P[1] -> Points beginning of second row. //... //P[height-1] -> Points beginning of last row. //Allocate array of <height> pointers. Pixel **P = (Pixel**)malloc(height*sizeof(Pixel*)); for (int i = 0; i < height; i++) { P[i] = (Pixel*)((char*)I + stride*i); //Advance by <stride> bytes from row to row. } //////////////////////////////////////////////////////////////////////////

flip_hrizntal_pixels

清理:

flip_hrizntal_pixels(P,
                     height, 
                     width);

测试(来自MATLAB的{free(P); 图片):

输入图片:
Before Flip

输出图像:
After Flip