Logic for padding of an image array
Pls. note this isn't any home work question, but work I am doing and where I needed some pointers.
I have a 2D image stored in a 1D array(row major order). I need to pad this image with the replication of the border pixels by 1 row of padding at top, 1 row at bottom, 1 column of padding to right,1 column to left of the image. I am trying to do this in a C code.
e.g. consider the 2D image astored in a 1D array as img[] shown below. Actual image is of size 3x4 (4 rows 3 colu开发者_JAVA技巧mns) and after padding by 1 row and 1 column in both directions(up/down/left/right), the final image would be of size 5x6 (6 rows , 5 columns).
int img[30] = {0,0,0,0,0,0,1,2,3,0,0,4,5,6,0,0,7,8,9,0,0,10,11,12,0,0,0,0,0,0};
In my case, the size of padding is known, just 1 row at top and bottom, 1 column to right and left of the image. So one way i implemented this is by creating a mapping between the indices of the pixels in the given image array to the padded output pixel index. I stored this mapping in a precalculated lookup table and used that table in padding logic.
After padding the output should look like:
int img[30] = {1,1,2,3,3,1,1,2,3,3,4,4,5,6,6,7,7,8,9,9,7,7,8,9,9};
The lookup table i created was:-
int lut[6,6,7,8,8,...];
and use this table as follows:
for(i=0;i<30;i++)
{
img[i] = img[lut[i]]; //something on this lines...
}
But I am interested in implementing this image padding logic programatically rathar than using lookup table based method. i.e. I want to calculate the pixel indices in code at run time. EDIT : Also I need to obtain the padded image in place(in same buffer which ofcourse would have provision of storage for the additional padded rows/columns).
Any pointers what would the code look like.
thank you.
-AD
#include <stdio.h>
void pad_image(int* img, int cols, int rows)
{
for( int i = 0 ; i < cols ; ++i )
{
// top and bottom
img[i] = img[i+cols];
img[i+cols*(rows-1)] = img[i+cols*(rows-1)-cols];
}
for( int j = 0 ; j < rows ; ++j )
{
// left and right
img[j*cols] = img[j*cols+1];
img[j*cols+cols-1] = img[j*cols+cols-2];
}
}
int main( int argc, char** argv )
{
int img[30] = {0,0,0,0,0,0,1,2,3,0,0,4,5,6,0,0,7,8,9,0,0,10,11,12,0,0,0,0,0,0};
for( unsigned int i = 0 ; i < 30 ; ++i )
printf("%d ",img[i]);
printf("\n");
pad_image(img,5,6);
for( unsigned int i = 0 ; i < 30 ; ++i )
printf("%d ",img[i]);
printf("\n");
}
Since your image is already located in the correct position within the destination array, this is reasonably simple - you don't have to move any data around within the array, just copy it.
The following function is called with the padded size (pad_image(img, 5, 6);
for your example). It iterates over the filled rows, padding the first and last columns; then duplicates the padded first and last filled rows.
#include <string.h>
void pad_image(int img[], int n_cols, int n_rows)
{
int row;
if (n_cols < 3 || n_rows < 3)
return;
for (row = 1; row < (n_rows - 1); row++)
{
/* Duplicate first column in this row */
img[row * n_cols] = img[row * n_cols + 1];
/* Duplicate last column in this row */
img[row * n_cols + n_cols - 1] = img[row * n_cols + n_cols - 2];
}
/* Duplicate first row */
memcpy(&img[0 * n_cols], &img[1 * n_cols], n_cols * sizeof img[0]);
/* Duplicate last row */
memcpy(&img[(n_rows - 1) * n_cols], &img[(n_rows - 2) * n_cols], n_cols * sizeof img[0]);
}
精彩评论