finish ex4
This commit is contained in:
		
							
								
								
									
										54
									
								
								ex4.py
									
									
									
									
									
								
							
							
						
						
									
										54
									
								
								ex4.py
									
									
									
									
									
								
							@@ -1,15 +1,51 @@
 | 
				
			|||||||
 | 
					"""
 | 
				
			||||||
 | 
					Author: Lukas Heiligenbrunner
 | 
				
			||||||
 | 
					Matr.Nr.: K12104785
 | 
				
			||||||
 | 
					Exercise 4
 | 
				
			||||||
 | 
					"""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import numpy as np
 | 
					import numpy as np
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def ex4(image_array: np.array, offset: (int, int), spacing: (int, int)):
 | 
					def ex4(image_array: np.array, offset: (int, int), spacing: (int, int)) -> (np.array, np.array, np.array):
 | 
				
			||||||
    mask = np.zeros(shape=image_array.shape, dtype=image_array.dtype)
 | 
					    if not isinstance(image_array, np.ndarray):
 | 
				
			||||||
    test = list(range(offset[0], mask.shape[1] - 1, spacing[0]))
 | 
					        raise TypeError
 | 
				
			||||||
    print(test)
 | 
					
 | 
				
			||||||
    pass
 | 
					    if len(image_array.shape) != 3 or image_array.shape[2] != 3:
 | 
				
			||||||
 | 
					        raise NotImplementedError
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if not all([str(x).isnumeric() for x in offset + spacing]):
 | 
				
			||||||
 | 
					        raise ValueError
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if not all([0 <= x <= 32 for x in offset]) or not all([2 <= x <= 8 for x in spacing]):
 | 
				
			||||||
 | 
					        raise ValueError
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    src = np.transpose(image_array, (2, 0, 1))
 | 
				
			||||||
 | 
					    mask = np.zeros_like(src)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    for x in range(offset[0], mask.shape[2], spacing[0]):
 | 
				
			||||||
 | 
					        for y in range(offset[1], mask.shape[1], spacing[1]):
 | 
				
			||||||
 | 
					            mask[:, y, x] = 1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # masking the input
 | 
				
			||||||
 | 
					    masked = src * mask
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # calculate the remaining pixels
 | 
				
			||||||
 | 
					    rest = src * (1 - mask)
 | 
				
			||||||
 | 
					    rest = rest[1 - mask > 0]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # raise error if known pixels are less than 144
 | 
				
			||||||
 | 
					    if (src.shape[1] * src.shape[2] - rest.size / 3) < 144:
 | 
				
			||||||
 | 
					        raise ValueError
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return masked, mask, rest
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if __name__ == '__main__':
 | 
					if __name__ == '__main__':
 | 
				
			||||||
    ex4(np.array([[(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0)],
 | 
					    f, d, g = ex4(np.array([[(255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255)],
 | 
				
			||||||
                  [(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0)],
 | 
					                            [(255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255)],
 | 
				
			||||||
                  [(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0)]
 | 
					                            [(255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255)],
 | 
				
			||||||
                  ]), offset=(1, 1), spacing=(2, 2))
 | 
					                            [(255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255)],
 | 
				
			||||||
 | 
					                            [(255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255)]
 | 
				
			||||||
 | 
					                            ]), offset=(1, 1), spacing=(2, 2))
 | 
				
			||||||
 | 
					    print(g)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user