Sarath Rajan Senthilkumar commited on
Commit
653a1d9
·
verified ·
1 Parent(s): 7eb4476

Upload 9 files

Browse files
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ __pycache__/
app.py ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from processes import *
3
+
4
+
5
+ def processing_pipeline(input_image):
6
+ # Convert Gradio image to OpenCV format
7
+ input_image = input_image.astype('uint8')
8
+
9
+ # Apply CLAHE
10
+ clahe_image = clahe(input_image)
11
+
12
+ # Apply sharpening
13
+ sharpened_image = sharpen(clahe_image, sharpen_strength=0.5)
14
+
15
+ # Apply denoising
16
+ onlyDenoising_image = denoise(clahe_image)
17
+
18
+ # Apply sharpening + denoising
19
+ denoised_image = denoise(sharpened_image, strength=10)
20
+
21
+ # apply denoiseing + shaprening
22
+ denoiseSharp_image = sharpen(denoise(input_image))
23
+
24
+ # Generate histograms
25
+ original_hist = plot_histogram(input_image)
26
+ clahe_hist = plot_histogram(clahe_image)
27
+ sharpened_hist = plot_histogram(sharpened_image)
28
+ denoised_hist = plot_histogram(denoised_image)
29
+ denoiseSharp_hist = plot_histogram(denoiseSharp_image)
30
+ onlyDenoising_hist = plot_histogram( onlyDenoising_image)
31
+
32
+ # Calculate SNR, CNR, and PSNR for each image
33
+ snr_original = "Cannot Calculate"
34
+ snr_clahe = calculate_snr(clahe_image, input_image - clahe_image)
35
+ snr_sharpened = calculate_snr(sharpened_image, input_image - sharpened_image)
36
+ snr_denoised = calculate_snr(denoised_image, input_image - denoised_image)
37
+ snr_denoiseSharp = calculate_snr(denoiseSharp_image,input_image - denoiseSharp_image)
38
+ snr_onlyDenoise = calculate_snr(onlyDenoising_image,input_image - onlyDenoising_image)
39
+
40
+ cnr_original = "Cannot Calculate"
41
+ cnr_clahe = calculate_cnr(clahe_image, input_image - clahe_image)
42
+ cnr_sharpened = calculate_cnr(sharpened_image, input_image - sharpened_image)
43
+ cnr_denoised = calculate_cnr(denoised_image, input_image - denoised_image)
44
+ cnr_denoiseSharp = calculate_cnr(denoiseSharp_image,input_image - denoiseSharp_image)
45
+ cnr_onlyDenoise = calculate_cnr(onlyDenoising_image,input_image - onlyDenoising_image)
46
+
47
+ psnr_original = "Cannot Calculate"
48
+ psnr_clahe = calculate_psnr(clahe_image, input_image)
49
+ psnr_sharpened = calculate_psnr(sharpened_image, input_image)
50
+ psnr_denoised = calculate_psnr(denoised_image, input_image)
51
+ psnr_denoiseSharp = calculate_psnr(denoiseSharp_image,input_image - denoiseSharp_image)
52
+ psnr_onlyDenoise = calculate_psnr(onlyDenoising_image,input_image - onlyDenoising_image)
53
+
54
+
55
+
56
+ return (input_image, clahe_image, sharpened_image, denoised_image,denoiseSharp_image,onlyDenoising_image,
57
+ original_hist, clahe_hist, sharpened_hist, denoised_hist,denoiseSharp_hist,onlyDenoising_hist ,
58
+ snr_original, snr_clahe, snr_sharpened, snr_denoised,snr_denoiseSharp,snr_onlyDenoise,
59
+ cnr_original, cnr_clahe, cnr_sharpened, cnr_denoised, cnr_denoiseSharp, cnr_onlyDenoise,
60
+ psnr_original, psnr_clahe, psnr_sharpened, psnr_denoised, psnr_denoiseSharp,psnr_onlyDenoise )
61
+
62
+
63
+ with gr.Blocks() as demo:
64
+
65
+ inputImg = gr.Image(label="Input Image")
66
+ processs = gr.Button("Process")
67
+ gr.ClearButton()
68
+
69
+ with gr.Column():
70
+ # Original Image
71
+ with gr.Row():
72
+ org_image = gr.Image(label="Original")
73
+ org_image_histogram = gr.Image(label="Original Histogram")
74
+ with gr.Column():
75
+ org_image_snr = gr.Textbox(label="SNR - Original")
76
+ org_image_cnr = gr.Textbox(label="CNR - Original")
77
+ org_image_psnr = gr.Textbox(label="PSNR - Original")
78
+
79
+ with gr.Row():
80
+ # CLAHE
81
+ clahe_image = gr.Image(label="CLAHE")
82
+ clahe_image_histogram = gr.Image(label="CLAHE Histogram")
83
+ with gr.Column():
84
+ clahe_image_snr = gr.Textbox(label="SNR - CLAHE")
85
+ clahe_image_cnr = gr.Textbox(label="CNR - CLAHE")
86
+ clahe_image_psnr = gr.Textbox(label="PSNR - CLAHE")
87
+
88
+ with gr.Row():
89
+ # CLAHE + Sharpen
90
+ clahe_sharpen_image = gr.Image(label="CLAHE + Sharpen")
91
+ clahe_sharpen_image_histogram = gr.Image(label="CLAHE + Sharpen Histogram")
92
+ with gr.Column():
93
+ clahe_sharpen_image_snr = gr.Textbox(label="SNR - CLAHE + Sharpen")
94
+ clahe_sharpen_image_cnr = gr.Textbox(label="CNR - CLAHE + Sharpen")
95
+ clahe_sharpen_image_psnr = gr.Textbox(label="PSNR - CLAHE + Sharpen")
96
+
97
+ with gr.Row():
98
+ # CLAHE + Denoising
99
+ clahe_denoise_image = gr.Image(label= "CLAHE + Denoise")
100
+ clahe_denoise_image_histogram = gr.Image(label="CLAHE + Denoise Histogram")
101
+ with gr.Column():
102
+ clahe_denoise_image_snr = gr.Textbox(label="SNR - CLAHE + Denoise")
103
+ clahe_denoise_image_cnr = gr.Textbox(label="CNR - CLAHE + Denoise")
104
+ clahe_denoise_image_psnr = gr.Textbox(label="PSNR - CLAHE + Denoise")
105
+
106
+
107
+
108
+ with gr.Row():
109
+ # CLAHE + Sharpen + Denoise
110
+ clahe_sharpen_denoise_image = gr.Image(label="CLAHE + Sharpen + Denoise")
111
+ clahe_sharpen_denoise_image_histogram = gr.Image(label="CLAHE + Sharpen + Denoise Histogram")
112
+ with gr.Column():
113
+ clahe_sharpen_denoise_image_snr = gr.Textbox(label="SNR - CLAHE + Sharpen + Denoise")
114
+ clahe_sharpen_denoise_image_cnr = gr.Textbox(label="CNR - CLAHE + Sharpen + Denoise")
115
+ clahe_sharpen_denoise_image_psnr = gr.Textbox(label="PSNR - CLAHE + Sharpen + Denoise")
116
+
117
+ with gr.Row():
118
+ clahe_denoise_sharpen_image = gr.Image(label= "CLAHE + Denoise + Sharpen")
119
+ clahe_denoise_sharpen_image_histogram = gr.Image(label="CLAHE + Denoise + Sharpen Histogram")
120
+ with gr.Column():
121
+ clahe_denoise_sharpen_image_snr = gr.Textbox(label="SNR - CLAHE + Denoise + Sharpen")
122
+ clahe_denoise_sharpen_image_cnr = gr.Textbox(label="CNR - CLAHE + Denoise + Sharpen")
123
+ clahe_denoise_sharpen_image_psnr = gr.Textbox(label="PSNR - CLAHE + Denoise + Sharpen")
124
+
125
+ processs.click(processing_pipeline,inputs=[inputImg], outputs = [org_image,clahe_image,clahe_sharpen_image,clahe_sharpen_denoise_image, clahe_denoise_sharpen_image,clahe_denoise_image,
126
+ org_image_histogram,clahe_image_histogram,clahe_sharpen_image_histogram,clahe_sharpen_denoise_image_histogram,clahe_denoise_sharpen_image_histogram,clahe_denoise_image_histogram ,
127
+ org_image_snr,clahe_image_snr,clahe_sharpen_image_snr,clahe_sharpen_denoise_image_snr,clahe_denoise_sharpen_image_snr ,clahe_denoise_image_snr,
128
+ org_image_cnr,clahe_image_cnr,clahe_sharpen_image_cnr,clahe_sharpen_denoise_image_cnr,clahe_denoise_sharpen_image_cnr,clahe_denoise_image_cnr,
129
+ org_image_psnr,clahe_image_psnr,clahe_sharpen_image_psnr,clahe_sharpen_denoise_image_psnr,clahe_denoise_sharpen_image_psnr,clahe_denoise_image_psnr] )
130
+
131
+
132
+ demo.launch()
processes.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import cv2
3
+ import matplotlib.pyplot as plt
4
+
5
+ def calculate_snr(image, noise):
6
+ # Calculate signal power
7
+ signal_power = np.mean(np.square(image))
8
+
9
+ # Calculate noise power
10
+ noise_power = np.mean(np.square(noise))
11
+
12
+ # Calculate SNR
13
+ snr = 10 * np.log10(signal_power / noise_power)
14
+
15
+ return snr
16
+
17
+ def calculate_cnr(image, noise):
18
+ # Calculate mean signal
19
+ mean_signal = np.mean(image)
20
+
21
+ # Calculate mean noise
22
+ mean_noise = np.mean(noise)
23
+
24
+ # Calculate standard deviation of signal
25
+ std_signal = np.std(image)
26
+
27
+ # Calculate standard deviation of noise
28
+ std_noise = np.std(noise)
29
+
30
+ # Calculate CNR
31
+ cnr = np.abs(mean_signal - mean_noise) / np.sqrt(std_signal**2 + std_noise**2)
32
+
33
+ return cnr
34
+
35
+ def calculate_psnr(image, reconstructed_image):
36
+ # Convert images to float64
37
+ image = np.float64(image)
38
+ reconstructed_image = np.float64(reconstructed_image)
39
+
40
+ # Calculate MSE
41
+ mse = np.mean((image - reconstructed_image) ** 2)
42
+
43
+ # Calculate maximum pixel value
44
+ max_pixel_value = np.max(image)
45
+
46
+ # Calculate PSNR
47
+ psnr = 20 * np.log10(max_pixel_value / np.sqrt(mse))
48
+
49
+ return psnr
50
+
51
+ def clahe(img, clip_limit=2.0, tile_size=(8, 8)):
52
+ # Create CLAHE object
53
+ clahe = cv2.createCLAHE(clipLimit=clip_limit, tileGridSize=tile_size)
54
+
55
+ # Convert image to LAB color space
56
+ lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
57
+
58
+ # Split LAB channels
59
+ l, a, b = cv2.split(lab)
60
+
61
+ # Apply CLAHE to the L channel
62
+ cl = clahe.apply(l)
63
+
64
+ # Merge the CLAHE enhanced L channel with the original A and B channels
65
+ limg = cv2.merge((cl, a, b))
66
+
67
+ # Convert LAB image back to BGR
68
+ return cv2.cvtColor(limg, cv2.COLOR_LAB2BGR)
69
+
70
+ def sharpen(image, sharpen_strength=1.0):
71
+ # Apply sharpening using unsharp masking
72
+ blurred = cv2.GaussianBlur(image, (0, 0), 3)
73
+ sharpened = cv2.addWeighted(image, 1.0 + sharpen_strength, blurred, -sharpen_strength, 0)
74
+ return sharpened
75
+
76
+ def denoise(image, strength=10):
77
+ # Apply non-local means denoising
78
+ denoised = cv2.fastNlMeansDenoisingColored(image, None, strength, strength, 7, 21)
79
+ return denoised
80
+
81
+ def plot_histogram(image):
82
+ # Calculate histogram
83
+ hist = cv2.calcHist([image], [0], None, [256], [0, 256])
84
+
85
+ # Plot histogram
86
+ plt.figure(figsize=(6, 4))
87
+ plt.plot(hist, color='black')
88
+ plt.xlabel('Pixel Value')
89
+ plt.ylabel('Frequency')
90
+ plt.title('Histogram')
91
+ plt.xlim([0, 256])
92
+ plt.grid(True)
93
+ plt.tight_layout()
94
+
95
+ # Convert plot to numpy array
96
+ plt_img = plt.gcf()
97
+ plt_img.canvas.draw()
98
+ img_np = np.array(plt_img.canvas.renderer.buffer_rgba())
99
+ plt.close()
100
+
101
+ return img_np[:, :, :3]
requirements.txt ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ aiofiles==23.2.1
2
+ altair==5.3.0
3
+ annotated-types==0.6.0
4
+ anyio==4.3.0
5
+ attrs==23.2.0
6
+ certifi==2024.2.2
7
+ charset-normalizer==3.3.2
8
+ click==8.1.7
9
+ contourpy==1.2.1
10
+ cycler==0.12.1
11
+ fastapi==0.110.3
12
+ ffmpy==0.3.2
13
+ filelock==3.14.0
14
+ fonttools==4.51.0
15
+ fsspec==2024.3.1
16
+ gradio==4.28.3
17
+ gradio_client==0.16.0
18
+ h11==0.14.0
19
+ httpcore==1.0.5
20
+ httpx==0.27.0
21
+ huggingface-hub==0.22.2
22
+ idna==3.7
23
+ importlib_resources==6.4.0
24
+ Jinja2==3.1.3
25
+ jsonschema==4.22.0
26
+ jsonschema-specifications==2023.12.1
27
+ kiwisolver==1.4.5
28
+ markdown-it-py==3.0.0
29
+ MarkupSafe==2.1.5
30
+ matplotlib==3.8.4
31
+ mdurl==0.1.2
32
+ numpy==1.26.4
33
+ opencv-python==4.9.0.80
34
+ orjson==3.10.2
35
+ packaging==24.0
36
+ pandas==2.2.2
37
+ pillow==10.3.0
38
+ pydantic==2.7.1
39
+ pydantic_core==2.18.2
40
+ pydub==0.25.1
41
+ Pygments==2.17.2
42
+ pyparsing==3.1.2
43
+ python-dateutil==2.9.0.post0
44
+ python-multipart==0.0.9
45
+ pytz==2024.1
46
+ PyYAML==6.0.1
47
+ referencing==0.35.0
48
+ requests==2.31.0
49
+ rich==13.7.1
50
+ rpds-py==0.18.0
51
+ ruff==0.4.2
52
+ semantic-version==2.10.0
53
+ shellingham==1.5.4
54
+ six==1.16.0
55
+ sniffio==1.3.1
56
+ starlette==0.37.2
57
+ tomlkit==0.12.0
58
+ toolz==0.12.1
59
+ tqdm==4.66.2
60
+ typer==0.12.3
61
+ typing_extensions==4.11.0
62
+ tzdata==2024.1
63
+ urllib3==2.2.1
64
+ uvicorn==0.29.0
65
+ websockets==11.0.3
sampleImages/1.jpg ADDED
sampleImages/10.jpg ADDED
sampleImages/100.jpg ADDED
sampleImages/114.jpg ADDED
sampleImages/138.jpg ADDED