opencv 图像分割与合并--用于神经网络 c++版本

opencv 图像分割与合并--用于神经网络 c++版本

原始图片:

在这里插入图片描述
分割结果
在这里插入图片描述

代码片


#include <iostream>
#include <vector>  
#include <core/core.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
struct segpic
{
    Mat m;
    int coordinateRow=0;
    int coordinateCol=0;
};
int main() {
    Mat img;
    Mat out;
    std::string path = "c:\\Users\\Lee\\Pictures\\02.jpg";
    img = cv::imread(path);
    imshow("原始图片", img);
    int row_seg = img.rows / 40;
    int col_seg = img.cols / 40;
    vector<segpic> imgarry;
    //分割图片,送入数组
    for (int row_star = 0;row_star+row_seg<=img.rows; row_star += row_seg)
        for(int col_star = 0;col_star+col_seg<=img.cols; col_star += col_seg)
    {
        segpic temp;
        out = img(Range(row_star, row_star + row_seg), Range(col_star, col_star + col_seg));//x,y
        temp.m = out;
        temp.coordinateRow = row_star;
        temp.coordinateCol = col_star;
       //分割图片写入文件夹--用时较长
       // String wpath;
        //wpath = "c:\\Users\\Lee\\Pictures\\pic\\"+to_string(row_star)+"-"+ to_string(row_star+ row_seg) + "-"+ to_string(col_star)+"-"+ to_string(col_star + col_seg)+".jpg";
        //imwrite(wpath,out);
        imgarry.push_back(temp);//存入数组
    }
    Mat combin = img.clone();//复制原图,用于尺寸固定
    combin.setTo(cv::Scalar(100, 0, 0));
    for (int i=0;i< imgarry.size();i++)
    {
        segpic temp = imgarry[i];
        Mat roi = temp.m;
        //copy到combin的指定位置
        roi.copyTo(combin(Rect(temp.coordinateCol, temp.coordinateRow, roi.cols, roi.rows)));
    }
    imshow("合并图片", combin);
    waitKey();
    return 0; 
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值