I’ve been working through the exercises in Chapter 8 to gain a better understanding of image processing. The RGB exercises are starting to make sense a little more, but the last three image processing exercises are still confusing to me.

I understand question 10’s concept about doubling the window and new image (multiplying the width and height by 2). But, I don’t understand the code in the for loop’s body. I was wondering if anyone could clarify the meaning of the code or if there are resources that could help me practice image processing more. It’s been hard to figure out how to adjust my learning techniques to truly understand the material covered in Chapter 8.

What helped me visualize the solution was to sketch out a simple 2x2 square (2 rows, 2 columns, 4 pixels total), then number the pixels/cells 1,2,3,4 (first row, first column is pixel 1, first row, second column is pixel 2, second row, first column is pixel 3, second row, second column is pixel 4.).

Then I sketched out a 4x4 square (4 rows, 4 columns, 16 pixels total). Now think of this 4x4 square as made up of 4 2x2 squares. You have your upper left 2x2 square, your upper right 2x2 square, your bottom left 2x2 square, and your bottom right 2x2 square. Each of these 2x2 square has to map to your original 2x2 square where you numbered the pixels.

So pixel 1 in the original 2x2 square is duplicated in the upper left 2x2 square of the 4x4 square. 1 pixel becomes 4 pixels. Same with pixel 2 of the original square, it becomes the 4 pixels of the upper right 2x2 square. And so on.

That’s all that’s happening within the for loops. You’re turning one pixel of the original image to 4 pixels of the doubled image. It’s easiest to see this by labeling pixel 4 of the original 2x2 square as being at position (row 1, column 1 or 1,1). Translate that pixel to the corresponding pixels of the 4x4 square and it should occupy the pixels at positions (2,2) (2,3) (3,2) (3,3)