001: Opencv 4.0代码执行

1. int main(int argc,char* argv[])详解

     argc记录了用户在运行程序的命令行中输入的参数的个数。  

     arg[]指向的数组中至少有一个字符指针,即arg[0].他通常指向程序中的可执行文件的文件名。在有些版本的编译器中还包括程序

文件所在的路径。
    
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

// OpenCV includes
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
using namespace cv;

int main( int argc, const char** argv )
{
   
//
argv[1]第一个参数是图像名
    Mat color = imread(argv[1]);
    Mat gray = imread(argv[1], IMREAD_GRAYSCALE); ///以灰度图的形式读取图像
///判断图像是否为空
if(! color.data ) // Check for invalid input { cout << "Could not open or find the image" << std::endl ; return -1; } // Write images 将读取的灰度图像写入本地 imwrite("lenaGray.jpg", gray); // Get same pixel with opencv function int myRow=color.cols-1; int myCol=color.rows-1; auto pixel= color.at<Vec3b>(myRow, myCol); cout << "Pixel value (B,G,R): (" << (int)pixel[0] << "," << (int)pixel[1] << "," << (int)pixel[2] << ")" << endl; // show images // imshow("Lena BGR", color); imshow("Lena Gray", gray); // wait for any key press waitKey(0); return 0; }
2.如何在window 环境中执行该任务。
 (1)ctrl+r进入控制台窗口,输入cd  /d  + exe所在的路径,进入exe所在文件夹
   (2) 根据实际参数个数 输入 ***.exe+参数1+参数2  然后回车
  

 3.但是无法在vs2017直接查看debug,因此还是要vs2017中设置

  点击项目——右键属性——调试——命令行参数(lena.jpg)

  设置对应的项目路径为 .\Debug ,这里是相对路径注意

  调试程序,可以进入断点啦

  

文件所在的路径。
    
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

// OpenCV includes
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
using namespace cv;

int main( int argc, const char** argv )
{
   
//
argv[1]第一个参数是图像名
    Mat color = imread(argv[1]);
    Mat gray = imread(argv[1], IMREAD_GRAYSCALE); ///以灰度图的形式读取图像
///判断图像是否为空
if(! color.data ) // Check for invalid input { cout << "Could not open or find the image" << std::endl ; return -1; } // Write images 将读取的灰度图像写入本地 imwrite("lenaGray.jpg", gray); // Get same pixel with opencv function int myRow=color.cols-1; int myCol=color.rows-1; auto pixel= color.at<Vec3b>(myRow, myCol); cout << "Pixel value (B,G,R): (" << (int)pixel[0] << "," << (int)pixel[1] << "," << (int)pixel[2] << ")" << endl; // show images // imshow("Lena BGR", color); imshow("Lena Gray", gray); // wait for any key press waitKey(0); return 0; }
2.如何在window 环境中执行该任务。
 (1)ctrl+r进入控制台窗口,输入cd  /d  + exe所在的路径,进入exe所在文件夹
   (2) 根据实际参数个数 输入 ***.exe+参数1+参数2  然后回车
  

 3.但是无法在vs2017直接查看debug,因此还是要vs2017中设置

  点击项目——右键属性——调试——命令行参数(lena.jpg)

  设置对应的项目路径为 .\Debug ,这里是相对路径注意

  调试程序,可以进入断点啦

  

猜你喜欢

转载自www.cnblogs.com/codeAndlearn/p/11519402.html
001