不显示python OpenCV错误 [英] Do not display python OpenCV error

查看:130
本文介绍了不显示python OpenCV错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有Python的OpenCV,更具体地说是cv2findTransformECC功能来执行图像注册.如果算法不收敛,此函数可能会引发错误.我使用简单的try ... except指令捕获了错误,并且处理了该错误.但是,OpenCV错误消息仍显示在我的终端中,我想将其隐藏.我该怎么办?

I am using OpenCV with python, and more specifically the findTransformECC fonction of cv2 to perform image registration. This function can throw errors if the algorithm does not converge. I catch the error with a simple try ... except instruction, and I handle the error. However the OpenCV error message is still displayed in my terminal, and I would like to hide it. How should I do?

这是一个简单的例子

try:
    cc, warp_matrix = cv2.findTransformECC(img1, img2, warp_matrix)
except cv2.error:
    cc = 15;
    print("An error occured but it does not matter")

如果findTrnaformECC函数抛出错误,我的程序会正确输出我的自定义错误消息(An error occured but it does not matter),但也会输出OpenCV错误(OpenCV Error: Iterations do not converge (The algorithm stopped before its convergence. The correlation is going to be minimized. Images may be uncorrelated or non-overlapped) in findTransformECC, file /home/travis/miniconda/conda-bld/conda_1485299288502/work/opencv-3.2.0/modules/video/src/ecc.cpp, line 530),我想防止这种情况.

If the findTrnaformECC function throws am error my program correctly outputs my custom error message (An error occured but it does not matter) but ALSO the OpenCV error (OpenCV Error: Iterations do not converge (The algorithm stopped before its convergence. The correlation is going to be minimized. Images may be uncorrelated or non-overlapped) in findTransformECC, file /home/travis/miniconda/conda-bld/conda_1485299288502/work/opencv-3.2.0/modules/video/src/ecc.cpp, line 530) and I would like to prevent that.

推荐答案

重定向stderr似乎不起作用,但是它给了我一个从命令行重定向它的想法,该命令运行得很好(在基于Unix的系统上)

Redirecting stderr did not seem to work but it gave me the idea to redirect it from the command-line which works pretty well (on Unix based systems)

python myscript.py 2> /dev/null

但是,这将隐藏应在stderr上显示的所有其他错误.对于我的精确应用而言,这不是问题,但是可以.

However this will hide all other errors that should be display on stderr. This is not a matter for my precise application but it could be.

这篇关于不显示python OpenCV错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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