/*******************************************************************************
ID: 3729557 • Letter: #
Question
/***********************************************************************************************
Part 3: Image Averaging
Your task will be to apply averaging to an image. We will be using k=1, which means you will
add all the adjacent neighbors. Remember to include the value of the target pixel as well.
Take into account that pixels that are on the edges won’t have all 8 neighbors, so your code
should be able to account for that.
- Your computed value should be in image_out, do not modify the values of image.
- The width and height of the image are equal to dim.
***********************************************************************************************/
__declspec(naked)
void imageAveraging(unsigned char* image, int dim, unsigned char* image_out){
RANDOMIZE_CALLER_REGISTERS
__asm
{
// YOUR CODE STARTS HERE
ret
// YOUR CODE ENDS HERE
}
}
Explanation / Answer
Hi as the image is stored in one dimentional array, we need to create function which maps 2d to 1d.
int twoDto1D(int row, int col,int width)
{
return (width * row+col); // this function is used to convert 2d to 1d.
}
void imageAveragin(unsigned char* image, int dim, unsigned char* image_out)
{
image_out = new unsigned char(dim*dim);
for(int i = 0; i < dim; i++)
for(int j = 0; j < dim ; j++)
{
if(i>0&&j>0&&i<(dim-1) && j <(dim-1))
{
image_out[twoDto1D(i,j,dim)] = (image[twoDto1D(i-1,j-1,dim)]+image[twoDto1D(i-1,j,dim)]+image[twoDto1D(i-1,j+1,dim)]+image[twoDto1D(i,j-1,dim)] + image[twoDto1D(i,j,dim)] + image[twoDto1D(i,j+1,dim)]+image[twoDto1D(i+1,j-1,dim)] + image[twoDto1D(i+1,j,dim)] + image[twoDto1D(i+1,j+1,dim)])/9;
}
else if(i==0)
{
if(j==0)
{
image_out[twoDto1D(i,j,dim)] = (image[twoDto1D(i,j,dim)]+ image[twoDto1D(i,j+1,dim)] + image[twoDto1D(i+1,j,dim)] + image[twoDto1D(i+1,j+1,dim)])/4;
}
else if(j == dim-1)
{
image_out[twoDto1D(i,j,dim)] = (image[twoDto1D(i,j,dim)]+ image[twoDto1D(i,j-1,dim)] + image[twoDto1D(i+1,j,dim)] + image[twoDto1D(i+1,j-1,dim)])/4;
}
else
{
image_out[twoDto1D(i,j,dim)] = (image[twoDto1D(i,j-1,dim)] + image[twoDto1D(i,j,dim)] + image[twoDto1D(i,j+1,dim)]+image[twoDto1D(i+1,j-1,dim)] + image[twoDto1D(i+1,j,dim)] + image[twoDto1D(i+1,j+1,dim)])/6;
}
}
else if(i==dim-1)
{
if(j==dim-1)
{
image_out[twoDto1D(i,j,dim)] = (image[twoDto1D(i,j,dim)]+ image[twoDto1D(i,j-1,dim)] + image[twoDto1D(i-1,j,dim)] + image[twoDto1D(i-1,j-1,dim)])/4;
}
else if(j==0)
{
image_out[twoDto1D(i,j,dim)] = (image[twoDto1D(i,j,dim)]+ image[twoDto1D(i,j+1,dim)] + image[twoDto1D(i-1,j,dim)] + image[twoDto1D(i-1,j+1,dim)])/4;
}
else
{
image_out[twoDto1D(i,j,dim)] = (image[twoDto1D(i-1,j-1,dim)]+image[twoDto1D(i-1,j,dim)]+image[twoDto1D(i-1,j+1,dim)]+image[twoDto1D(i,j-1,dim)] + image[twoDto1D(i,j,dim)] + image[twoDto1D(i,j+1,dim)])/6;
}
}
else if(j==0 && i>0 && i < dim-1)
{
image_out[twoDto1D(i,j,dim)] = (image[twoDto1D(i-1,j,dim)]+image[twoDto1D(i-1,j+1,dim)] + image[twoDto1D(i,j,dim)] + image[twoDto1D(i,j+1,dim)] + image[twoDto1D(i+1,j,dim)] + image[twoDto1D(i+1,j+1,dim)])/6;
}
else if(j == dim-1 && i > 0 && i < dim -1)
{
image_out[twoDto1D(i,j,dim)] = (image[twoDto1D(i-1,j-1,dim)]+image[twoDto1D(i-1,j,dim)]+image[twoDto1D(i,j-1,dim)] + image[twoDto1D(i,j,dim)] +image[twoDto1D(i+1,j-1,dim)] + image[twoDto1D(i+1,j,dim)] )/6;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.