OpenCV實(shí)現(xiàn)拼圖板小游戲
80后可能還對(duì)兒時(shí)玩過(guò)的一種經(jīng)典木質(zhì)的拼圖板游戲記憶猶新,一般是一種4*4或5*5規(guī)格的手持活動(dòng)板,通過(guò)挪動(dòng)每個(gè)小板子的位置,拼出來(lái)板子上完整的圖像,那時(shí)候還沒(méi)有網(wǎng)吧,手機(jī)也還是大哥大的天下,所以這也可以算得上是最早的“手游”了吧。
今天我們用OpenCV來(lái)復(fù)現(xiàn)一下兒時(shí)的經(jīng)典!
代碼:
#include "core/core.hpp" #include "highgui/highgui.hpp" #include "imgproc/imgproc.hpp" #include <time.h> using namespace cv; Mat Sourceimage, Spilteimage, Rebuildimage, Dstimage; int rows, cols; int Roirows, Roicols; vector<Mat>arraryimage; void Randarrary(vector<Mat> &vectorMat); //隨機(jī)排列子圖像序列函數(shù) static int vectornumber = 0; void OnMouseAction(int event, int x, int y, int flags, void *ustc); //鼠標(biāo)回調(diào)事件函數(shù) int mainFun() { Sourceimage = imread("D:\\test\\lena.jpg"); imshow("Source image", Sourceimage); rows = 6; //將圖像分割成rows行 cols = 4; //將圖像分割成cols列 Roirows = Sourceimage.rows / rows; Roicols = Sourceimage.cols / cols; Spilteimage = Mat::zeros(Sourceimage.rows, Sourceimage.cols, Sourceimage.type()); Dstimage = Mat::zeros(Sourceimage.rows, Sourceimage.cols, Sourceimage.type()); for (int i = 0; i<rows; i++) { for (int j = 0; j<cols; j++) { Mat SourceRoi = Sourceimage(Rect(j*Roicols, i*Roirows, Roicols - 1, Roirows - 1)); arraryimage.push_back(SourceRoi); } } // 隨機(jī)函數(shù) Randarrary(arraryimage); for (int i = 0; i<rows; i++) { for (int j = 0; j<cols; j++) { Mat SpilterRoi = Spilteimage(Rect(j*Roicols, i*Roirows, Roicols - 1, Roirows - 1)); addWeighted(SpilterRoi, 0, arraryimage[vectornumber], 1, 0, SpilterRoi); vectornumber++; imshow("Splite image", Spilteimage); waitKey(150); } } setMouseCallback("Splite image", OnMouseAction); waitKey(); return 0; } //*******************************************************************// //隨機(jī)調(diào)換所有的子圖像序列的位置,用于在 Splite image中顯示 //*******************************************************************// void Randarrary(vector<Mat>& vectorMat) { for (int i = 0; i<vectorMat.size(); i++) { srand(int(time(0))); int a = rand() % (vectorMat.size() - i) + i; swap(vectorMat[i], vectorMat[a]); } } //*******************************************************************// //鼠標(biāo)回調(diào)函數(shù),用于獲取需要查找的子圖像在原圖像中的位置,并在疊加顯示在目標(biāo)圖像中 //*******************************************************************// void OnMouseAction(int event, int x, int y, int flags, void *ustc) { if (event == CV_EVENT_LBUTTONDOWN) { Mat RoiSpilte, RoiSource; int rows = (y / Roirows)*Roirows; int clos = (x / Roicols)*Roicols; RoiSpilte = Spilteimage(Rect(clos, rows, Roicols, Roirows)); imshow("Slice", RoiSpilte); Mat image = Mat::zeros(Sourceimage.rows - Roirows, Sourceimage.cols - Roicols, CV_32FC1); matchTemplate(Sourceimage, RoiSpilte, image, 1); normalize(image, image, 0, 1, NORM_MINMAX); double minV = 0; double maxV = 0; Point minP, maxP; minMaxLoc(image, &minV, &maxV, &minP, &maxP); Mat ROIDst = Dstimage(Rect(minP.x, minP.y, Roicols, Roirows)); addWeighted(ROIDst, 0, RoiSpilte, 1, 0, ROIDst, -1); imshow("Jigsaw image", Dstimage); } } //-----開(kāi)始------ void COpenCVLearningDlg::OnBnClickedStartButton() { mainFun(); }
效果:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++基于消息隊(duì)列的多線(xiàn)程實(shí)現(xiàn)示例代碼
這篇文章主要給大家介紹了關(guān)于C++基于消息隊(duì)列的多線(xiàn)程實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用C++具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04C++產(chǎn)生隨機(jī)數(shù)的幾種方法小結(jié)
本文主要介紹了C++產(chǎn)生隨機(jī)數(shù)的幾種方法小結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03C++利用宏實(shí)現(xiàn)類(lèi)成員反射詳解
這篇文章主要為大家詳細(xì)介紹了C++如何利用宏實(shí)現(xiàn)類(lèi)成員反射,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,有興趣的小伙伴可以了解一下2024-01-01C++通過(guò)msxml調(diào)用webservice示例分享
這篇文章主要介紹了C++通過(guò)msxml調(diào)用webservice示例分享,需要的朋友可以參考下2014-03-03C++中strcpy函數(shù)的實(shí)現(xiàn)
strncpy這個(gè)可以指定拷貝字符的長(zhǎng)度,指定源地址,目標(biāo)地址,還有需要拷貝的字符的長(zhǎng)度; strcpy只能傳入兩個(gè)參數(shù),只指定拷貝的起始地址跟目標(biāo)地址,然后整體拷貝;2015-10-10Qt實(shí)現(xiàn)制作簡(jiǎn)單的計(jì)算器
計(jì)算器是我們生活中很常見(jiàn)的東西,它可以由多種語(yǔ)言多種方式來(lái)實(shí)現(xiàn)。本文主要介紹的是利用Qt實(shí)現(xiàn)的簡(jiǎn)易計(jì)算器的制作,文中的示例代碼講解詳細(xì),需要的可以參考一下2022-12-12在C++17中實(shí)現(xiàn)無(wú)鎖數(shù)據(jù)結(jié)構(gòu)的方法詳解
在探索?C++17?中的無(wú)鎖數(shù)據(jù)結(jié)構(gòu)之前,我們首先需要理解無(wú)鎖編程的基本概念及其在現(xiàn)代軟件開(kāi)發(fā)中的重要性,在這個(gè)章節(jié)中,我們將深入探討無(wú)鎖編程的概念,以及它如何滿(mǎn)足人類(lèi)對(duì)于更高效、更可靠軟件的本能需求,文中通過(guò)代碼示例介紹的非常詳細(xì),感興趣的朋友可以參考下2023-12-12