I am using OpenCV and NumPy to apply a Sobel filter to an image in Python, and I expected the output to have smaller dimensions after applying the filter. According to the equation output = input - kernel_size // stride + 1, I thought the output should be smaller than the input by the kernel size (which is 3 in this case). However, when I apply the Sobel filter, the output image dimensions remain the same as the input image.
Here is the code I am using:
import cv2
import numpy as np
input_image = cv2.imread('img3.pgm', cv2.IMREAD_GRAYSCALE)
print(f"length of input image rows : {len(input_image)}")
print(f"length of input image columns: {len(input_image[0])}")
sobel_x = cv2.Sobel(input_image, cv2.CV_64F, 1, 0, ksize=3) # Sobel in x-direction
sobel_y = cv2.Sobel(input_image, cv2.CV_64F, 0, 1, ksize=3) # Sobel in y-direction
edge_magnitude = cv2.magnitude(sobel_x, sobel_y)
edge_magnitude_cropped = np.clip(edge_magnitude, 0, 255).astype(np.uint8)
print(f"length of filtered_image row {len(edge_magnitude_cropped)}" )
print(f"length of filtered_image col {len(edge_magnitude_cropped[0])}" )
cv2.imwrite('filtered_image_cv_cropped.pgm', edge_magnitude_cropped)
print("\nFiltered image saved as 'filtered_image_cv.pgm'.")
with open('original_image_cv_cropped.txt', 'w') as f:
for row in input_image:
f.write(" ".join(str(pixel) for pixel in row) + "\n")
print("\nOriginal image data saved as 'original_image_cv.txt'.")
with open('filtered_image_cv_cropped.txt', 'w') as f:
for row in edge_magnitude_cropped:
f.write(" ".join(str(pixel) for pixel in row) + "\n")
print("\nFiltered image data saved as 'filtered_image_cv.txt'.")
In the code above, I apply the Sobel filter with a kernel size of 3. The input image is 736x736, and I expected the output to be smaller, as per the formula. But after applying the Sobel filter, the dimensions of the output are the same as the input (736x736). When I manually perform the operation, I get the expected smaller image. Why is this happening with OpenCV?
The documentation of cv2.Sobel
explicitly describes the output image dst
as (emphasis by me):
dst
output image of the same size and the same number of channels assrc
where src
is the input image.
This size consistency is achieved by padding the input image before applying the Sobel operator (called pixel extrapolation in the documentation).
While you can control which padding method is used via the borderType
parameter of cv2.Sobel
, turning off padding completely does not seem to be an option. If you still would like to have a result of the expected shape, I would suggest you correct it manually by symmetrically removing excess boundary values from the image/array resulting from applying cv2.Sobel
.