cv2.error: OpenCV(3.4.9) C:\projects\opencv-python\opencv\modules\core\src\arithm.cpp:687: error: (-5:Bad argument) When the input arrays in add/subtract/multiply/divide functions have different types, the output array type must be explicitly specified in
时间: 2023-07-21 20:10:50 浏览: 70
这个错误通常是因为两个操作数的数据类型不同,导致无法进行操作。在这种情况下,您需要将数据类型匹配,或者在使用OpenCV的算术函数时明确指定输出数组的数据类型。
以下是一些可能的解决方法:
1. 将两个操作数的数据类型匹配:您可以使用`astype()`方法将其中一个数组的数据类型转换为另一个数组的数据类型。例如,如果第一个数组是uint8类型,而第二个数组是float类型,您可以使用以下代码将第二个数组转换为uint8类型:
```python
array2 = array2.astype(np.uint8)
```
2. 明确指定输出数组的数据类型:在使用OpenCV的算术函数时,您可以使用`dtype`参数明确指定输出数组的数据类型。例如,如果您想将两个数组相加,并将结果存储在一个uint8类型的数组中,您可以使用以下代码:
```python
result = cv2.add(array1, array2, dtype=cv2.CV_8U)
```
在这个示例中,我们使用了`cv2.CV_8U`常量来指定输出数组的数据类型为uint8。
希望这些解决方法能够帮助您解决问题。
相关问题
cv2.error: OpenCV(3.4.9) C:\projects\opencv-python\opencv\modules\imgproc\src\bilateral_filter.dispatch.cpp:424: error: (-210:Unsupported format or combination of formats) Bilateral filtering is only implemented for 8u and 32f images in function 'cv::bilateralFilter'
这个错误提示是在使用OpenCV的双边滤波函数bilateralFilter时出现的。根据错误提示,双边滤波只支持8位无符号和32位浮点图像格式。因此,你需要检查你的输入图像格式是否支持双边滤波,如果不支持,则需要进行格式转换。你可以使用函数cv::cvtColor将图像转换为支持的格式。例如,将图像转换为8位无符号格式可以使用以下代码:
```
cv::cvtColor(src, dst, cv::COLOR_BGR2GRAY);
```
其中,src是输入图像,dst是转换后的图像,cv::COLOR_BGR2GRAY是转换的类型。
OpenCV(3.4.9) /io/opencv/modules/imgcodecs/src/loadsave.cpp:740: error: (-215:Assertion failed) !_img.empty() in function 'imwrite'
这个错误发生在使用 OpenCV 的 imwrite 函数时,一般是因为要写入的图像为空,导致无法进行写入操作。您可以检查一下要写入的图像是否为空,或者是在读入图像的时候出现了问题,导致要写入的图像为空。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)