• opencv 提取RGB并用cocos2d-x 纹理方式显示


    {

    #include "MainScene.h"
    #include "SimpleAudioEngine.h"
    #include <iostream>
    #include <string.h>
    using namespace std;
    #include "opencv.hpp"
    #include <opencv2/features2d.hpp>
    #include "opencv2/highgui.hpp"
    using namespace cv;
    
    
    extern "C"
    {
    #include "SDL.h"
    
    #include <libavutil/imgutils.h>
    #include <libavutil/parseutils.h>
    #include <libswscale/swscale.h>
    #include <libavcodec/avcodec.h>
    #include <libavformat/avformat.h>
    #include <libavformat/avio.h>
    #include <libavutil/file.h>
    #include <libavdevice/avdevice.h>
    }
    
    
    Scene* MainScene::createScene()
    {
        return MainScene::create();
    }
    
    
    bool MainScene::init()
    {
        if ( !Scene::init() )
        {
            return false;
        }
    
        visibleSize = Director::getInstance()->getVisibleSize();
        Vec2 origin = Director::getInstance()->getVisibleOrigin();
        
        string filename = "I:/ImageGrid/135708z4u8eha6gucne2ug.jpg";
        //string filename = "D:/PriPro/Pro/simulator/win32/src/client/res/log/Log_1.png";
    
        Image *image = new Image();
        image->autorelease();
        Mat img = imread(filename.c_str());
        int size = img.rows * img.cols * 4;
        uchar *data = new uchar[size];
        memset(data, 255, size);
    
        if (img.empty())
        {
            return 0;
        }
        int pos = 0;
    
        for (int i = 0; i < img.rows; i++)
        {
            for (int j = 0; j < img.cols; j++)
            {
                auto rgb = img.at<Vec3b>(i, j);
    
                pos += 4;
    
                uchar r = (uchar)rgb[2];
                data[pos - 4] = r;
                uchar g = (uchar)rgb[1];
                data[pos - 3] = g;
                uchar b = (uchar)rgb[0];
                data[pos - 2] = b;
                
            }
        }
    
    
    
    
    
        image->initWithRawData(data,
            img.rows * img.cols,
            img.cols,img.rows,
            (int)Texture2D::PixelFormat::RGBA8888);
        image->setPNGPremultipliedAlphaEnabled(true);
    
        Texture2D *tex = new Texture2D();
        tex->autorelease();
        tex->initWithImage(image);
    
    
    
        auto s = Sprite::createWithTexture(tex);
        s->setScale(0.5);
        s->setPosition(visibleSize / 2);
    
        this->addChild(s);
    
    
    
        return true;
    }

    }

  • 相关阅读:
    starUML学习笔记一
    android 反编译教程
    android asyncTask 笔记
    android v13 的新特性
    ViewPage+Fragment+indicator+Tabhost效果
    android dp sp pt mm in px
    Tabhost+framgent+ViewPager滑动效果
    android studio github 项目导入问题
    Fragment 中 onCreate和onCreateView的区别
    android 抽屉式滑动demo
  • 原文地址:https://www.cnblogs.com/YZFHKMS-X/p/13954018.html
Copyright © 2020-2023  润新知