




'''simple docstring'''





import numpy as np
from cva import destroyAllWindows, imread, imshow, waitKey




class     UpperCamelCase					:



  def __init__( self		,    __lowerCAmelCase		,    __lowerCAmelCase   ):







      '''simple docstring'''


      _a							:       Any       					=			0
      # max greyscale value for #FFFFFF
      _a							:       Optional[Any]       					=			int(self.get_greyscale(255		,    255		,    255   )   )

      if not self.min_threshold < threshold < self.max_threshold:
          _a							:       List[str]       					=			f'''Factor value should be from 0 to {self.max_threshold}'''
          raise ValueError(__lowerCAmelCase   )

      _a							:       Optional[int]       					=			input_img
      _a							:       Optional[int]       					=			threshold
      _a					,   _a							:       str       					=			self.input_img.shape[1], self.input_img.shape[0]

      # error table size (+4 columns and +1 row) greater than input image because of
      # lack of if statements
      _a							:       Tuple       					=			[
          [0 for _ in range(self.height + 4   )] for __ in range(self.width + 1   )
      ]
      _a							:       List[Any]       					=			np.ones((self.width, self.height, 3)		,    np.uinta   ) * 255



  @classmethod
  def 						a_       ( cls		,    __lowerCAmelCase		,    __lowerCAmelCase		,    __lowerCAmelCase   ):







      '''simple docstring'''


      return 0.114 * blue + 0.587 * green + 0.2126 * red



  def 						a_       ( self   ):







      '''simple docstring'''


      for y in range(self.height   ):
          for x in range(self.width   ):
              _a							:       Dict       					=			int(self.get_greyscale(*self.input_img[y][x]   )   )
              if self.threshold > greyscale + self.error_table[y][x]:
                  _a							:       str       					=			(0, 0, 0)
                  _a							:       Dict       					=			greyscale + self.error_table[x][y]
              else:
                  _a							:       List[str]       					=			(255, 255, 255)
                  _a							:       Optional[int]       					=			greyscale + self.error_table[x][y] - 255
              self.error_table[y][x + 1] += int(8 / 32 * current_error   )
              self.error_table[y][x + 2] += int(4 / 32 * current_error   )
              self.error_table[y + 1][x] += int(8 / 32 * current_error   )
              self.error_table[y + 1][x + 1] += int(4 / 32 * current_error   )
              self.error_table[y + 1][x + 2] += int(2 / 32 * current_error   )
              self.error_table[y + 1][x - 1] += int(4 / 32 * current_error   )
              self.error_table[y + 1][x - 2] += int(2 / 32 * current_error   )


if __name__ == "__main__":
  # create Burke's instances with original images in greyscale
  a		:  Optional[int]   			=			[
      Burkes(imread('''image_data/lena.jpg''', 1), threshold)
      for threshold in (1, 1_2_6, 1_3_0, 1_4_0)
  ]

  for burkes in burkes_instances:
    burkes.process()

  for burkes in burkes_instances:
    imshow(
        F"""Original image with dithering threshold: {burkes.threshold}""",
        burkes.output_img,
    )

  waitKey(0)
  destroyAllWindows()








'''simple docstring'''





from cva import COLOR_BGR2GRAY, cvtColor, imread, imshow, waitKey
from numpy import divide, inta, multiply, ravel, sort, zeros_like





def UpperCAmelCase						(					UpperCAmelCase_      ,  UpperCAmelCase_=3	):
    _a							:       Optional[Any]       					=			int(mask / 2	)
    # copy image size
    _a							:       Optional[int]       					=			zeros_like(UpperCAmelCase_	)
    for i in range(UpperCAmelCase_      ,  gray_img.shape[0] - bd	):
        for j in range(UpperCAmelCase_      ,  gray_img.shape[1] - bd	):
            # get mask according with mask
            _a							:       List[str]       					=			ravel(gray_img[i - bd : i + bd + 1, j - bd : j + bd + 1]	)
            # calculate mask median
            _a							:       Optional[Any]       					=			sort(UpperCAmelCase_	)[inta(divide((multiply(UpperCAmelCase_      ,  UpperCAmelCase_	))      ,  2	) + 1	)]
            _a							:       int       					=			median
    return median_img


if __name__ == "__main__":
  # read original image
  a		:  List[Any]   			=			imread('''../image_data/lena.jpg''')
  # turn image in gray scale value
  a		:  Optional[Any]   			=			cvtColor(img, COLOR_BGR2GRAY)

  # get values with two different mask size
  a		:  str   			=			median_filter(gray, 3)
  a		:  Optional[Any]   			=			median_filter(gray, 5)

  # show result images
  imshow('''median filter with 3x3 mask''', medianaxa)
  imshow('''median filter with 5x5 mask''', medianaxa)
  waitKey(0)


