robbins88

I have the binary function done for this question, but I can't get the blur function to work using arrays/matrices. Can anyone help me? This is what I have so far:

``````void threshold(int src[], int rows, int cols, int dest[], int thr) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++)
if (src[i*cols + j] > thr)
dest[i*cols + j] = 255;
else
dest[i*cols + j] = 0;
}
}
/** blur ******************************************************************
* @params - src, in any units, @pre=>0
* 			 rows, in any units, @pre=>0, must be a whole number
* 	         cols, in any units, @pre=>0, must be a whole number
* 			 dest, in any units, @pre=0 or 255
* @modifies - returns the array dest
* @returns - nothing
**************************************************************************/
void blur(int src[], int rows, int cols, int dest[]) {
int count;
for (int i = 0; i<rows; i++) {
for (int j = 0; j<cols; j++)
for (int count = 4; count <= i*cols + j; count++)
int sum[i*j];
sum += src[count];
dest = sum / count;
}
}``````

I'm not exactly sure where I'm going wrong.

All the resources and information can be found here:

Ancient Dragon 5,243

you need to make more generous use of { and }

``````void blur(int src[], int rows, int cols, int dest[]) {
int count;
for (int i = 0; i<rows; i++)
{
for (int j = 0; j<cols; j++)
{
for (int count = 4; count <= i*cols + j; count++)
{
int sum[i*j];
sum += src[count];
dest = sum / count;
}
}
}``````

line 19 above:
(1) you can't declare an array like that because i*j must be a constant. If you want to use variables then you have to allocate the array with either new or malloc().
(2) Why are you declaring that array inside that loop??? Move the declaration up probably above line 3.
line 10: the previous line 9 declares sum as an array, but line 10 attempts to use it as a simple integer. You can't have it both ways.

dougy83 74

To reference a pixel, say at (x, y) in the src matrix, wouldn't it be better to use:

``````#define pixelRef(X, Y) ((Y) * cols + (X))
int pixelVal = src[pixelRef(x, y)];``````

The blur is supposed to set set each destination pixel (xi, yi) to the average of up to 9 source pixels around (xi, yi).

i.e.

``````dst[pixelRef(x, y)] = (
src[pixelRef(x-1, y-1)]  + src[pixelRef(x, y-1)]  + src[pixelRef(x+1, y-1)] +
src[pixelRef(x-1, y)]     + src[pixelRef(x, y)]     + src[pixelRef(x+1, y)] +
src[pixelRef(x-1, y+1)] + src[pixelRef(x, y+1)] + src[pixelRef(x+1, y+1)] ) / 9.0 + .5;``````

is what could be used for all pixels that are not on the edges. The .0 + .5 is for rounding the result. This of course can be put in a for( ; ; ) loop.