最近为了建立图片数据库,要进行抠图,在使用了Windows自带的截图软件之后,发现非常的麻烦,每次都要直接打开图片,抠图,然后还要输入保存的文件名字,这种纯手工的方式,要多长时间才能搞定十万张图片。
本着能偷懒就偷懒的想法,在实际的数据库建立过程中,打开一个目录下面的文件和将抠出来的图保存下来的过程,根本不需要人的参与,因为这两部分都可以由程序自己来完成,因此,就产生了基于OpenCV的抠图程序。
要解决在每个文件夹下面的图片都进行处理,需要解决下面三个问题:
读取一个文件夹下面的所有文件;
显示图片,并获得鼠标在图片上面选择的矩形框;
自动生成文件的名字并保存选择的区域。
对于第一个问题,已经在另外一个博客中描述了。
对于第二个问题,通过查看OpenCV的帮助文档可以知道,可以通过setMouseCallback函数在图片显示窗口中添加鼠标事件的回调函数,从而获得鼠标的坐标,同时也能获得鼠标的事件,例如鼠标右键点击、左键点击和鼠标移动等事件。通过将鼠标左键点击时的坐标作为开始点的坐标,将鼠标左键释放时的坐标作为结束点的坐标,并通过一定的处理,将
对于第三个问题,文件的名字可以使用数字的表示,而保持选定的区域,可以通过将选定的区域设置为图片的ROI,从而使用OpenCV的imwrite函数将ROI区域保存为图片的格式。
具体的实现代码如下:
functions.h
/*M///
// Copyright (c) 2014, sheng
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//M*/
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include <QStringList>
#include <string>
#include "opencv2/opencv.hpp"
QStringList GetFilesInDirectory(const QString &Directory);
bool SaveImageInJPEG(const std::string& FileName, const cv::Mat& Image);
bool Screenshot(cv::Mat Image, const std::string& FileName,
const int& StartedX, const int& StartedY,
const int& EndedX, const int& EndedY);
void OnMouse( int Event, int X, int Y, int flag, void* Userdata);
std::string IntToString(int value);
bool DrawRectangle(cv::Mat& Image, const int& StartedX, const int& StartedY,
const int& EndedX, const int& EndedY,
const cv::Scalar& Color = cv::Scalar(255,0,0),
int Thickness = 2, int LintType = 8);
bool MakingRectanglePoint(int& StartedX, int& StartedY,
int& EndedX, int& EndedY);
#endif // FUNCTIONS_H
userdata.h
/*M///
// Copyright (c) 2014, sheng
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//M*/
#ifndef USERDATA_H
#define USERDATA_H
#include <string>
#include "opencv2/opencv.hpp"
/**
* @brief The UserData struct Use to contain the window name and the image.
*
* @author sheng
* @date 2014-08-27
* @version 0.1
*
* @history
* <author> <date> <version> <description>
*