Reducing a matrix by 1/4 size

I'm doing a practice problem and it states:

Image data is conveniently represented as a matrix of values. A greyscale image is a 2 dimensional matrix of integers on the range 0 to 255 inclusive.


I'm then asked to create a function which could reduce an image by 1/4, by taking as input: the original image together with its width and height, and it must write the compressed image to another matrix that can assumed to be appropriately sized. The height and width of original matrix is even.

Here's my attempt at the code below, I'm having trouble with what to put inside the array functions... for example when I want to get the average, and how to format the new matrix so it has the correct values/size inside of it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Example program
#include <iostream>
#include <string>
int scaling(int imag[], int rows, int colns, int imga[]);
int main()
{
  int imag[]={4,8,16,4,8,4,32,64,4,40,44,48,88,92,12,8,4,8,16,4,8,4,32,64,4,40,44,48,88,92,12,8};
  scaling(imag, 8, 8);
               std::cout<<imag[13];

}
int scaling(int imag[], int rows, int colns, int imga[]){
       for(int r=0;r<rows;r++)
        for(int c=0;c<colns;c++)
         int imga[]*=(imag[(r*colns)+c]+imag[((r*colns)+c)+1]+imag[((r*colns)+c)+2]+imag[((r*colns)+c)+3])/4;
         return imga[4];
             
}

Last edited on
I got it working a bit but I'm not sure if it's correct. I'm a bit confused about adding values to the array... this line:
imga[r*colnHalf+c]=((imag[(rOrig*colns)+cOrig])+(imag[(rOrig*colns)+(cOrig+1)])+(imag[(rOrig+1)*(colns+cOrig)])+(imag[(rOrig+1)*colns+(cOrig+1)]))/4;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Example program
#include <iostream>
#include <string>
int scaling(int imag[], int rows, int colns, int imga[]);
int main()
{
    int imga[800*600];
  int imag[]={4,8,16,4,8,4,32,64,4,40,44,48,88,92,12,8};
  scaling(imag, 4, 4, imga); 
  std::cout<<imga[0];
           

}
int scaling(int imag[], int rows, int colns, int imga[]){
    int colnHalf=colns/2;
    int rowHalf=rows/2;
       for(int r=0;r<rowHalf;r++){
       int rOrig=r*2;
        for(int c=0;c<colnHalf;c++){
        int cOrig=c*2;
       imga[r*colnHalf+c]=((imag[(rOrig*colns)+cOrig])+(imag[(rOrig*colns)+(cOrig+1)])+(imag[(rOrig+1)*(colns+cOrig)])+(imag[(rOrig+1)*colns+(cOrig+1)]))/4;
        }
       }
        
             
}
Last edited on
Topic archived. No new replies allowed.