10/08/2026 Computer Vision Michael Eckmann Some numpy stuff and in-class exercises that are parts of the programming assignment. ------------- 0. Reminder of the available types in numpy: available types: >>> import numpy as np >>> np.sctypes {'int': [, , , ], 'uint': [, , , ], 'float': [, , , ], 'complex': [, , ], 'others': [, , , , ]} >>> np.sctypeDict.keys() dict_keys(['?', 0, 'byte', 'b', 1, 'ubyte', 'B', 2, 'short', 'h', 3, 'ushort', 'H', 4, 'i', 5, 'uint', 'I', 6, 'intp', 'p', 7, 'uintp', 'P', 8, 'long', 'l', 'ulong', 'L', 'longlong', 'q', 9, 'ulonglong', 'Q', 10, 'half', 'e', 23, 'f', 11, 'double', 'd', 12, 'longdouble', 'g', 13, 'cfloat', 'F', 14, 'cdouble', 'D', 15, 'clongdouble', 'G', 16, 'O', 17, 'S', 18, 'unicode', 'U', 19, 'void', 'V', 20, 'M', 21, 'm', 22, 'b1', 'bool8', 'i8', 'int64', 'u8', 'uint64', 'f2', 'float16', 'f4', 'float32', 'f8', 'float64', 'f16', 'float128', 'c8', 'complex64', 'c16', 'complex128', 'c32', 'complex256', 'object0', 'bytes0', 'str0', 'void0', 'M8', 'datetime64', 'm8', 'timedelta64', 'int32', 'i4', 'uint32', 'u4', 'int16', 'i2', 'uint16', 'u2', 'int8', 'i1', 'uint8', 'u1', 'complex_', 'single', 'csingle', 'singlecomplex', 'float_', 'intc', 'uintc', 'int_', 'longfloat', 'clongfloat', 'longcomplex', 'bool_', 'bytes_', 'string_', 'str_', 'unicode_', 'object_', 'int', 'float', 'complex', 'bool', 'object', 'str', 'bytes', 'a', 'int0', 'uint0']) --------------------------- 1. Reminder of empty, zeros, ones to create arrays >>> myarr1 = np.empty((10,5),np.uint8) >>> myarr1 array([[142, 81, 255, 102, 76], [ 86, 0, 0, 0, 0], [ 0, 0, 0, 0, 0], [ 0, 192, 192, 119, 95], [128, 127, 0, 0, 224], [208, 119, 95, 128, 127], [ 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0], [ 42, 0, 0, 0, 101], [115, 39, 44, 64, 0]], dtype=uint8) >>> myarr2 = np.zeros((10,5), np.uint8) >>> myarr2 array([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]], dtype=uint8) >>> myarr3 = np.ones((10,5), np.uint8) >>> myarr3 array([[1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1]], dtype=uint8) ---------------------- 2. Observe what flatten and reshape do below >>> myarr3.shape (10, 5) >>> myarr3f = myarr3.flatten() >>> myarr3f array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=uint8) >>> myarr3f.shape (50,) >>> myarr3rs = myarr3.reshape((50)) >>> myarr3rs.shape (50,) >>> myarr3rs array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=uint8) >>> myarr3[0,0] = 50 >>> myarr3 array([[50, 1, 1, 1, 1], [ 1, 1, 1, 1, 1], [ 1, 1, 1, 1, 1], [ 1, 1, 1, 1, 1], [ 1, 1, 1, 1, 1], [ 1, 1, 1, 1, 1], [ 1, 1, 1, 1, 1], [ 1, 1, 1, 1, 1], [ 1, 1, 1, 1, 1], [ 1, 1, 1, 1, 1]], dtype=uint8) >>> myarr3f array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=uint8) >>> myarr3rs array([50, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=uint8) # can do operations on each element like so: >>> myarr3rs - 1 array([49, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=uint8) # can easily get the sum of all the elements of array by using sum: >>> sum(myarr3rs) 99 ---------------------- Exercise: Write a line of code that will create the HSV image (as a 3d ndarray) based on the size of the original image and select a valid type for the elements. Hints: Use np.empty because you will eventually have code that alters every element of the array For the type, realize that you'll need to be able to store 0..359 and 361 for undefined in Hue, and 0..255 for S and V Exercise: Write a line of code that will create the histogram (a 2d ndarray) that is of the size stated in the assignment and of a valid type for the elements. Hints: Use np.zeros because you will likely want to start the values off at zero so that when you consider each pixel, you will add 1 to the correct bin Exercise: Come up with the code that will change the histogram shape from 2d to 1d and to normalize the values (that is, make them all be floating point values that are proportions of the number of pixels in the bin to the overall number of pixels in the original histogram.) The sum of the normalized values should be 1.0 Note well: if you use flatten, to convert the array to 1d, it will make a copy of the data, but if you use reshape it will return an alias to the original. This may not matter as I don't think you'll need to keep the original shape and values after you make it 1d. Exercise: Try your hand at writing the distance functions.