#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
int main(int argc, char** argv){
if( argc < 2){
std::cout<<"there is no input image file"<<std::endl;
return 0;
}
//通过命令行终端传入main函数参数
std::string imageDir = argv[1];
cv::Mat image = imread(imageDir);
cv::Mat imageCrop = cv::Mat::zeros(image.rows-20, image.cols, CV_8UC3);
for(int v = 0; v < imageCrop.rows; v++){
for(int u = 0; u < imageCrop.cols; u++){
imageCrop.ptr<cv::Vec3b>(v)[u] = image.ptr<cv::Vec3b>(v)[u];
}
}
cv::imshow("image", imageCrop);
cv::waitKey(0);
cv::imwrite("wall contour.jpg", imageCrop);
}
上面代码是opencv对图像进行裁切的函数,代码非常简单。这段程序需要在命令终端传人参数。但是自己运行时报错:
terminate called after throwing an instance of 'cv::Exception'
what(): OpenCV(3.4.3) /home/roma/Downloads/opencv/modules/core/src/matrix.cpp:235:
error: (-215:Assertion failed) s >= 0 in function 'setSize'
Aborted (core dumped)
然后用GDB进行debug,launch.json文件配置如下:
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "/home/roma/vscode/C/build/imageCorp",
//args 是对应需要传入的参数
"args": ["/home/roma/Pictures/wall contour2.jpg"],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}
自己debug一点问题没有,没有任何报错,代码也想不出哪里有问题。后面突然想到可能是传入参数的问题,传入的参数是图片路径:
/home/roma/Pictures/wall contour2.jpg
在lauch,json文件里,因为加了“ ”,所以当成一个字符串处理。但是在终端输入没有加字符串,所以遇到空格的地方就断掉了,实际程序输入的参数为:
/home/roma/Pictures/wall
解决方法是要么将wall contour2.pg改成wall_contour2.jpg或者在终端输入的时候加入双引号