How do I modify all elements of a 2D array at once?

closed account (oN3h0pDG)
Hello all, I have a assignment where I'm supposed to take pixel values of an image and invert them by 1 (1.00 - pixel). So I have a 2D Array of 25 pixel values and I have to subtract each pixel from 1.00. For example: 0.00 would become 1.00, and 0.25 would become 0.75 ans so on. The problem is I have no idea how to do it, I'm probably just overlooking something easy but as it remains I cannot see it. Thanks for you're help! It is appreciated, also if you see any bugs or have feedback on anything I can change please let me know, it is very helpful, thanks!

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <iomanip>
using namespace std;

// Procedure that takes the original grayscale image and turns it into a negated image, then displays it to the console.
void negatedImage(double imageI[5][5]) {
	double imageJ = imageI[5][5];

	cout << "Negated Image: " << endl;

	for(int i=0; i<5; i++)  {  // This loops through the rows.

								for(int j=0; j<5; j++) { // This loops through the columns

									imageJ[i][j] = 1 - j; // ??? Unsure ???

									cout << setprecision(2) << fixed;
									cout << imageJ[i][j] << " ";


								}
							}

}

// Procedure that takes the grayscale image and displays it to the console.
void displayImageData(double imageI[5][5]) {

	cout << "Grayscale Image: " << endl;
	for(int i=0; i<5; i++)  {  // This loops through the rows.

						for(int j=0; j<5; j++) { // This loops through the columns


							cout << setprecision(2) << fixed;
							cout << imageI[i][j] << " ";


						}
					}
	cout << endl;

	negatedImage(imageI);
}

int main() {

// image pixels
double imageI[5][5] = {{ 0.00 , 0.25 , 0.50 , 0.25, 0.00 } ,{ 0.20 , 0.40 , 0.60 , 0.80, 1.00 } ,{ 0.25 , 0.50, 1.00, 0.50, 0.25 } ,{ 0.20, 0.40, 0.60, 0.80, 1.00 } ,{0.00, 0.25, 0.50, 0.25, 0.00}};

displayImageData(imageI); // call procedure to display image pixel numbers


	return 0;
}
Last edited on
The problem is that you create a copy of the image data in void negatedImage(double imageI[5][5])
and modify it, but don't display it.

In displayImageData you call negatedImage(imageI); after you have displayed it.

In main you call only displayImageData(imageI);

I hope this will put you on the right track.

If not please feel free to ask again.
The problem is that you create a copy of the image data in void negatedImage(double imageI[5][5])
and modify it, but don't display it.

There is no copy and negatedImage looks as if it'll display the image as it's negated (although it would certainly be a better design if it didn't), provided he can get it to compile.


double imageJ = imageI[5][5]; imageJ is a single double. imageI[5][5] is out of bounds of your array.

1
2
3
4
5
void negatedImage(double imageI[5][5]) {
    for (unsigned i=0; i<5; ++i)
        for (unsigned j=0; j<5; ++j)
            imageI[i][j] = 1 - imageI[i][j];
}






@cire

double imageJ = imageI[5][5]; imageJ is a single double.


You are absolutely right. Probably have been sitting too long in front of the screen. :(
Topic archived. No new replies allowed.