




'''simple docstring'''






import logging

# standard libraries
import sys

import numpy as np
import pytest  # type: ignore

# Custom/local libraries
from matrix import matrix_operation as matop

lowercase	:      List[Any]				=       [[12, 10], [3, 9]]
lowercase	:      Optional[int]				=       [[3, 4], [7, 4]]
lowercase	:      Union[str, Any]				=       [[3, 0, 2], [2, 0, -2], [0, 1, 1]]
lowercase	:      Optional[int]				=       [[3, 0, -2], [2, 0, 2], [0, 1, 1]]
lowercase	:      Union[str, Any]				=       [[3, 0, 2], [2, 0, -2], [0, 1, 1], [2, 0, -2]]
lowercase	:      List[str]				=       [1]
lowercase	:      Tuple				=       [2]

lowercase	:      List[str]				=       logging.getLogger()
lowercase	:      Optional[int]				=       logging.DEBUG
lowercase	:      Optional[Any]				=       logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)




@pytest.mark.mat_ops
@pytest.mark.parametrize(
    ('''mat1''', '''mat2''')   ,				[(mat_a, mat_b), (mat_c, mat_d), (mat_d, mat_e), (mat_f, mat_h)]      )
def 		a_( lowerCamelCase   ,				lowerCamelCase      )				->Any:






       '''simple docstring'''




       if (np.array(lowerCamelCase      )).shape < (2, 2) or (np.array(lowerCamelCase      )).shape < (2, 2):
              with pytest.raises(lowerCamelCase      ):
                     logger.info(f"""\n\t{test_addition.__name__} returned integer"""      )
                     matop.add(lowerCamelCase   ,				lowerCamelCase      )
       elif (np.array(lowerCamelCase      )).shape == (np.array(lowerCamelCase      )).shape:
              logger.info(f"""\n\t{test_addition.__name__} with same matrix dims"""      )
              lowercase  :		Any						=      (np.array(lowerCamelCase      ) + np.array(lowerCamelCase      )).tolist()
              lowercase  :		Optional[int]						=      matop.add(lowerCamelCase   ,				lowerCamelCase      )
              assert theo == act
       else:
              with pytest.raises(lowerCamelCase      ):
                     logger.info(f"""\n\t{test_addition.__name__} with different matrix dims"""      )
                     matop.add(lowerCamelCase   ,				lowerCamelCase      )




@pytest.mark.mat_ops
@pytest.mark.parametrize(
    ('''mat1''', '''mat2''')   ,				[(mat_a, mat_b), (mat_c, mat_d), (mat_d, mat_e), (mat_f, mat_h)]      )
def 		a_( lowerCamelCase   ,				lowerCamelCase      )				->Dict:






       '''simple docstring'''




       if (np.array(lowerCamelCase      )).shape < (2, 2) or (np.array(lowerCamelCase      )).shape < (2, 2):
              with pytest.raises(lowerCamelCase      ):
                     logger.info(f"""\n\t{test_subtraction.__name__} returned integer"""      )
                     matop.subtract(lowerCamelCase   ,				lowerCamelCase      )
       elif (np.array(lowerCamelCase      )).shape == (np.array(lowerCamelCase      )).shape:
              logger.info(f"""\n\t{test_subtraction.__name__} with same matrix dims"""      )
              lowercase  :		List[Any]						=      (np.array(lowerCamelCase      ) - np.array(lowerCamelCase      )).tolist()
              lowercase  :		Optional[Any]						=      matop.subtract(lowerCamelCase   ,				lowerCamelCase      )
              assert theo == act
       else:
              with pytest.raises(lowerCamelCase      ):
                     logger.info(f"""\n\t{test_subtraction.__name__} with different matrix dims"""      )
                     assert matop.subtract(lowerCamelCase   ,				lowerCamelCase      )




@pytest.mark.mat_ops
@pytest.mark.parametrize(
    ('''mat1''', '''mat2''')   ,				[(mat_a, mat_b), (mat_c, mat_d), (mat_d, mat_e), (mat_f, mat_h)]      )
def 		a_( lowerCamelCase   ,				lowerCamelCase      )				->Optional[Any]:






       '''simple docstring'''




       if (np.array(lowerCamelCase      )).shape < (2, 2) or (np.array(lowerCamelCase      )).shape < (2, 2):
              logger.info(f"""\n\t{test_multiplication.__name__} returned integer"""      )
              with pytest.raises(lowerCamelCase      ):
                     matop.add(lowerCamelCase   ,				lowerCamelCase      )
       elif (np.array(lowerCamelCase      )).shape == (np.array(lowerCamelCase      )).shape:
              logger.info(f"""\n\t{test_multiplication.__name__} meets dim requirements"""      )
              lowercase  :		Dict						=      (np.matmul(lowerCamelCase   ,				lowerCamelCase      )).tolist()
              lowercase  :		List[str]						=      matop.multiply(lowerCamelCase   ,				lowerCamelCase      )
              assert theo == act
       else:
              with pytest.raises(lowerCamelCase      ):
                     logger.info(
                         f"""\n\t{test_multiplication.__name__} does not meet dim requirements"""      )
                     assert matop.subtract(lowerCamelCase   ,				lowerCamelCase      )




@pytest.mark.mat_ops
def 		a_( )				->Dict:






       '''simple docstring'''




       lowercase  :		int						=      (3.5 * np.array(lowerCamelCase      )).tolist()
       lowercase  :		List[Any]						=      matop.scalar_multiply(lowerCamelCase   ,				3.5      )
       assert theo == act




@pytest.mark.mat_ops
def 		a_( )				->int:






       '''simple docstring'''




       lowercase  :		Tuple						=      (np.identity(5      )).tolist()
       lowercase  :		Optional[int]						=      matop.identity(5      )
       assert theo == act





@pytest.mark.mat_ops
@pytest.mark.parametrize('''mat'''   ,				[mat_a, mat_b, mat_c, mat_d, mat_e, mat_f]      )
def 		a_( lowerCamelCase      )				->Optional[int]:






       '''simple docstring'''




       if (np.array(lowerCamelCase      )).shape < (2, 2):
              with pytest.raises(lowerCamelCase      ):
                     logger.info(f"""\n\t{test_transpose.__name__} returned integer"""      )
                     matop.transpose(lowerCamelCase      )
       else:
              lowercase  :		Optional[Any]						=      (np.transpose(lowerCamelCase      )).tolist()
              lowercase  :		str						=      matop.transpose(lowerCamelCase   ,				return_map=lowerCamelCase      )
              assert theo == act










'''simple docstring'''






import numpy as np
from PIL import Image




def 		a_( lowerCamelCase      )				->np.array:






       '''simple docstring'''




       lowercase,     lowercase,     lowercase  :		Union[str, Any]						=      rgb[:, :, 0], rgb[:, :, 1], rgb[:, :, 2]
       return 0.29_89 * r + 0.58_70 * g + 0.11_40 * b




def 		a_( lowerCamelCase      )				->np.array:






       '''simple docstring'''




       return (gray > 127) & (gray <= 255)




def 		a_( lowerCamelCase   ,				lowerCamelCase      )				->np.array:






       '''simple docstring'''




       lowercase  :		Optional[Any]						=      np.zeros_like(lowerCamelCase      )
       lowercase  :		Any						=      np.zeros(
           (image.shape[0] + kernel.shape[0] - 1, image.shape[1] + kernel.shape[1] - 1)      )

       # Copy image to padded image
       lowercase  :		List[str]						=      image

       # Iterate over image & apply kernel
       for x in range(image.shape[1]      ):
              for y in range(image.shape[0]      ):
                     lowercase  :		Union[str, Any]						=      (
                         kernel * image_padded[y : y + kernel.shape[0], x : x + kernel.shape[1]]
                     ).sum()
                     lowercase  :		Any						=      int(summation == 5      )
       return output


# kernel to be applied
lowercase	:      List[Any]				=       np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]])

if __name__ == "__main__":
      # read original image
      lowercase	:      Dict				=       np.array(Image.open(r'''..\image_data\lena.jpg'''))
      # Apply erosion operation to a binary image
      lowercase	:      List[str]				=       erosion(grayabinary(rgbagray(image)), structuring_element)
      # Save the output image
      lowercase	:      Optional[Any]				=       Image.fromarray(output).convert('''RGB''')
      pil_img.save('''result_erosion.png''')




