OpenCV——运用于pixels war游戏

// The "Square Detector" program.
// It loads several images sequentially and tries to find squares in
// each image

#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

#include <iostream>
#include <math.h>
#include <string.h>
#include <vector>

using namespace cv;
using namespace std;

static void help()
{
    cout <<
        "\nA program using pyramid scaling, Canny, contours, contour simpification and\n"
        "memory storage (it‘s got it all folks) to find\n"
        "squares in a list of images pic1-6.png\n"
        "Returns sequence of squares detected on the image.\n"
        "the sequence is stored in the specified memory storage\n"
        "Call:\n"
        "./squares\n"
        "Using OpenCV version %s\n" << CV_VERSION << "\n" << endl;
}

int thresh = 50, N = 11;
const char* wndname = "Square Detection Demo";

// helper function:
// finds a cosine of angle between vectors
// from pt0->pt1 and from pt0->pt2
static double angle( Point pt1, Point pt2, Point pt0 )
{
    double dx1 = pt1.x - pt0.x;
    double dy1 = pt1.y - pt0.y;
    double dx2 = pt2.x - pt0.x;
    double dy2 = pt2.y - pt0.y;
    return (dx1*dx2 + dy1*dy2)/sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10);
}

// returns sequence of squares detected on the image.
// the sequence is stored in the specified memory storage
static void findSquares( const Mat& image, vector<vector<Point> >& squares )
{
    squares.clear();

    Mat pyr, timg, gray0(image.size(), CV_8U), gray;

    // down-scale and upscale the image to filter out the noise
    pyrDown(image, pyr, Size(image.cols/2, image.rows/2));
    pyrUp(pyr, timg, image.size());
    vector<vector<Point> > contours;

    // find squares in every color plane of the image
    for( int c = 0; c < 3; c++ )
    {
        int ch[] = {c, 0};
        mixChannels(&timg, 1, &gray0, 1, ch, 1);//分别将r,g,b三个通道的内容拷贝到gray0通道

        // try several threshold levels
        for( int l = 0; l < N; l++ )
        {
            // hack: use Canny instead of zero threshold level.
            // Canny helps to catch squares with gradient shading
            if( l == 0 )
            {
                // apply Canny. Take the upper threshold from slider
                // and set the lower to 0 (which forces edges merging)
                Canny(gray0, gray, 5 , thresh, 5);
                // dilate canny output to remove potential
                // holes between edge segments
                dilate(gray, gray, Mat(), Point(-1,-1));

            }
            else
            {
                // apply threshold if l!=0:
                //     tgray(x,y) = gray(x,y) < (l+1)*255/N ? 255 : 0
                gray = gray0 >= (l+1)*255/N;
            }

            // find contours and store them all as a list
            //imshow("temp",gray);
            findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

            vector<Point> approx;

            // test each contour
            for( size_t i = 0; i < contours.size(); i++ )
            {
                // approximate contour with accuracy proportional
                // to the contour perimeter
                approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true)*0.02, true);

                // square contours should have 4 vertices after approximation
                // relatively large area (to filter out noisy contours)
                // and be convex.
                // Note: absolute value of an area is used because
                // area may be positive or negative - in accordance with the
                // contour orientation

                int a=approx.size();
                int b=abs(contourArea(Mat(approx)));
                bool c=isContourConvex(Mat(approx));
                if( approx.size() == 4 &&
                    fabs(contourArea(Mat(approx))) >5 &&fabs(contourArea(Mat(approx)))<10000&&
                    isContourConvex(Mat(approx)) )
                {
                    double maxCosine = 0;

                    for( int j = 2; j < 5; j++ )
                    {
                        // find the maximum cosine of the angle between joint edges
                        double cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1]));
                        maxCosine = MAX(maxCosine, cosine);
                    }

                    // if cosines of all angles are small
                    // (all angles are ~90 degree) then write quandrange
                    // vertices to resultant sequence
                    if( maxCosine < 0.5 )
                        squares.push_back(approx);
                }
            }
        }
    }
}

// the function draws all the squares in the image
static void drawSquares( Mat& image, const vector<vector<Point> >& squares )
{
    //vector<Point> pointsToTest;
    vector<int> valueOfChannel0/*,valueOfChannel1,valueOfChannel2*/;
    int differentPoint,normalPoint,tempPoint=0;
    bool setDifferentPoint=0;
    //bool setNormalPoint=0;

    for( size_t i = 0; i < squares.size(); i++ )
    {
        const Point* p = &squares[i][0];
        int n = (int)squares[i].size();

        //////////////////////////////////////////////////////////////////////////////////////////////
        Point middlePoint((squares[i][0].x+squares[i][3].x)/2,(squares[i][0].y+squares[i][1].y)/2);
        //pointsToTest.push_back(middlePoint);
        tempPoint=(int)image.at<Vec3b>(middlePoint.y,middlePoint.x)[0];
        //cout<<tempPoint<<endl;
        if (0==i)
        {
            normalPoint=tempPoint;
            polylines(image, &p, &n, 1, true, Scalar(255,0,255), 1, CV_AA);
        }
        else
        {
            if (tempPoint!=normalPoint)
            {
                if (!setDifferentPoint)
                {
                    differentPoint=tempPoint;
                    setDifferentPoint=1;
                }
                polylines(image, &p, &n, 1, true, Scalar(0,0,255), 1, CV_AA);
            }
            else
            {
                polylines(image, &p, &n, 1, true, Scalar(255,0,255), 1, CV_AA);
            }

        }

    }

    imshow(wndname, image);
}

int main(int /*argc*/, char** /*argv*/)
{
    static const char* names[] = { "Image 001.png", "Image 002.png", "Image 003.png",
        "Image 004.png", "Image 005.png", "Image 006.png","Image 007.png",0 };
    help();
    namedWindow( wndname, 1 );
    vector<vector<Point> > squares;

    for( int i = 0; names[i] != 0; i++ )
    {
        Mat image = imread(names[i], 1);
        if( image.empty() )
        {
            cout << "Couldn‘t load " << names[i] << endl;
            continue;
        }

        findSquares(image, squares);
        drawSquares(image, squares);

        int c = waitKey();
        if( (char)c == 27 )
            break;
        remove("Image 001.png");
    }

    return 0;
}
时间: 2024-08-10 23:27:54

OpenCV——运用于pixels war游戏的相关文章

Facebook开源游戏平台ELF: 一个用于实时战略游戏研究的轻量级平台

ELF是一个用于游戏研究的应用广泛的(Extensive).轻量级的(Lightweight).灵活的(Flexible)平台,特别适用于实时战略(RTS)游戏.在C++方面,ELF采用C++线程来并发运行多个游戏.在Python方面,ELF可以一次性返回一批游戏状态,使其对现代RL(强化学习)非常友好.另一方面,在其他平台(例如OpenAI Gym)中,一个Python接口只能包含一个游戏实例.这使得游戏的并发运行有点复杂,而这又是许多现代强化学习算法的要求. 对于RTS游戏的研究,ELF配备

OpenCV做飞机射击类游戏(一)

1.OpenCV的配置: 要求: Win7  Win8  Win10系统. opencv的安装路径为默认. 电脑为 64 位. 1.安装Opencv1.0和VC6.0++ : VC6.0++的安装路径可以改变,但是Opencv的安装路径最默认,不要改变其安装路径,不然会很麻烦. 2.添加环境变量: 右键我的电脑->属性->高级系统设置->环境变量->用户变量的Path添加 C:\Program Files (x86)\OpenCV\bin; 3.进入VC6.0++ 进行配置: 工具

用于部署war并重启Tomcat的脚本

只需要定义两个变量, 一个是目标tomcat实例的目录, 另一个是war包的名称 # Please define the absolute path of tomcat instance THIS_TC_INSTANCE='/home/tomcat/tomcat8_jdk8_1' THIS_APP_MODULE='throne-commons' df -h echo '' pid=`ps -ef|grep ${THIS_TC_INSTANCE}|grep -v 'grep'|grep 'java

程序员如何用思维导图高效学习Java编程

Java作为一种常见的计算机编程语言,它具有简单.稳定.多线等特点,被广泛运用于PC.游戏控制台.数据中心.超级计算机以及互联网,相对于C++,Java会稍微容易些,但是依然需要我们学会很多的编程,作为一名程序员,如何系统的掌握这些程序呢?下面是分享的用思维导图学习Java编程方法介绍,一起看看吧! 为什么要用思维导图学习Java呢? 首先思维导图是一种结构性模型,有利于整合知识框架,其次,思维导图是一种带色彩的图文结合,相对于单纯的文字而言,更好的被人们所记住,迅捷画图作为一款好用的思维导图工

干货推荐:如何运维千台以上游戏云服务器——游族网络

干货推荐:如何运维千台以上游戏云服务器——游族网络 来自上海游族网络的运维总监李志勇,在3月4日云栖社区中带来的分享“如何运维千台以上游戏云服务器”.本次分享重点是云时代的运维,包括游戏上云部署整体方案.游戏服务器批量运维管理,并对企业选择RDS还是自建MySQL数据库给出了自己建议. 关于分享者: 李志勇,2010年加入游族网络,目前担任游族网络运维总监,全面负责游族网络运维业务.他具有十年运维工作经验,八年游戏行业从业经验,专注于游戏虚拟化技术和网络优化. 分享正文: 游戏产品架构进化史  

游戏运维的最佳实践:搜狐畅游自动化运维之旅!

搜狐黎志刚见证了畅游游戏自动化运维平台的从无到有,通过在其中踩过的坑.解过的结,他向大家来阐述游戏运维的进阶之路.本文主要围绕畅游游戏管理体系与运维自动化的演变历程.运维自动化的实现及未来运维四方面展开. 畅游运维管理体系与运维自动化的演变历程 畅游运维管理体系演变历程 从 2008 年毕业以实习生的身份进入搜狐畅游,我同公司一起成长,经历了整个运维管理体系从小到大的过程. 整个运维管理体系是从最初石器时代(脚本化),之后的青铜时代(半自动化).蒸汽时代(DevOPS)一路演变过来,现在处于自动

运维常识整理

运维常识整理基础服务: LAMP:Linux+Apache+MySQL+(PHP\Python\Perl) 是一组用来建立web应用平台的解决方案.LNMP:Linux+ Nginx+MySQL+PHP 网站服务器架构 Apache:一款可以跨平台的Web服务器软件.Nginx:一个高性能的HTTP和反向代理服务,也是一个IMAP/POP3/SMTP服务.MySQL:一个开源的关系型数据库管理系统.FTP:File Transfer Protocol(文件传输协议).控制文件的双向传输.DNS:

论黑客喜欢攻击地方网络棋牌游戏原因与解决方案

地方棋牌游戏很容易招到黑客攻击,尤其是对于一些比较出名的平台,风险就更高一些.究竟为什么黑客这么喜欢攻击地方性棋牌游戏呢?厦门欧页网络科技有限公司,与你一起探讨. 经过许多被攻击的案例显示,黑客之所以会选择攻击棋牌游戏平台是以下原因: 1. 盗卖游戏币.一般这种情况会出现在比较成熟的平台上,使用这种方式通常都是长期进行的过程.就像蛀米虫一样,慢慢的.悄悄的进行. 2. 勒索行为.这个是绝大部分黑客攻击平台的原因.如果出现这个原因的,黑客就不会专门去挑选平台,无论平台大小.是否成熟.做这种事的也并

网页游戏运营模式研究

网页游戏的定义   网页游戏的英文名称为又称无客户端网络游戏,它是基于浏览器的网络在线多人互动的游戏,也是网络游戏的一种.这种游戏的特点是玩家无需下载游戏客户端及安装,只需短短的几秒钟就可打开网页用浏览器加载就能玩的网络游戏.尤其适合上班及没有时间休闲娱乐的人群,只要花一点点时间并且消费成本比较低,而且游戏内具有自动成长功能,玩家即使关掉电脑也可心满意足的去工作,如果想随时了解游戏内的具体情况,还有一些游戏具有短信提醒功能. 中国网页游戏从发展初期,再到如今的爆发式发展,中国网页游戏规模不断的扩