在opencv中显示和读取图像时出错

时间:2017-07-13 10:32:11

标签: python-2.7 image-processing opencv3.0

代码:

Route {#109 ▼
  +uri: "oauth/authorize"
  +methods: array:2 [▶]
  +action: array:6 [▼
    "middleware" => array:2 [▼
      0 => "web"
      1 => "auth"
    ]
    "uses" => "\Laravel\Passport\Http\Controllers\AuthorizationController@authorize"
    "controller" => "\Laravel\Passport\Http\Controllers\AuthorizationController@authorize"
    "namespace" => "\Laravel\Passport\Http\Controllers"
    "prefix" => "oauth"
    "where" => []
  ]
  +controller: null
  +defaults: []
  +wheres: []
  +parameters: null
  +parameterNames: null
  +computedMiddleware: null
  +compiled: CompiledRoute {#203 ▶}
  #router: Router {#21 ▶}
  #container: Application {#3 ▶}
}

它抛出一个错误:

import numpy as np
import cv2

img = cv2.imread('/home/pi/Downloads/download.jpg',0)
cv2.imshow(img)

cv2.waitkey(0)
cv2.destroyAllWindows()

尝试使用opencv和python打开图像。但它显示出时髦的错误。此外,我对编程和opencv都很陌生。

由于

2 个答案:

答案 0 :(得分:1)

OpenCV的cv2.imshow()需要两个参数:

  1. 要显示的窗口的名称;
  2. 图像本身。
  3. 那么,你的错误TypeError: Required argument 'mat' (pos 2) not found所说的是缺少第二个参数,因为该函数正在将img变量解释为窗口名称。

    除此之外,cv2.waitkey(0)也会产生错误,正确的函数名称为cv2.waitKey(0)(使用大写字母K)。

    所以正确的代码是:

    import numpy as np
    import cv2
    
    img = cv2.imread('/home/pi/Downloads/download.jpg',0)
    cv2.imshow('My window',img)
    
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    

答案 1 :(得分:0)

您需要在cv2.imshow()的第一个参数中传递窗口名称,例如cv2.imshow('image',img)