search
尋找貓咪~QQ 地點 桃園市桃園區 Taoyuan , Taoyuan

OPENCV 圖片放大、切割(分割)擷取、保存(儲存) – jashliao部落格

OPENCV 圖片放大、切割(分割)擷取、保存(儲存)



資料來源: https://blog.csdn.net/LordofRobots/article/details/72312704


GITHUB: https://github.com/jash-git/opencv249_image_cut_save


#include 
#include 
#include 
#include 

#include 
#include 

#include 
#if defined(WIN32)
    #define  TIMEB    _timeb
    #define  ftime    _ftime
    typedef __int64 TIME_T;
#else
    #define TIMEB timeb
    typedef long long TIME_T;
#endif

using namespace cv;
using namespace std;
Mat src;//input image

void Pause()
{
    printf("Press Enter key to continue...");
    fgetc(stdin);
}
int main()
{
   Mat input;
	input = imread("Lena_original.jpg");
	if (!input.data)
    {
		printf("could not load image...\n");
	}
    else
    {
        //放大
        pyrUp(input, src, Size(input.cols*2, input.rows*2));

        const char*  window_image = "input image";
        namedWindow(window_image, CV_WINDOW_AUTOSIZE);
        imshow(window_image, src);

        Rect rect00(0,0,src.cols/2,src.rows/2);   //创建一个Rect框,属于cv中的类,四个参数代表x,y,width,heigh
        Rect rect01(0,0+src.rows/2,src.cols/2,src.rows/2);
        Rect rect02(0+src.cols/2,0,src.cols/2,src.rows/2);
        Rect rect03(0+src.cols/2,0+src.rows/2,src.cols/2,src.rows/2);

        Mat mat00,mat01,mat02,mat03;

        Mat image_cut = Mat(src, rect00);      //从img中按照rect进行切割,此时修改image_cut时image中对应部分也会修改,因此需要copy
        mat00 = image_cut.clone();   //clone函数创建新的图片

        image_cut = Mat(src, rect01);
        mat01 = image_cut.clone();

        image_cut = Mat(src, rect02);
        mat02 = image_cut.clone();

        image_cut = Mat(src, rect03);
        mat03 = image_cut.clone();

        imshow("mat00", mat00);
        imshow("mat01", mat01);
        imshow("mat02", mat02);
        imshow("mat03", mat03);

        imwrite("00.png", mat00);
        imwrite("01.png", mat01);
        imwrite("02.png", mat02);
        imwrite("03.png", mat03);
    }
    waitKey(0);
    Pause();
    return 0;
}


熱門推薦

本文由 jashliaoeuwordpress 提供 原文連結

寵物協尋 相信 終究能找到回家的路
寫了7763篇文章,獲得2次喜歡
留言回覆
回覆
精彩推薦