OpenCV Error: 'src data type = 15 is not supported' - Solution and Explanation
This article addresses the 'src data type = 15 is not supported' error encountered in OpenCV, a common issue arising from incompatible data types. The error message indicates that the input data type used for an OpenCV operation is not supported. This typically happens when you're working with images and the data type doesn't align with OpenCV's expectations.
To resolve this issue, ensure the input data type is compatible with the OpenCV function. For instance, when performing operations like the Discrete Fourier Transform (DFT) or Inverse DFT (IDFT), OpenCV requires the input to be of type np.float32.
Solution: Modify the data type of the input data to np.float32.
Example Code:
import numpy as np
import cv2
import matplotlib.pyplot as plt
# Load BMP image
image_path = 'your_image.bmp' # Replace with your actual BMP image path
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
height, width = image.shape
# 8x8 block 2D DFT
dft_image = np.zeros_like(image, dtype=np.complex128)
for i in range(0, height, 8):
for j in range(0, width, 8):
block = np.float32(image[i:i+8, j:j+8])
dft_block = cv2.dft(block.astype(np.float32), flags=cv2.DFT_COMPLEX_OUTPUT)
dft_image[i:i+8, j:j+8] = dft_block[:, :, 0] + 1j * dft_block[:, :, 1]
# Keep only the phase and perform inverse DFT
dft_phase = np.angle(dft_image)
dft_inverse = cv2.idft(np.exp(1j * dft_phase).astype(np.float32), flags=cv2.DFT_SCALE)
# Display images
plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.subplot(1, 2, 2)
plt.imshow(np.abs(dft_inverse), cmap='gray')
plt.title('Inverse DFT')
plt.show()
Explanation:
- Load the Image: Load your BMP image using
cv2.imread()and ensure it's grayscale usingcv2.IMREAD_GRAYSCALE. - 8x8 Block DFT: Perform the DFT on 8x8 blocks of the image using
cv2.dft(). The input tocv2.dft()needs to be of typenp.float32, so we convert the block to this data type. - Extract Phase: The phase spectrum of the DFT is calculated using
np.angle(dft_image). - Inverse DFT: The inverse DFT (
cv2.idft()) is applied to the phase spectrum to reconstruct the image. Again, the input needs to benp.float32for the inverse DFT. - Display Images: Use
matplotlib.pyplotto visualize the original image and the result of the inverse DFT.
Remember to: Replace 'your_image.bmp' with the path to your BMP image file.
This revised code effectively addresses the 'src data type = 15 is not supported' error by explicitly converting the input data type to np.float32 before performing the DFT and IDFT operations. This ensures compatibility with OpenCV's requirements and facilitates successful image processing.
原文地址: https://www.cveoy.top/t/topic/bAnG 著作权归作者所有。请勿转载和采集!