This is code
# 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) When I run it, I have following error:
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 I have installed openCV with
brew install opencv I did reinstall openCV again but same problem is there.
I would suspect that there was some problem with compiling of openCV.
Maybe because of different compilers clang, gcc.
Any suggesting are appreciated.