|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
高光谱多通道图片阈值分割。
发生异常: error
OpenCV(4.7.0) D:\a\opencv-python\opencv-python\opencv\modules\core\src\arithm.cpp:230: error: (-215:Assertion failed) (mtype == CV_8U || mtype == CV_8S) && _mask.sameSize(*psrc1) in function 'cv::binary_op'
File "D:\0000可见光2\程序\光谱提取\提取光谱.py", line 38, in <module>
background_removed_image = cv2.bitwise_and(rawimg_RT, rawimg_RT, mask=binary_mask)
cv2.error: OpenCV(4.7.0) D:\a\opencv-python\opencv-python\opencv\modules\core\src\arithm.cpp:230: error: (-215:Assertion failed) (mtype == CV_8U || mtype == CV_8S) && _mask.sameSize(*psrc1) in function 'cv::binary_op'
输出结果:
(950, 384) (950, 384, 288)
uint8 float32
(950, 384, 288) (950, 384, 288)
uint8 uint8
代码:import numpy as np
import matplotlib.pyplot as plt
import cv2
# Load the hyperspectral image
rawimg_RT = np.fromfile('1-1_RT.raw', dtype=np.float32)
rawimg_RT = rawimg_RT.reshape(950, 384, 288)
# Select a suitable band with maximum contrast between peanuts and background
band_for_masking = 42 # You may need to adjust this based on your data
# Calculate the difference image between peanuts and background
difference_image = rawimg_RT[:,: , band_for_masking]
# Convert the difference image to an 8-bit image
normalized_difference = cv2.normalize(difference_image, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
# Apply Otsu's thresholding to create a binary mask
#_, binary_mask = cv2.threshold(normalized_difference, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
_,binary_mask=cv2.threshold(normalized_difference,0.6,1,cv2.THRESH_BINARY)#阈值
print(binary_mask.shape,rawimg_RT.shape)
print(binary_mask.dtype,rawimg_RT.dtype)
cv2.imshow('Background Removed Image', binary_mask)
cv2.waitKey(0)
cv2.destroyAllWindows()
num_channels = 288 # 例如,创建一个3通道的掩膜
binary_mask = np.stack((binary_mask,) * num_channels, axis=2)
rawimg_RT = rawimg_RT.astype(np.uint8)
print(binary_mask.shape,rawimg_RT.shape)
print(binary_mask.dtype,rawimg_RT.dtype)
#binary_mask = np.array(binary_mask, dtype=np.uint8)
# 对高光谱图像应用二值化掩膜
background_removed_image = cv2.bitwise_and(rawimg_RT, rawimg_RT, mask=binary_mask)
# 显示去除背景后的图像
cv2.imshow('Background Removed Image', background_removed_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
根据您提供的代码和错误信息,我注意到问题可能出现在以下几个地方:
1. 数据类型不匹配:根据错误信息, _mask.sameSize(*psrc1)的断言失败,这可能是由于二值化掩膜( binary_mask)的数据类型与原始图像( rawimg_RT)的数据类型不匹配导致的。请确保它们具有相同的数据类型。
在代码中,您可以尝试将以下行:
rawimg_RT = rawimg_RT.astype(np.uint8)
2. 通道数不匹配:在创建二值化掩膜后,您将其复制为与原始图像具有相同通道数的形状。但是,根据输出结果, binary_mask的形状为 (950, 384, 1),而 rawimg_RT 的形状为 (950, 384, 288)。由于通道数不匹配,无法使用 cv2.bitwise_and 函数将它们应用于原始图像上。
您可以尝试重新构建具有相同通道数的二值化掩膜,例如:
num_channels = rawimg_RT.shape[2] binary_mask = np.repeat(binary_mask, num_channels, axis=2)
请参考修改后的代码如下所示: import numpy as npimport matplotlib.pyplot as pltimport cv2# Load the hyperspectral imagerawimg_RT = np.fromfile('1-1_RT.raw', dtype=np.float32)rawimg_RT = rawimg_RT.reshape(950, 384, 288)# Select a suitable band with maximum contrast between peanuts and backgroundband_for_masking = 42 # You may need to adjust this based on your data# Calculate the difference image between peanuts and backgrounddifference_image = rawimg_RT[:,:,band_for_masking]# Convert the difference image to an 8-bit imagenormalized_difference = cv2.normalize(difference_image, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)# Apply Otsu's thresholding to create a binary mask_, binary_mask = cv2.threshold(normalized_difference, 0.6, 1, cv2.THRESH_BINARY)cv2.imshow('Binary Mask', binary_mask)cv2.waitKey(0)cv2.destroyAllWindows() # Match the number of channels in the binary mask to the original imagenum_channels = rawimg_RT.shape[2]binary_mask = np.repeat(binary_mask, num_channels, axis=2)# Convert data types to matchrawimg_RT = rawimg_RT.astype(np.uint8)binary_mask = binary_mask.astype(np.uint8)# Apply binary mask to the hyperspectral imagebackground_removed_image = cv2.bitwise_and(rawimg_RT, rawimg_RT, mask=binary_mask)cv2.imshow('Background Removed Image', background_removed_image)cv2.waitKey(0)cv2.destroyAllWindows()
请尝试使用修改后的代码,并确保 1-1_RT.raw 文件存在于脚本所在的目录中。如果问题仍然存在,请提供更多关于错误的详细信息,以便我们进一步排查。
球一个最佳答案谢谢啦!这对我非常重要!
|
|