【OpenCV】Common problems and solutions

0 Preface

  This blog mainly summarizes some problems encountered in the use of OpenCV and the corresponding solutions. The focus here is on OpenCV, which is based on both C++ and Python. It is more comprehensive and will follow my study. The experience is constantly updated.

OpenCV environment configuration related:

1 Chinese garbled problem

  OpenCV's support for Chinese is very bad. For example, the name of the window displaying pictures cannot be in Chinese, otherwise garbled characters will appear; similarly, the path of the read and saved pictures cannot contain Chinese, otherwise an error will be reported.
  However, it seems that OpenCV based on VS configuration can solve this problem. It may be because of the different compilers used. VSCode configuration OpenCV generally uses the MinGW compiler, while VS configuration uses Microsoft's MSVC compiler. .
  Garbled characters appear because the default encoding method of VS is GBK/GB2312, and the encoding method required by OpenCV is UTF-8, and pay attention to the "with BOM" version, otherwise the OpenCV code will report an error "c2001 constant contains a newline character " .
  But it is a bit cumbersome to manually change the encoding method of the file every time. It is recommended to install a plug-in here:
insert image description here

insert image description here

  For OpenCV in python, at first I thought the encoding method would be more compatible, but unfortunately, in python-opencv, if the window name is set to Chinese, it will still be garbled.

2 Illegal path problem

  It is a classic problem that reading and saving pictures cannot have Chinese, but what I didn't expect is that OpenCV doesn't seem to handle illegal paths well enough.
  I accidentally encountered a problem before, that is, using opencv to save pictures in python is always not executed, and no error is reported . However, it can be saved with a general file name. Later, after testing, it was found that the file path should be a problem. It is not Chinese, but a colon. I found out by searching for information that the file path in Windows cannot contain colons, because colons have other meanings. For the record, this kind of problem is indeed a bit rare.

reference link

Guess you like

Origin blog.csdn.net/ZHOU_YONG915/article/details/131520258