OpenCV實(shí)現(xiàn)圖像切割功能
更新時(shí)間:2019年01月12日 13:27:28 作者:lindamtd
這篇文章主要為大家詳細(xì)介紹了OpenCV實(shí)現(xiàn)圖像切割功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
openCV實(shí)現(xiàn)將圖像切成m*n塊,供大家參考,具體內(nèi)容如下
一、代碼部分:
#include "stdafx.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <vector>
#include<string>
#include<sstream>
using namespace std;
using namespace cv;
//Cut an image into m*n patch
void Cut_img(Mat src_img, int m, int n, vector<Mat> ceil_img)
{
int t = m * n;
int height = src_img.rows;
int width = src_img.cols;
int ceil_height = height / m;
int ceil_width = width / n;
Mat roi_img;
//String concatenation
ostringstream oss;
string str, str1, str2;
Point p1, p2;
for (int i = 0; i<m; i++)
{
for (int j = 0; j<n; j++)
{
Rect rect(j*ceil_width, i*ceil_height, ceil_width, ceil_height);
src_img(rect).copyTo(roi_img);
ceil_img.push_back(roi_img);
oss << i;
str1 = oss.str();
oss.str("");
oss << j;
str2 = oss.str();
oss.str("");
str = "roi_img_" + str1 + "_" + str2;
imshow(str, roi_img);
IplImage *ipl_roi_img=&IplImage(roi_img);
//save processed img
char tmp[100]="\0";
sprintf(tmp,"..\\post_img\\71253_%d_%d.jpg",i,j);
cvSaveImage(tmp,ipl_roi_img);
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
char *img_name_path="..\\image\\71253.jpg";
Mat img = imread(img_name_path,1);
imshow("src_img", img);
waitKey(0);
int m = 2;
int n = 2;
vector<Mat> ceil_img ;
Cut_img(img, m, n, ceil_img);
cvWaitKey(0);
return 0;
}
二、程序運(yùn)行結(jié)果:
(1)原圖像:

(2)切割后圖像:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語言初識動(dòng)態(tài)內(nèi)存管理malloc calloc realloc free函數(shù)
動(dòng)態(tài)內(nèi)存是相對靜態(tài)內(nèi)存而言的。所謂動(dòng)態(tài)和靜態(tài)就是指內(nèi)存的分配方式。動(dòng)態(tài)內(nèi)存是指在堆上分配的內(nèi)存,而靜態(tài)內(nèi)存是指在棧上分配的內(nèi)存2022-03-03
error LNK2019: 無法解析的外部符號 問題的解決辦法
error LNK2019: 無法解析的外部符號 問題的解決辦法,需要的朋友可以參考一下2013-05-05
如何用C++求兩個(gè)數(shù)的最大公約數(shù)和最小公倍數(shù)
最大公約數(shù)是指兩個(gè)或多個(gè)整數(shù)共有約數(shù)中,最大的一個(gè)約數(shù),常用的方法是歐幾里得算法,也叫輾轉(zhuǎn)相除法,下面這篇文章主要給大家介紹了關(guān)于如何用C++求兩個(gè)數(shù)的最大公約數(shù)和最小公倍數(shù)的相關(guān)資料,需要的朋友可以參考下2023-01-01
C語言實(shí)現(xiàn)對文件進(jìn)行操作的示例詳解
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)對文件進(jìn)行操作的相關(guān)知識,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)C語言有一定的幫助,需要的可以參考一下2023-04-04
C語言連續(xù)生成隨機(jī)數(shù)的實(shí)現(xiàn)方法
這篇文章主要介紹了C語言連續(xù)生成隨機(jī)數(shù)的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01

