0% found this document useful (0 votes)
134 views3 pages

Image Manipulation: Inversion & Rotation

The document discusses how black-and-white images are represented as two-dimensional arrays, specifically a 10 × 10 array where 1 represents black and 0 represents white. It outlines methods for inverting the image and rotating it clockwise by 90 degrees, including pseudocode for these operations. Additionally, it introduces an efficient algorithm to rotate the image without using extra memory by transposing the array and reversing its rows.

Uploaded by

kundaliakush68
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
134 views3 pages

Image Manipulation: Inversion & Rotation

The document discusses how black-and-white images are represented as two-dimensional arrays, specifically a 10 × 10 array where 1 represents black and 0 represents white. It outlines methods for inverting the image and rotating it clockwise by 90 degrees, including pseudocode for these operations. Additionally, it introduces an efficient algorithm to rotate the image without using extra memory by transposing the array and reversing its rows.

Uploaded by

kundaliakush68
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

–6– 8820 – 7011

13. Images in computers are stored as two-dimensional arrays.

A black-and-white image (Figure 1) is stored as a 10 × 10 two-dimensional array named


MAT (Figure 2).

Each element of MAT holds a number for a colour; 1 represents the colour black and 0
represents the colour white.

Figure 1: The simple black-and-white image Figure 2: The corresponding two-dimensional


array MAT
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
[0] 0 0 0 0 0 0 0 1 0 0
[1] 0 0 0 1 0 0 0 0 1 0
[2] 0 0 0 1 0 0 0 0 0 1
[3] 0 0 0 1 0 0 0 0 0 0
[4] 0 0 0 1 0 0 0 0 0 0
[5] 0 0 0 1 0 0 0 0 0 0
[6] 0 0 0 1 0 0 0 0 0 0
[7] 0 0 0 1 0 0 0 0 0 0
[8] 0 0 0 1 1 1 1 1 0 0
[9] 0 0 0 0 0 0 0 0 0 0

In an application, the black-and-white image can be inverted (all white pixels are changed
to black, and all black pixels are changed to white).

Method invert(N,A)accepts a positive integer N and an N × N two-dimensional array A that


holds the data for a simple black-and-white image; it returns the inverted N × N two-dimensional
array A.

(a) Construct an algorithm in pseudocode for the method invert(N,A). [3]

(This question continues on the following page)


–7– 8820 – 7011

(Question 13 continued)

In the application, it is also possible to rotate an image clockwise by 90 degrees (90°).


For example, when the simple black-and-white image is rotated, the corresponding
10 × 10 two-dimensional array MAT is updated.

This would mean the first row of the original MAT is the last column in the rotated MAT,
the second row is the second-to-last last column, … and the last row is the first column.

Figure 3: The simple black-and-white image Figure 4: The corresponding two-dimensional


rotated by 90° (clockwise) array MAT
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
[0] 0 0 0 0 0 0 0 0 0 0
[1] 0 0 0 0 0 0 0 0 0 0
[2] 0 0 0 0 0 0 0 0 0 0
[3] 0 1 1 1 1 1 1 1 1 0
[4] 0 1 0 0 0 0 0 0 0 0
[5] 0 1 0 0 0 0 0 0 0 0
[6] 0 1 0 0 0 0 0 0 0 0
[7] 0 1 0 0 0 0 0 0 0 1
[8] 0 0 0 0 0 0 0 0 1 0
[9] 0 0 0 0 0 0 0 1 0 0

Consider the following algorithm fragment:

K=input()
loop for M=0 to K mod 4 - 1
A=rotate(N,A)
end loop

where:

y N is an integer and A is the N × N two-dimensional array that holds data about an image
y K(K>=0) is an integer showing how many times the image should be rotated
y method rotate(N,A) accepts an integer N and an N × N two-dimensional array A
representing an image. It returns an N × N two-dimensional array representing the image
rotated clockwise by 90°.

(b) (i) State the number of degrees by which the image will be rotated if the input value
of K is 3. [1]

(ii) Outline why it is more efficient that the loop in the given algorithm fragment
executes (K mod 4) times instead of K times. You may give an appropriate
example in your answer. [2]

(This question continues on the following page)

Turn over
–8– 8820 – 7011

(Question 13 continued)

The algorithm for method rotate(N,A) uses an additional N × N two-dimensional array,


named B. The N × N dimensional array B is initialized and updated using the values from A to
represent the image rotated clockwise by 90°.

(c) Construct the algorithm in pseudocode for the method rotate(N,A) described above. [3]

To avoid inefficient use of memory, a new algorithm for the method rotate(N,A) is constructed.

The N × N two-dimensional array A should be rotated clockwise by 90°, without the use of
any additional arrays.

One way of rotating the two-dimensional array A clockwise by 90° is to transpose A, and then
reverse each row of A.

The transpose of A is formed by turning all the rows of A into columns. For example, the
value in the first row and third column (A[1][3]) is swapped with the value in the third row
and first column (A[3][1]).

(d) Construct the new algorithm in pseudocode for the method rotate(N,A)described above. [6]

You might also like