0
votes

I'm writing an image processing code to perform a median filter with a variable sized window. The greyscale image has been read into an array image1, and I'm trying to copy a window selection of the array into a second array window. This is easy for a fixed sized window (3x3 window shown) as you can just say:

window[1]=image1[m-((win_size-1)/2)][n-((win_size-1)/2)];
window[2]=image1[m][n-((win_size-1)/2)];
window[3]=image1[m+((win_size-1)/2)][n-((win_size-1)/2)];
window[4]=image1[m-((win_size-1)/2)][n];
window[5]=image1[m][n];
window[6]=image1[m+((win_size-1)/2)][n];
window[7]=image1[m-((win_size-1)/2)][n+((win_size-1)/2)];
window[8]=image1[m][n+((win_size-1)/2)];
window[9]=image1[m+((win_size=1)/2)][n+((win_size-1)/2)];

In MATLAB you can generalise this to any sized window easily by using a vector in the array call:

window = image1(m-((win_size-1)/2):m+((win_size-1)/2),n-((win_size-1)/2):n+((win_size-1)/2));

I can't work out a way to do this in C, can anyone help me with this please?

2
For copying large amounts of data between structures and arrays, get to know memmove(). - Lee Daniel Crocker
@LeeDanielCrocker Why not memcpy? The source and destination are not overlapping here, so no need to use intermediate storage... - Eugene Sh.
Safer to recommend to newbies, and most good memmove() implementations do not use intermediate storage. - Lee Daniel Crocker

2 Answers

0
votes

Solved by using nested for loops with a pre-defined int outside the loop. Assigned to 0 at start of first loop then +1 on each iteration.

-2
votes

You will have to dynamically allocate memory for an image, whatever image may be, for an array then add it to your array. I don't know exactly how to do it in C, but in C++ it would look something like:

image = new Image [5];