QT中使用opencv c++批量处理图像(缩放、转灰度)



main.cpp


#include 
#include 
#include

using namespace std;
using namespace cv;

int main()
{
    cv::Mat outImage,dstImg,andstImg;
    cv::String src_path =  "/home/jacky/people re/new/*.JPG";
    cv::String newName_path = "/home/jacky/people re";
    char newName[10];
    std::vector filenames;
    cv::glob(src_path, filenames);//获取路径下所有文件名
    cout << "filenames.size:" << filenames.size() << endl;
    for(int i = 0; i < filenames.size(); i++)
    {
        String ImgName = filenames[i];
        if(ImgName.find(".JPG") != String::npos)//匹配.png文件
        {
            outImage = imread(ImgName,CV_LOAD_IMAGE_UNCHANGED);//CV_LOAD_IMAGE_UNCHANGED以原始图像读取

            cv::cvtColor(outImage, dstImg, CV_RGB2GRAY);
            cv::resize(dstImg,andstImg,Size(50,50));
            sprintf(newName, "%04d.jpg", i);
            imwrite(newName_path+newName, andstImg);//文件夹必须提前创建,否则代码执行没有效果
            cout << newName_path+newName << endl;
        }
        else
        {
            cout << "NOT A PNG IMAGE" << endl;
        }
    }
    return 0;
}


.pro






TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += \
        main.cpp


INCLUDEPATH += /usr/local/include \
               /usr/local/include/opencv \
               /usr/local/include/opencv2
LIBS += /usr/lib/x86_64-linux-gnu/libopencv_highgui.so \
        /usr/lib/x86_64-linux-gnu/libopencv_core.so    \
        /usr/lib/x86_64-linux-gnu/libopencv_imgproc.so \
        /usr/lib/x86_64-linux-gnu/libopencv_videoio.so \
        /usr/lib/x86_64-linux-gnu/libopencv_objdetect.so\
        /usr/lib/x86_64-linux-gnu/libopencv_imgcodecs.so


你可能感兴趣的:(QT中使用opencv c++批量处理图像(缩放、转灰度))