Task_3_Numpy
Task_3_Numpy
Submission Guidelines:
Submit code here like before: https://github.com/Team-Foxtrot-GIKI/Basics
Note: submit in your own branch, make a separate folder for Task_3. Avoid the use of AI if
you want to improve your skills in python.
Tasks
1. Generate a 1D numpy array of 768 elements [0, 1, …., 765] convert it into a 3D array
with 16 rows and 16 columns and depth of 3. Now try to convert it to the same 3D
array with depth 4 instead of 3. Is it possible?
2. Write a function that take a 2D array and returns the last column, excluding the
element of the first row
3. Write a function called crop that takes a 2D array and the coordinates of the top-left
and bottom-right corners of the desired subarray, then returns the cropped portion of
the array.
4. Write a function that takes in a BGR (Blue-Red-Green) image array and returns a
BGR image with increased contrast: decrease the lightness of the pixels with
lightness less than 55, by 30 and increase the lightness of the pixels with lightness
greater than 200, by 30. Ensure that the lightness value remains in the range (0, 255)
inclusive. Try to use numpy’s functionality to achieve this instead of using for loops.
5. Write a function that takes in a BGR image array, places a black box (BGR color =
(0,0,0)) in a random position in the image, and returns the resulting image. The
height and width of the box should be 30% of the height and width of the original
image respectively (Hint: to generate a random position, generate a random
coordinate (0 <= x < 30% of image width, 0 <= y < 30% of image height) and use it
as a top-right corner for the box)
6. Write a function that takes in an numpy array of [x, y] 2D coordinates, along with an
angle in degrees, rotates the coordinates around the origin (0, 0) and returns the
resulting numpy array. Use matrix multiplication with a 2D rotation matrix to achieve
this. You can use the following coordinates of a star in a 320 by 320 coordinate
system to test your function
[[0,64],[-94,128],[-60,18],[-152,-50],[-36,-52],[0,-160],[38,-51],[153,-49],
[61,19],[94,129]]
Note:
● To use the opencv functions, install opencv using pip install opencv-python and
import it using import cv2.
● You can import a BGR image into a numpy array using the function image =
cv2.imread(file_path)
● The dimension of a BGR image array is (y, x, 3) (height in pixels, width in pixels,
Blue-Green-Red values).
● The BGR array can be converted to HLS array (Hue, Lightness, Saturation) using
image = cv2.cvtColor(image, cv2.COLOR_BGR2HLS) and converted back using
image = cv2.cvtColor(image, cv2.COLOR_HLS2BGR)
● You can display a BGR image array using the functions cv2.imshow(‘test’, image);
cv2.waitKey(); cv2.destroyAllWindows()
● You can use the following code snippet to convert your coordinate array to a 320 by
320 BGR image, which can be displayed using the previous code