OpenCV实现拼图板小游戏_第1页
OpenCV实现拼图板小游戏_第2页
OpenCV实现拼图板小游戏_第3页
OpenCV实现拼图板小游戏_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第OpenCV实现拼图板小游戏80后可能还对儿时玩过的一种经典木质的拼图板游戏记忆犹新,一般是一种4*4或5*5规格的手持活动板,通过挪动每个小板子的位置,拼出来板子上完整的图像,那时候还没有网吧,手机也还是大哥大的天下,所以这也可以算得上是最早的“手游”了吧。

今天我们用OpenCV来复现一下儿时的经典!

代码:

#include"core/core.hpp"

#include"highgui/highgui.hpp"

#include"imgproc/imgproc.hpp"

#includetime.h

usingnamespacecv;

MatSourceimage,Spilteimage,Rebuildimage,Dstimage;

introws,cols;

intRoirows,Roicols;

vectorMatarraryimage;

voidRandarrary(vectorMatvectorMat);//随机排列子图像序列函数

staticintvectornumber=0;

voidOnMouseAction(intevent,intx,inty,intflags,void*ustc);//鼠标回调事件函数

intmainFun()

Sourceimage=imread("D:\\test\\lena.jpg");

imshow("Sourceimage",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(inti=0;irows;i++)

for(intj=0;jcols;j++)

MatSourceRoi=Sourceimage(Rect(j*Roicols,i*Roirows,Roicols-1,Roirows-1));

arraryimage.push_back(SourceRoi);

//随机函数

Randarrary(arraryimage);

for(inti=0;irows;i++)

for(intj=0;jcols;j++)

MatSpilterRoi=Spilteimage(Rect(j*Roicols,i*Roirows,Roicols-1,Roirows-1));

addWeighted(SpilterRoi,0,arraryimage[vectornumber],1,0,SpilterRoi);

vectornumber++;

imshow("Spliteimage",Spilteimage);

waitKey(150);

setMouseCallback("Spliteimage",OnMouseAction);

waitKey();

return0;

//*******************************************************************//

//随机调换所有的子图像序列的位置,用于在Spliteimage中显示

//*******************************************************************//

voidRandarrary(vectorMatvectorMat)

for(inti=0;ivectorMat.size();i++)

srand(int(time(0)));

inta=rand()%(vectorMat.size()-i)+i;

swap(vectorMat[i],vectorMat[a]);

//*******************************************************************//

//鼠标回调函数,用于获取需要查找的子图像在原图像中的位置,并在叠加显示在目标图像中

//*******************************************************************//

voidOnMouseAction(intevent,intx,inty,intflags,void*ustc)

if(event==CV_EVENT_LBUTTONDOWN)

MatRoiSpilte,RoiSource;

introws=(y/Roirows)*Roirows;

intclos=(x/Roicols)*Roicols;

RoiSpilte=Spilteimage(Rect(clos,rows,Roicols,Roirows));

imshow("Slice",RoiSpilte);

Matimage=Mat::zeros(Sourceimage.rows-Roirows,Sourceimage.cols-Roicols,CV_32FC1);

matchTemplate(Sourceimage,RoiSpilte,image,1);

normalize(image,image,0,1,NORM_MINMAX);

doubleminV=0;

doublemaxV=0;

PointminP,maxP;

minMaxLoc(image,minV,maxV,minP,maxP);

MatROIDst

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论