我使用OpenCV在python中尝试了此代码,但我不理解错误消息

时间:2019-09-10 17:50:09

标签: python opencv

我在带有OpenCV的python中使用了此代码,但我不理解该错误。有人可以解释错误消息的含义吗?

img = cv2.imread('img.png',0)

laplacian = cv2.Laplacian(img,cv2.CV_64F)

plt.subplot(2,2,2)
plt.imshow(laplacian,cmap='gray')
plt.title('laplacian')


laplacian = cv2.Laplacian(img,cv2.CV_64F)

error: OpenCV(3.4.1) C:\Miniconda3\conda-bld\opencv-suite_1533128839831\work\modules\core\src\matrix.cpp:760: error: (-215) dims <= 2 && step[0] > 0 in function cv::Mat::locateROI

1 个答案:

答案 0 :(得分:2)

您将从2个图开始子图,并且仅显示一张图像。所以应该是2,2,1而不是2,2,2。并且在显示图像后添加标题。应该是之前。

您必须使用plt.imshow()将图像添加到隐式图中的子图中,然后使用plt.show()实际显示该图。 (您还可以先创建一个特别命名的图形,然后为其创建子图。)


试试这个:

import cv2
from matplotlib import pyplot as plt

# read image and convert to grayscale
img = cv2.imread('img.png',0)

# compute laplacian
laplacian = cv2.Laplacian(img,cv2.CV_64F)


# show both original and laplacian using pyplot
plt.subplot(2,2,1)
plt.imshow(img,cmap = 'gray')
plt.title('Original')
plt.subplot(2,2,2)
plt.imshow(laplacian,cmap = 'gray')
plt.title('Laplacian')
plt.show()

# show just laplacian using pyplot
plt.subplot(2,2,1)
plt.imshow(laplacian,cmap = 'gray')
plt.title('Laplacian')
plt.show()

# or show just laplacian with no subplot specification or title
plt.imshow(laplacian,cmap = 'gray')
plt.show()

# show laplacian using OpenCV
cv2.imshow("Laplacian", laplacian)
cv2.waitKey(0)
cv2.destroyAllWindows()


请查看:

pyplot subplot

simple examples to display images