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; }
}