在Mac上的python中,openCV中的函数calcHist崩溃 [英] Function calcHist in openCV crashing in python on mac

查看:241
本文介绍了在Mac上的python中,openCV中的函数calcHist崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代码

# 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

我已经用以下命令安装了openCV

I have installed openCV with

brew install opencv

我确实再次重新安装了openCV,但是仍然存在相同的问题.

I did reinstall openCV again but same problem is there.

我怀疑openCV的编译存在问题.
也许是因为不同的编译器clang,gcc.

I would suspect that there was some problem with compiling of openCV.
Maybe because of different compilers clang, gcc.

任何建议都值得赞赏.

Any suggesting are appreciated.

推荐答案

在openCV中使用另一个函数时,我遇到了相同的错误.使用您帖子中的代码,我也收到了错误消息. openCV 3.0.0也不起作用,它给了我另一种错误.

I got the same error when using another function in openCV. I also got the error using the code in your post. openCV 3.0.0 did not work either, it gave me another type of error.

最终可行的方法是通过Anaconda安装openCV.它仍然使用openCV 2.4.8,所以也许就是解决方案.

What finally worked was installing openCV through Anaconda. It still uses openCV 2.4.8 so maybe that was the solution.

首先,我在brew中取消了openCV的链接:"brew unlink opencv"

First I unlinked openCV in brew: "brew unlink opencv"

然后安装了Anaconda软件包: http://continuum.io/downloads#all

Then installed the Anaconda package: http://continuum.io/downloads#all

随后安装openCV:"conda安装opencv"

Afterwards install openCV: "conda install opencv"

最后,更新numpy:"conda更新numpy"

Finally, update numpy: "conda update numpy"

这篇关于在Mac上的python中,openCV中的函数calcHist崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆