OpenCV argc 和 argv 混淆 [英] OpenCV argc and argv confusion

查看:28
本文介绍了OpenCV argc 和 argv 混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看一些 OpenCV 教程,并在开头找到了这一行(这是链接,代码位于 CalcHist 部分下 http://opencv.willowgarage.com/documentation/c/histograms.html)

I'm checking some OpenCV tutorial and found this line at the beginning (here is the link, code is under the CalcHist section http://opencv.willowgarage.com/documentation/c/histograms.html)

if (argc == 2 && (src = cvLoadImage(argv[1], 1)) != 0)

我以前从未见过这个,真的不明白.我查了一些关于这个主题的问答,但仍然不明白.有人可以向我解释一下这行是什么意思吗?

I've never seen this before and really don't understand it. I checked some Q&A regarding this subject but still don't understand it. Could someone explain to me what is the meaning of this line?

谢谢!

推荐答案

该行按顺序执行以下操作:

The line does the following, in order:

  1. 测试是否 argc == 2 - 也就是说,是否正好有 1 个命令行参数(第一个参数"是可执行文件名称)
  2. 如果是(因为如果 argc 不是 2,短路 && 将中止测试而不评估右侧),设置src 到在该命令行参数上调用的 cvLoadImage 的结果
  3. 测试结果(以及因此src)是否不为零
  1. Tests if argc == 2 - that is, if there was exactly 1 command line argument (the first "argument" is the executable name)
  2. If so (because if argc is not 2, the short-circuiting && will abort the test without evaluating the right-hand-side), sets src to the result of cvLoadImage called on that command-line argument
  3. Tests whether that result (and hence src) is not zero

argcargv 是 C 中 main 函数采用的两个参数的名称(几乎总是). argc 是一个整数,等于调用可执行文件时出现的命令行参数的数量.argv 是一个 char* 数组(代表以 NULL 结尾的字符串数组),包含这些命令行参数的实际值.从逻辑上讲,它包含 argc 条目.

argc and argv are the names (almost always) given to the two arguments taken by the main function in C. argc is an integer, and is equal to the number of command-line arguments present when the executable was called. argv is an array of char* (representing an array of NULL-terminated strings), containing the actual values of those command-line arguments. Logically, it contains argc entries.

请注意,argcargv 始终将可执行文件的名称作为第一个条目,因此以下命令调用:

Note that argc and argv always have the executable's name as the first entry, so the following command invocation:

$> my_program -i input.txt -o output.log

...将 5 放入 argc,而 argv 将包含五个字符串 my_program, -i>、input.txt-ooutput.log.

...will put 5 in argc, and argv will contain the five strings my_program, -i, input.txt, -o, output.log.

所以你引用的 if-test 首先检查是否有 1 个命令行参数,除了可执行文件名称(argc == 2).然后继续使用该参数 (cvLoadImage(argv[1], 1))

So your quoted if-test is checking first whether there was exactly 1 command-line argument, apart from the executable name (argc == 2). It then goes on to use that argument (cvLoadImage(argv[1], 1))

检查argc然后使用argv[n]是一个常见的习惯用法,因为它是不安全 访问超出 argv 数组的末尾.

Checking argc and then using argv[n] is a common idiom, because it is unsafe to access beyond the end of the argv array.

这篇关于OpenCV argc 和 argv 混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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