File size: 2,225 Bytes
bd3e8b6
 
 
 
774bc32
bd3e8b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import cv2
import numpy as np
import os

image_height=224 
image_width=224


def read_image(image_path,image_height,image_width):
  image=cv2.imread(image_path)
  image=cv2.resize(image, (image_height,image_width))
  image=cv2.cvtColor(image, cv2.COLOR_RGB2BGR)

  return image

def min_max_normalization (image):
    float_image = image.astype(np.float32)

    # Calculate the minimum and maximum pixel values
    min_value = np.min(float_image)
    max_value = np.max(float_image)

    # Perform Min-Max normalization
    normalized_image = (float_image - min_value) / (max_value - min_value)

    return normalized_image


def apply_histogram_normalization(image):

  b_channel, g_channel, r_channel = cv2.split(image)

  normalized_b = cv2.normalize(b_channel, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX)
  normalized_g = cv2.normalize(g_channel, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX)
  normalized_r = cv2.normalize(r_channel, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX)


  normalized_image = cv2.merge((normalized_b, normalized_g, normalized_r))

  return normalized_image


def remove_noise(image):

  median = cv2.medianBlur(image,5)

  return median

def adaptive_gamma_correction(image):
  def apply_adaptive_gamma_correction(channel, gamma):
    corrected_channel = np.power((channel / 255.0), 1.0 / gamma)
    return cv2.normalize(corrected_channel, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX)


  b_channel, g_channel, r_channel = cv2.split(image)


  gamma = 1.5
  gamma_corrected_b = apply_adaptive_gamma_correction(b_channel, gamma)
  gamma_corrected_g = apply_adaptive_gamma_correction(g_channel, gamma)
  gamma_corrected_r = apply_adaptive_gamma_correction(r_channel, gamma)


  gamma_corrected_image = cv2.merge((gamma_corrected_b, gamma_corrected_g, gamma_corrected_r))


  gamma_corrected_image=min_max_normalization(gamma_corrected_image)

  return gamma_corrected_image


def preprocess_image(img_path):

    image = read_image(img_path,image_height,image_width)

    normalized_image= apply_histogram_normalization(image)
    median= remove_noise(normalized_image)
    gamma_corrected_image=adaptive_gamma_correction(median)

    return gamma_corrected_image*255