c语言opencv删除图片

在进行图像处理的时候,经常要进行图像的剔除 。下面我们学习一下opencv删除图像的函数 。
【c语言opencv删除图片】//int result = remove(img_path[i].c_str()); //绝对或者相对路径都可以int result = remove("1.jpg"); //绝对或者相对路径都可以if (result == 0)cout << "delete succeeded!删除图片成功" << endl;elsecout << "delete failed! 删除图片失败" << endl;具体应用示例为了方便大家理解如何使用,下面我举一个例子,在相机标定的时候进行图像不符合规范的自动删除
#include "opencv2/core/core.hpp"#include "opencv2/imgproc/imgproc.hpp"#include "opencv2/calib3d/calib3d.hpp"#include "opencv2/highgui/highgui.hpp"#include <IOStream>#include <fstream>#include <iostream>#include <stdlib.h> //srand()和rand()函数 #include<windows.h>using namespace cv;using namespace std;#definedebug_show_picture1 //是否显示部分调试图片,方便调试#definedebug_save_shipin0 //是否保存结果视频/****************打印相关组件 start !********************************************************************//*打印等级,修改后面的宏定义可以改变函数输出打印等级*/#define ALG_PRTINT_LEVER PRINT_LEVEL_UNLIMIT#define ALG_PRTINT(...)SAL_printf(__VA_ARGS__)#define ALG_PRT(...)ALG_PRTINT(__FUNCTION__, __LINE__, PRINT_LEVEL_UNLIMIT, __VA_ARGS__)#define ALG_DBG(...)ALG_PRTINT(__FUNCTION__, __LINE__, PRINT_LEVEL_DBG,__VA_ARGS__)#define ALG_WAR(...)ALG_PRTINT(__FUNCTION__, __LINE__, PRINT_LEVEL_WRN,__VA_ARGS__)#define ALG_ERR(...)ALG_PRTINT(__FUNCTION__, __LINE__, PRINT_LEVEL_ERR,__VA_ARGS__)/************************************************************************************************ @enumHAT_SAL_PRT_LEVEL_E* @brief打印输出的等级***************************************************************************************************/typedef enum _PRT_LEVEL_E_{ PRINT_LEVEL_ERR = 0,/*错误打印信息*/ PRINT_LEVEL_WRN = 1,/*警告打印信息*/ PRINT_LEVEL_DBG = 2,/*调试打印信息*/ PRINT_LEVEL_UNLIMIT = 3,/*无限制打印信息*/ PRINT_LEVEL_NOPRT = 4,/*没有打印信息*/} PRT_LEVEL_E;/*******************************************************************************Function: SAL_printfDescription: 该函数能够通过设置的打印等级ALG_PRTINT_LEVER,来控制是否输出相关语句Input:Output:Return:0:Successfulohters:Failed*******************************************************************************/void SAL_printf(const char *pFun, UINT line, PRT_LEVEL_E levelParam, const char *fmt, ...){ static INT8 g_printfInfo[4][16] = { "ERR", "WAR", "DBG", "INF" }; va_list p; if (ALG_PRTINT_LEVER == PRINT_LEVEL_NOPRT || levelParam == PRINT_LEVEL_NOPRT) {return; } if (levelParam <= ALG_PRTINT_LEVER) {va_start(p, fmt);printf("[ALG][%s][%s][%4d] ", g_printfInfo[levelParam], pFun, line);vprintf(fmt, p);va_end(p); }}/******************************打印相关组件 end*************************************************************************/class Ve{public: vector<string> ReadImage(cv::String pattern);};vector<string> Ve::ReadImage(cv::String pattern){ vector<string> temp; vector<cv::String> fn; glob(pattern, fn, false); size_t count = fn.size(); //number of png files in images folder for (size_t i = 0; i < count; i++) {temp.push_back(fn[i]); } return temp;}void main(){ ofstream fout("caliberation_result.txt");/* 保存标定结果的文件 */ //读取每一幅图像,从中提取出角点,然后对角点进行亚像素精确化cout << "开始提取角点………………n"; int image_count = 0;/* 图像数量 */ Size image_size;/* 图像的尺寸 */ Size board_size = Size(8, 11);/* 标定板上每行、列的角点数 */ vector<Point2f> image_points_buf;/* 缓存每幅图像上检测到的角点 */ vector<vector<Point2f>> image_points_seq; /* 保存检测到的所有角点 */ int count = -1;//用于存储角点个数 。//while (getline(fin, filename)) //{ cv::String pattern = "./data/0223_2che/2che_huoche_video4/*.jpg"; Ve ve; vector<string> img_path = ve.ReadImage(pattern); for (int i = 0; i < img_path.size(); i++) {image_count++;// 用于观察检验输出//cout << "image_count = " << image_count << endl;/* 输出检验*///cout << "-->count = " << count << endl;Mat imageInput = imread(img_path[i]);if (imageInput.empty()){ALG_ERR("can not open pic,图片 %s 打开失败n", img_path[i].c_str());cout << "can not open pic!n";Sleep(100000);//睡眠一下,不至于窗口跳出exit(-1);}Mat imageInput_copy = imageInput.clone(); //校正后输出图片if (image_count == 1)//读入第一张图片时获取图像宽高信息{image_size.width = imageInput.cols;image_size.height = imageInput.rows;cout << "image_size.width = " << image_size.width << endl;cout << "image_size.height = " << image_size.height << endl;}//printf("输出结果:%sn", filename.c_str());/* 提取角点 */if (0 == findChessboardCorners(imageInput, board_size, image_points_buf)){cout << "can not find chessboard corners!n"; //找不到角点ALG_ERR("图片: %s 找不到角点,请检查或者更换该图片n", img_path[i].c_str());int result = remove(img_path[i].c_str());//绝对或者相对路径都可以if (result == 0)cout << "delete succeeded!删除图片成功" << endl;elsecout << "delete failed! 删除图片失败" << endl;continue;}else{ALG_PRT("正在提取图片%s的角点,image_count=%dn", img_path[i].c_str(), image_count);Mat view_gray;cvtColor(imageInput, view_gray, COLOR_RGB2GRAY);/* 亚像素精确化 */find4QuadCornerSubpix(view_gray, image_points_buf, Size(5, 5)); //对粗提取的角点进行精确化//cornerSubPix(view_gray,image_points_buf,Size(5,5),Size(-1,-1),TermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER,30,0.1));image_points_seq.push_back(image_points_buf);//保存亚像素角点/* 在图像上显示角点位置 */drawChessboardCorners(view_gray, board_size, image_points_buf, false); //用于在图片中标记角点namedWindow("Camera Calibration", 0);//创建窗口imshow("Camera Calibration", view_gray);//显示图片waitKey(5);//暂停0.5S//这种初始化点的方式也可以for (int i = 0; i < image_points_buf.size(); i++){Point p2;p2.x = image_points_buf[i].x;p2.y = image_points_buf[i].y;//画实心点circle(imageInput_copy, p2, 8, Scalar(0, 0, 255), -1); //第五个参数我设为-1,表明这是个实点 。}namedWindow("imageInput_copy", 0);//创建窗口imshow("imageInput_copy", imageInput_copy);//显示图片waitKey(5);//暂停0.5S} } ALG_PRT("所有图片检查完毕n"); waitKey(5000000000000);//暂停0.5S }


推荐阅读