使用cv2.imshow()时发生错误

时间:2017-11-10 14:40:00

标签: python opencv

任务是,我从相机拍摄照片,然后使用cnns处理照片,然后显示带有结果的照片。但是当我使用opencv python接口来显示图像(os是mac os)时出现错误:

 while(1):
    test_data = []
    ret, frame = cap.read()
    frame = cv2.imread('hello.jpg')
    h, w, _ = frame.shape
    img = copy.deepcopy(frame)
    img = pre_process(img)
    test_data.append([img])
    ret_res = _infer(inferer, test_data, threshold)
    draw_result(frame, ret_res, h, w)     

    cv2.imshow("hellocapture", frame)

发生以下错误:

PC: @                0x0 (unknown)
*** SIGFPE (@0x7fffc5186d01) received by PID 36436 (TID 0x7fffe38773c0) stack 
trace: ***
@     0x7fffdab7bb3a _sigtramp
@     0x7fffc5186d02 CFNumberCreate
@     0x7fffc6c027b5 -[NSPlaceholderNumber initWithDouble:]
@     0x7fffcae3b594 +[CALayer defaultValueForKey:]
@     0x7fffcaebd489 classDescription_locked()
@     0x7fffcaebc9fe classDescription_locked()
@     0x7fffcaebc9fe classDescription_locked()
@     0x7fffcaeb8292 classDescription()
@     0x7fffcaeb8495 CAObject_classInfo
@     0x7fffcae3bdf4 CA::Layer::class_state()
@     0x7fffcae3e081 -[CALayer init]
@     0x7fffc2d07854 -[NSView makeBackingLayer]
@     0x7fffc2d076db -[NSView(NSInternal) _createLayerAndInitialize]
@     0x7fffc2d06d4d -[NSView _doSetWantsLayerYES]
@     0x7fffc2d069da -[NSView setWantsLayer:]
@     0x7fffc2d142b6 __49-[NSThemeFrame _floatTitlebarAndToolbarFromInit:]_block_invoke
@     0x7fffc364b8b6 +[NSAnimationContext runAnimationGroup:]
@     0x7fffc2d13f23 -[NSThemeFrame _floatTitlebarAndToolbarFromInit:]
@     0x7fffc2d11a9c -[NSThemeFrame initWithFrame:styleMask:owner:]
@     0x7fffc2d10522 -[NSWindow _commonInitFrame:styleMask:backing:defer:]
@     0x7fffc2d0ec03 -[NSWindow _initContent:styleMask:backing:defer:contentView:]
@     0x7fffc2d0e65f -[NSWindow initWithContentRect:styleMask:backing:defer:]
@        0x1171ddebd -[QCocoaWindow initWithContentRect:styleMask:backing:defer:]
@        0x1171dddb1 -[NSWindow(QWidgetIntegration) qt_initWithQWidget:contentRect:styleMask:]
@        0x1171cee19 qt_mac_create_window()
@        0x1171ce119 QWidgetPrivate::createWindow_sys()
@        0x1171ce073 qt_mac_window_for()
@        0x1171d3b13 QWidgetPrivate::setModal_sys()
@        0x1172637c8 QWidget::create()
@        0x1175d5431 QToolBarPrivate::init()
@        0x1175d631c QToolBar::QToolBar()
@        0x111ca9c98 CvWindow::createToolBar()
Floating point exception: 8

并且我确定cv2.imshow有问题,但我不知道怎么做?

如果我只使用以下代码,那就没问题

#!/usr/bin/env python
# coding=utf-8
import cv2  
import numpy
import matplotlib.pyplot as plot

cap = cv2.VideoCapture(0)
while(1):
    # get a frame
    ret, frame = cap.read()
    # show a frame
    print frame.shape
    cv2.imshow("capture", frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
       break

1 个答案:

答案 0 :(得分:0)

在OpenCV中显示图像时,通常需要设置waitKey并释放:

while(1):
    test_data = []
    ret, frame = cap.read()
    frame = cv2.imread('hello.jpg')
    h, w, _ = frame.shape
    img = copy.deepcopy(frame)
    img = pre_process(img)
    test_data.append([img])
    ret_res = _infer(inferer, test_data, threshold)
    draw_result(frame, ret_res, h, w)     

    cv2.imshow("hellocapture", frame)
    if cv2.waitKey(1) & 0xFF == ord('q')
        break

cap.release()
cv2.destroyAllWindows()