【问题标题】:Why does it say "Name 'gray' can be not defined"?为什么它说“无法定义名称'灰色'”?
【发布时间】:2023-03-16 12:20:01
【问题描述】:

这个程序是由我之前的合作学生在我目前工作的公司编写的。该项目是通过向计算机网络摄像头显示不同的图像来制作智能汽车套件 [Kuman SM11] 驱动器。换句话说,就是汽车没有传感器的自动驾驶。

他编写的文件之一是名为“arucoMarkerDistanceDetection”的文件。文件比较长,如果我发的代码块不够,请告诉我!

下面是发生错误的函数定义之一。在最后一行,您可以在其上方看到gray.shape[::-1]gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)。为什么Pycharm上面只定义了几行就告诉我可能没有定义?

我一开始就缺乏编码经验,所以这是一个挑战。另外,尝试剖析别人的代码是另一项任务。除此之外,我正在使用 Pycharm,但我仍然不是 100% 熟悉。

for fname in images:
    img = cv2.imread(fname)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # find the chess board (calibration pattern) corners
    ret, corners = cv2.findChessboardCorners(gray, (7, 6), None)

    # if calibration pattern is found, add object points,
    # image points (after refining them)
    if ret == True:
        objpoints.append(objp)

        # Refine the corners of the detected corners
        corners2 = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria)
        imgpoints.append(corners2)

        # Draw and display the corners
        img = cv2.drawChessboardCorners(img, (7, 6), corners2, ret)

ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)

    line 100, in <module>
        ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)
    cv2.error: OpenCV(4.1.1) C:\projects\opencv-python\opencv\modules\calib3d\src\calibration.cpp:3677: error: (-215:Assertion failed) nimages > 0 in function 'cv::calibrateCameraRO'

【问题讨论】:

  • 如果图像为空,则未定义。你应该在循环之前给它一个默认值。
  • 请注意,PyCharm 不知道会进入循环。但是,如果您确实知道images 永远不会为空,那么忽略该警告是安全的。

标签: python python-3.x opencv pycharm aruco


【解决方案1】:

如果“images”为空,则 for 循环将不会运行,因此变量 gray 可能不会被初始化,并且在循环之后,您正在访问可能不存在的 gray。因此代码可能会中断。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多