【发布时间】:2014-11-07 17:35:17
【问题描述】:
这是代码
# USAGE
# python grayscale_histogram.py --image ../images/beach.png
# Import the necessary packages
from matplotlib import pyplot as plt
import argparse
import cv2
# Construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True,
help = "Path to the image")
args = vars(ap.parse_args())
# Load the image, convert it to grayscale, and show it
image = cv2.imread(args["image"])
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow("Original", image)
# Construct a grayscale histogram
hist = cv2.calcHist([image], [0], None, [256], [0, 256])
# Plot the histogram
plt.figure()
plt.title("Grayscale Histogram")
plt.xlabel("Bins")
plt.ylabel("# of Pixels")
plt.plot(hist)
plt.xlim([0, 256])
plt.show()
cv2.waitKey(0)
当我运行它时,出现以下错误:
Snows-MacBook-Pro:code Mac$ python chapter-07/grayscale_histogram.py -i images/wave.png
OpenCV Error: Assertion failed (step[dims-1] == (size_t)CV_ELEM_SIZE(flags)) in create, file /tmp/opencv-miY1tR/opencv-2.4.9/modules/core/src/matrix.cpp, line 236
Traceback (most recent call last):
File "chapter-07/grayscale_histogram.py", line 21, in <module>
hist = cv2.calcHist([image], [0], None, [256], [0, 256])
cv2.error: /tmp/opencv-miY1tR/opencv-2.4.9/modules/core/src/matrix.cpp:236: error: (-215) step[dims-1] == (size_t)CV_ELEM_SIZE(flags) in function create
我已经安装了openCV
brew install opencv
我确实再次重新安装了 openCV,但仍然存在同样的问题。
我怀疑openCV的编译存在一些问题。
也许是因为不同的编译器clang,gcc。
任何建议表示赞赏。
【问题讨论】: