DHEIVER commited on
Commit
4c1ab88
1 Parent(s): 5517146

Rename preprocess.py to obstruction_detector.py

Browse files
Files changed (2) hide show
  1. obstruction_detector.py +55 -0
  2. preprocess.py +0 -13
obstruction_detector.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2 as cv
2
+ import numpy as np
3
+ from scipy.signal import find_peaks
4
+ from PIL import Image # Import PIL
5
+
6
+ class ObstructionDetector:
7
+ def __init__(self, threshold=500):
8
+ self.threshold = threshold
9
+
10
+ def preprocess_image(self, image):
11
+ # Convert the image to grayscale if it's a color image
12
+ if len(image.shape) == 3:
13
+ image = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
14
+
15
+ # Apply Gaussian blur to reduce noise
16
+ preprocessed_image = cv.GaussianBlur(image, (5, 5), 0)
17
+
18
+ # Perform other preprocessing steps as needed (e.g., contrast adjustment, histogram equalization)
19
+
20
+ return preprocessed_image
21
+
22
+ def plot_histogram(self, image):
23
+ # Calculate the histogram
24
+ histogram = cv.calcHist([image], [0], None, [256], [0, 256])
25
+
26
+ # Smoothing the histogram using a simple moving average (window size = 5)
27
+ kernel = np.ones((5, 1)) / 5
28
+ smoothed_histogram = cv.filter2D(histogram, -1, kernel)
29
+
30
+ return smoothed_histogram
31
+
32
+ def count_histogram_peaks(self, smoothed_histogram):
33
+ # Find peaks in the smoothed histogram with frequency greater than the threshold
34
+ peaks, _ = find_peaks(smoothed_histogram.flatten(), height=self.threshold)
35
+ return peaks
36
+
37
+ def detect_obstruction(self, pil_image): # Accept PIL image directly
38
+ # Convert PIL image to NumPy array
39
+ img = np.array(pil_image)
40
+
41
+ # Preprocess the image
42
+ preprocessed_img = self.preprocess_image(img)
43
+
44
+ # Count the number of peaks in the smoothed histogram above the threshold
45
+ smoothed_histogram = self.plot_histogram(preprocessed_img)
46
+ peaks = self.count_histogram_peaks(smoothed_histogram)
47
+
48
+ # Check if peaks are too close together
49
+ peak_spacing = np.diff(peaks)
50
+ if len(peak_spacing) == 0 or np.all(peak_spacing < 10):
51
+ report = "A imagem NÃO contém obstrução significativa | e NÃO possui múltiplas distribuições de densidade claramente distintas."
52
+ else:
53
+ report = "A imagem contém obstrução significativa | possui múltiplas distribuições de densidade claramente distintas."
54
+
55
+ return report
preprocess.py DELETED
@@ -1,13 +0,0 @@
1
- import cv2
2
- import numpy as np
3
-
4
- def unsharp_masking(img, kernel_size=5, threshold=2.0):
5
- if kernel_size % 2 == 0:
6
- kernel_size += 1 # Ensure the kernel size is odd
7
- gaussian = cv2.GaussianBlur(img, (kernel_size, kernel_size), 2.0)
8
- unsharp_mask = cv2.addWeighted(img, threshold, gaussian, -1.0, 0)
9
- # Clip the pixel values to the valid range [0, 255]
10
- unsharp_mask = np.clip(unsharp_mask, 0, 255)
11
- # Normalize the image to bring pixel values back to [0, 255]
12
- cv2.normalize(unsharp_mask, unsharp_mask, 0, 255, cv2.NORM_MINMAX)
13
- return unsharp_mask