OpenCV實(shí)現(xiàn)圖像切割功能
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)切割后圖像:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語(yǔ)言 文件的隨機(jī)讀寫(xiě)詳解及示例代碼
本文主要介紹C語(yǔ)言 文件的隨機(jī)讀寫(xiě),這里整理了相關(guān)資料及示例代碼以便大家學(xué)習(xí)參考,學(xué)習(xí)此部分內(nèi)容的朋友可以參考下2016-08-08用C編寫(xiě)一個(gè)送給女朋友的情人節(jié)小程序 可愛(ài)!
非常可愛(ài)的情人節(jié)小程序!文章為大家分享了用C編寫(xiě)一個(gè)送給女朋友的小程序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02C++編寫(xiě)生成不重復(fù)的隨機(jī)數(shù)代碼
本文給大家匯總介紹了3種c++實(shí)現(xiàn)生成不重復(fù)的隨機(jī)數(shù)的函數(shù),十分的簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下。2015-05-05c語(yǔ)言實(shí)現(xiàn)學(xué)生管理系統(tǒng)詳解
這篇文章主要為大家介紹了c語(yǔ)言實(shí)現(xiàn)學(xué)生管理系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助<BR>2021-12-12

C++實(shí)現(xiàn)順序表的常用操作(插入刪出查找輸出)

OpenCV視頻流C++多線(xiàn)程處理方法詳細(xì)分析