Spaces:
Sleeping
Sleeping
fixes to high pass and low pass,laplacian and sobel
#2
by
Ritabrata04
- opened
- transforms.py +8 -2
transforms.py
CHANGED
|
@@ -24,8 +24,10 @@ def sobel_edge_detection(image):
|
|
| 24 |
sobel_x = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=3)
|
| 25 |
sobel_y = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=3)
|
| 26 |
sobel_combined = np.sqrt(sobel_x**2 + sobel_y**2)
|
|
|
|
| 27 |
return sobel_combined
|
| 28 |
|
|
|
|
| 29 |
# 3. Gaussian Blur
|
| 30 |
def gaussian_blur(image, kernel_size=5):
|
| 31 |
blurred_img = cv2.GaussianBlur(image, (kernel_size, kernel_size), 0)
|
|
@@ -37,8 +39,10 @@ def laplacian_of_gaussian(image):
|
|
| 37 |
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 38 |
blurred_img = cv2.GaussianBlur(image, (3, 3), 0)
|
| 39 |
log_img = cv2.Laplacian(blurred_img, cv2.CV_64F)
|
|
|
|
| 40 |
return log_img
|
| 41 |
|
|
|
|
| 42 |
# 5. Median Filtering
|
| 43 |
def median_filter(image, kernel_size=5):
|
| 44 |
median_img = cv2.medianBlur(image, kernel_size)
|
|
@@ -66,7 +70,7 @@ def high_pass_filter(image):
|
|
| 66 |
dft_shift = np.fft.fftshift(dft)
|
| 67 |
rows, cols = image.shape
|
| 68 |
crow, ccol = rows // 2, cols // 2
|
| 69 |
-
mask = np.ones((rows, cols), np.
|
| 70 |
mask[crow-30:crow+30, ccol-30:ccol+30] = 0
|
| 71 |
fshift = dft_shift * mask
|
| 72 |
f_ishift = np.fft.ifftshift(fshift)
|
|
@@ -74,6 +78,7 @@ def high_pass_filter(image):
|
|
| 74 |
img_back = np.abs(img_back)
|
| 75 |
return img_back
|
| 76 |
|
|
|
|
| 77 |
def low_pass_filter(image):
|
| 78 |
if len(image.shape) == 3: # Convert to grayscale if RGB
|
| 79 |
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
|
@@ -81,7 +86,7 @@ def low_pass_filter(image):
|
|
| 81 |
dft_shift = np.fft.fftshift(dft)
|
| 82 |
rows, cols = image.shape
|
| 83 |
crow, ccol = rows // 2, cols // 2
|
| 84 |
-
mask = np.zeros((rows, cols), np.
|
| 85 |
mask[crow-30:crow+30, ccol-30:ccol+30] = 1
|
| 86 |
fshift = dft_shift * mask
|
| 87 |
f_ishift = np.fft.ifftshift(fshift)
|
|
@@ -89,6 +94,7 @@ def low_pass_filter(image):
|
|
| 89 |
img_back = np.abs(img_back)
|
| 90 |
return img_back
|
| 91 |
|
|
|
|
| 92 |
def wavelet_transform(image):
|
| 93 |
import pywt
|
| 94 |
if len(image.shape) == 3: # Convert to grayscale if RGB
|
|
|
|
| 24 |
sobel_x = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=3)
|
| 25 |
sobel_y = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=3)
|
| 26 |
sobel_combined = np.sqrt(sobel_x**2 + sobel_y**2)
|
| 27 |
+
sobel_combined = cv2.normalize(sobel_combined, None, 0, 255, cv2.NORM_MINMAX) # Added normalization
|
| 28 |
return sobel_combined
|
| 29 |
|
| 30 |
+
|
| 31 |
# 3. Gaussian Blur
|
| 32 |
def gaussian_blur(image, kernel_size=5):
|
| 33 |
blurred_img = cv2.GaussianBlur(image, (kernel_size, kernel_size), 0)
|
|
|
|
| 39 |
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 40 |
blurred_img = cv2.GaussianBlur(image, (3, 3), 0)
|
| 41 |
log_img = cv2.Laplacian(blurred_img, cv2.CV_64F)
|
| 42 |
+
log_img = cv2.normalize(log_img, None, 0, 255, cv2.NORM_MINMAX) # Added normalization
|
| 43 |
return log_img
|
| 44 |
|
| 45 |
+
|
| 46 |
# 5. Median Filtering
|
| 47 |
def median_filter(image, kernel_size=5):
|
| 48 |
median_img = cv2.medianBlur(image, kernel_size)
|
|
|
|
| 70 |
dft_shift = np.fft.fftshift(dft)
|
| 71 |
rows, cols = image.shape
|
| 72 |
crow, ccol = rows // 2, cols // 2
|
| 73 |
+
mask = np.ones((rows, cols), np.float64) # Changed to float64
|
| 74 |
mask[crow-30:crow+30, ccol-30:ccol+30] = 0
|
| 75 |
fshift = dft_shift * mask
|
| 76 |
f_ishift = np.fft.ifftshift(fshift)
|
|
|
|
| 78 |
img_back = np.abs(img_back)
|
| 79 |
return img_back
|
| 80 |
|
| 81 |
+
|
| 82 |
def low_pass_filter(image):
|
| 83 |
if len(image.shape) == 3: # Convert to grayscale if RGB
|
| 84 |
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
|
|
|
| 86 |
dft_shift = np.fft.fftshift(dft)
|
| 87 |
rows, cols = image.shape
|
| 88 |
crow, ccol = rows // 2, cols // 2
|
| 89 |
+
mask = np.zeros((rows, cols), np.float64) # Changed to float64
|
| 90 |
mask[crow-30:crow+30, ccol-30:ccol+30] = 1
|
| 91 |
fshift = dft_shift * mask
|
| 92 |
f_ishift = np.fft.ifftshift(fshift)
|
|
|
|
| 94 |
img_back = np.abs(img_back)
|
| 95 |
return img_back
|
| 96 |
|
| 97 |
+
|
| 98 |
def wavelet_transform(image):
|
| 99 |
import pywt
|
| 100 |
if len(image.shape) == 3: # Convert to grayscale if RGB
|