Reputation: 1
This is my code :
from google.colab import files
import cv2
import numpy as np
import matplotlib.pyplot as plt
import pywt
def wavelet_inpainting(image, mask, wavelet='haar', level=3, threshold=20):
# Apply discrete wavelet transform
coeffs = pywt.wavedec2(image, wavelet, level=level)
# Thresholding
for i in range(1, len(coeffs)):
coeffs[i] = pywt.threshold(coeffs[i], threshold, mode='soft')
# Resize the mask to match the size of the detailed coefficients
mask_resized = cv2.resize(mask, (coeffs[1][1].shape[1], coeffs[1][1].shape[0]))
# Inpainting by replacing coefficients in masked regions with zeros
for i in range(1, len(coeffs)):
coeffs[i] *= np.logical_not(mask_resized)
# Reconstruct the image
reconstructed_image = pywt.waverec2(coeffs, wavelet)
return reconstructed_image
uploaded_scratched = files.upload()
uploaded_mask = files.upload()
# Get the uploaded file names
scratched_file = list(uploaded_scratched.keys())[0]
mask_file = list(uploaded_mask.keys())[0]
# Load the image and mask
img = cv2.imread(scratched_file)
mask = cv2.imread(mask_file, cv2.IMREAD_GRAYSCALE)
# Print image sizes for debugging
print("Image size:", img.shape)
print("Mask size:", mask.shape)
# Apply wavelet inpainting
inpainted_image = wavelet_inpainting(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), mask, wavelet='db4', level=3, threshold=20)
# Display the images using matplotlib
plt.figure(figsize=(15, 5))
plt.subplot(1, 3, 1)
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.title('Image with missing pixels')
plt.xticks([]), plt.yticks([])
plt.subplot(1, 3, 2)
plt.imshow(mask, cmap='gray')
plt.title('Mask of the missing pixels')
plt.xticks([]), plt.yticks([])
plt.subplot(1, 3, 3)
plt.imshow(inpainted_image, cmap='gray')
plt.title('Image with missing pixels inpainted (Wavelet)')
plt.xticks([]), plt.yticks([])
plt.show()
ValueError Traceback (most recent call last) in <cell line: 44>() 42 43 # Apply wavelet inpainting ---> 44 inpainted_image = wavelet_inpainting(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), mask, wavelet='db4', level=3, threshold=20) 45 46 # Display the images using matplotlib
in wavelet_inpainting(image, mask, wavelet, level, threshold) 18 # Inpainting by replacing coefficients in masked regions with zeros 19 for i in range(1, len(coeffs)): ---> 20 coeffs[i] *= np.logical_not(mask_resized) 21 22 # Reconstruct the image
ValueError: operands could not be broadcast together with shapes (3,275,411) (141,209) (3,275,411)
I was trying to do image inpainting by using wavelet transform. But I was getting the error and could not run the code properly.
Upvotes: 0
Views: 28