基于OpenCV的抠图程序

时间:2022-05-15 23:21:19
    最近为了建立图片数据库,要进行抠图,在使用了Windows自带的截图软件之后,发现非常的麻烦,每次都要直接打开图片,抠图,然后还要输入保存的文件名字,这种纯手工的方式,要多长时间才能搞定十万张图片。
    本着能偷懒就偷懒的想法,在实际的数据库建立过程中,打开一个目录下面的文件和将抠出来的图保存下来的过程,根本不需要人的参与,因为这两部分都可以由程序自己来完成,因此,就产生了基于OpenCV的抠图程序。




    要解决在每个文件夹下面的图片都进行处理,需要解决下面三个问题:
  1. 基于OpenCV的抠图程序读取一个文件夹下面的所有文件;
  2. 基于OpenCV的抠图程序显示图片,并获得鼠标在图片上面选择的矩形框;
  3. 基于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>
* sheng 2014-08-27 0.1 build the struct
*/
struct UserData
{
std::string WindowName;
cv::Mat* OriginImage;
};

#endif // USERDATA_H

   IntToString.h
#ifndef INTTOSTRING_H
#define INTTOSTRING_H

#include <iostream>
std::string IntToString(int value);

#endif // INTTOSTRING_H

   IntToString.cpp
#include <iostream>
#include <sstream>
#include "IntToString.h"

/**
* @brief IntToString translating the int value to the string.
* @param value
* @return the string of the input value
*
* @author sheng
* @version 0.1 [build the function] [2013/12/18] [sheng]
*/


std::string IntToString(int value)
{
std::ostringstream convert;
convert << value;
return convert.str();
}

     GetFileInDirectory.cpp
#include <QDir>
#include <iostream>
/**
* @brief GetFilesInDirectory This function get the
* @param Directory
* @return A list of the names of all the files, or An empty list if the
* directory is unreadable, does not exist,or dose not contain
* any files.
*
* @author sheng
* @version 1.0.0
* @date 2014-04-03
*
* @history <author> <date> <description>
* sheng 2014-04-03 build the function
*
*/
QStringList GetFilesInDirectory(const QString &Directory)
{
// set the directory
QDir Dir(Directory);

// set the filter and sorting rules.
Dir.setFilter(QDir::Files | QDir::NoSymLinks);
Dir.setSorting(QDir::Size | QDir::Name);

// return the FileList in the directory
return Dir.entryList();
}

    DrawRectangle.cpp
/*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*/


#include "opencv2/opencv.hpp"

/**
* @brief DrawRectangle Draw a rectangle in the image.
* @param Image The image
* @param StartedX The x-coordinate of the started point
* @param StartedY The y-coordinate of the started point
* @param EndedX The x-coordinate of the ended point
* @param EndedY The y-coordinate of the ended point
* @param Color The color of the rectangle
* @param Thickness The thickness of the lines of the rectangle
* @param LintType The tpye of the line
* @return true if the operation is successed
* false otherwise
*
* @author sheng
* @date 2014-08-27
* @version 0.1
*
* @history
* <author> <date> <version> <description>
* sheng 2014-08-27 0.1 build the module
*/
bool DrawRectangle(cv::Mat& Image, const int& StartedX, const int& StartedY,
const int& EndedX, const int& EndedY,
const cv::Scalar& Color, int Thickness, int LineType)
{

if (Image.empty())
{
std::cout << "The image is empty. " << std::endl;
return false;
}


// set the started point and the ended point
cv::Point StartedPoint(StartedX, StartedY);
cv::Point EndedPoint(EndedX, EndedY);


// draw the rectangle
cv::rectangle(Image, StartedPoint, EndedPoint, Color, Thickness, LineType);

return true;
}

    MakingRectanglePoint.cpp
/*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*/




/**
* @brief MakingRectanglePoint
* @param StartedX
* @param StartedY
* @param EndedX
* @param EndedY
* @return
*
* @author sheng
* @date
* @version 0.1
*
* @history
* <author> <date> <version> <description>
* sheng 0.1
*
*/
bool MakingRectanglePoint(int& StartedX, int& StartedY,
int& EndedX, int& EndedY)
{
// exchage the x of the position
if (StartedX > EndedX)
{
int tmp = EndedX;
EndedX = StartedX;
StartedX = tmp;
}

// exchage the y of the position
if (StartedY > EndedY)
{
int tmp = EndedY;
EndedY = StartedY;
StartedY = tmp;
}


return true;

}

    OnMouseCalled.cpp
/*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*/


#include "functions.h"
#include "opencv2/opencv.hpp"
#include <iostream>
#include <string>
#include "userdata.h"

const std::string PrefixOfFileName = "E:/Database_For_Experiment/car";

/**
* @brief OnMouse The call back function used to the mouse moving
* @param Event The mouse event
* @param X The position x of the mouse
* @param Y The position y of the mouse
* @param flag
* @param Userdata
*
*
* @author sheng
* @date 2014-08-26
* @version 0.1
*
*
* @history
* <author> <date> <version> <description>
* sheng 2014-08-26 0.1 build the module
*
*
*/
void OnMouse( int Event, int X, int Y, int flag, void* Userdata)
{
static int StartedX = -1;
static int StartedY = -1;
static int Index = 0;


// get the Image and the windows name
UserData* data = static_cast<UserData*>(Userdata);
cv::Mat* Image = data->OriginImage;
std::string WindowName = data->WindowName;


switch (Event)
{
case CV_EVENT_LBUTTONDOWN:
if (StartedX == -1)
{
StartedX = X;
StartedY = Y;
std::cout << "Left click" << std::endl;
}
break;

case CV_EVENT_LBUTTONUP:
if (StartedX != -1)
{
std::string FileName = PrefixOfFileName + "/" +
IntToString(Index) +".jpg";

// Making the position to be valid
MakingRectanglePoint(StartedX, StartedY, X, Y);

// Get the image
if(!Screenshot(*Image, FileName, StartedX, StartedY, X, Y))
{
std::cout << "Screenshot failed." << std::endl;
}


StartedX = -1;
StartedY = -1;
Index++;

// show the orgin image
cv::imshow(WindowName, *Image);
}
break;

case CV_EVENT_MOUSEMOVE:
if (StartedX != -1)
{
cv::Mat ImageCopy;
Image->copyTo(ImageCopy);

// draw rectangle
DrawRectangle(ImageCopy, StartedX, StartedY, X, Y);

// show the image
cv::imshow(WindowName, ImageCopy);

}
break;


default:
break;
}
}

    SaveImageInJPEG.cpp
/*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*/

#include <string>
#include <iostream>
#include "opencv2/opencv.hpp"



/**
* @brief SaveImageInJPEG Save the image in jpeg format.
* @param FileName The name used to save the image.
* @param Image The image which is to be saved.
* @return true when the saving operator is successed.
* false otherwise
*
* @author sheng
* @date 2014-08-26
* @version 0.1
*
* @history
* <author> <date> <version> <description>
* sheng 2014-08-26 0.1 build the module
*
*/
bool SaveImageInJPEG(const std::string& FileName, const cv::Mat& Image)
{

if (FileName.empty())
{
std::cout << "The name of the file is empty." << std::endl;
return false;
}


if (Image.empty())
{
std::cout << "The data of the image is empty." << std::endl;
return false;
}


// set the jpeg params
std::vector<int> Params;
Params.push_back(CV_IMWRITE_JPEG_QUALITY);
Params.push_back(100);

// save the image file
bool Result = cv::imwrite(FileName, Image, Params);

return Result;

}

    Screenshot.cpp
/*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*/


#include <string>
#include <iostream>
#include "opencv2/opencv.hpp"

#include "functions.h"



/**
* @brief Screenshot Sava the srceen shot
* @param Image The hole image
* @param FileName The file name
* @param StartedX The started position x for the ROI
* @param StartedY The started position y for the ROI
* @param EndedX The ended position x for the ROI
* @param EndedY The ended position y for the ROI
* @return true when the operation is successed
* false otherwise
*
* @author sheng
* @date 2014-08-28
* @version 0.2.1
*
*
* @history
* <author> <date> <version> <description>
* sheng 2014-08-28 0.2.1 add some comment
* sheng 2014-08-27 0.2 fixed the bug that the
* program terminaled when
* the mouse is out of
* the image window.
* sheng 2014-08-26 0.1 build the module
*
*/
bool Screenshot(cv::Mat Image, const std::string& FileName,
const int& StartedX, const int& StartedY,
const int& EndedX, const int& EndedY)
{
// check whether the start point is out of the image window
if (StartedX < 0 )
{
std::cout << "The started is smaller than zero." << std::endl;
return false;
}

if (StartedY < 0)
{
std::cout << "The strated is smaller than zero." << std::endl;
return false;
}


// check whether the ended point is out of the image window
if (EndedY > Image.rows)
{
std::cout << "The EndedY is bigger than the width." << std::endl;
return false;
}

if (EndedX > Image.cols)
{
std::cout << "The EndedX is bigger than heigh." << std::endl;
return false;
}


// calculating the width and the heigh
int Width = EndedX - StartedX;
int Heigh = EndedY - StartedY;



// get the ROI rectangle.
cv::Rect ROI(StartedX, StartedY, Width, Heigh);
std::cout << "ROI rec." << std::endl;


// get the ROI image
cv::Mat ScreenshorImage = Image(ROI);


// save the ROI image.
bool Result = SaveImageInJPEG(FileName, ScreenshorImage);


return Result;

}


     Screenshot.cpp
/*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*/


#include "opencv2/opencv.hpp"
#include <iostream>
#include <QStringList>
#include "userdata.h"
#include "functions.h"


static const int VK_DOWN = 40;
static const int VK_UP = 38;
static const int ESC = 27;

void Test_OnMouse()
{
// the directory
QString Directory = "E:/Database_For_Experiment/vehicle/1";
QString FileName;

QStringList FileNameList = GetFilesInDirectory(Directory);

std::string WindowName = "Image";
cv::namedWindow(WindowName, CV_WINDOW_AUTOSIZE);

cv::Mat Image;
UserData userdata;
userdata.WindowName = WindowName;
userdata.OriginImage = &Image;
cv::setMouseCallback(WindowName, &OnMouse, static_cast<void*>(&userdata));

bool IsESC = false;

for (int Index = 0; Index < FileNameList.size(); Index++)
{
FileName = FileNameList.at(Index);
std::cout << "The name of the image is " <<
FileName.toLocal8Bit().constData() << std::endl;
FileName = Directory + "/" + FileName;

Image = cv::imread(FileName.toLocal8Bit().constData(),
cv::IMREAD_UNCHANGED);
cv::imshow(WindowName, Image);

bool InNext = false;

while(!InNext)
{
int Result = cv::waitKey();

switch (Result)
{
case 'd':
InNext = true;
break;

case 'u':
if (Index != 0)
{
InNext = true;

// back to the front image
Index = Index - 2;
}
break;


case ESC:
IsESC = true;
break;


default:
break;
}

// break the inner loop
if (IsESC)
{
break;
}
}

// break the loop
if (IsESC)
{
break;
}

}

cv::destroyAllWindows();
}


     main.cpp
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

#include <iostream>

using namespace cv;
using namespace std;

void Test_OnMouse();

int main( int argc, char** argv )
{

Test_OnMouse();
return 0;
}


github地址:https://github.com/shengno/Screenshot

enjoy it~

本文版权所有,欢迎转载,转载请注明出处,谢谢。





















基于OpenCV的抠图程序基于OpenCV的抠图程序基于OpenCV的抠图程序基于OpenCV的抠图程序基于OpenCV的抠图程序基于OpenCV的抠图程序基于OpenCV的抠图程序基于OpenCV的抠图程序基于OpenCV的抠图程序基于OpenCV的抠图程序基于OpenCV的抠图程序基于OpenCV的抠图程序基于OpenCV的抠图程序