Is there any attempts made to use OpenCV in cocos2d-x?

Hello
i wander is there any known attempts made to use OpenCV in cocos2d-x ?
any open source or tutorials ?
even for cocos2d objc version …

1 Like

@yuye mentioned a possibility of creating a tutorial on OpenCV in this post, but I haven’t heard if he’s decided to make that one or not. Perhaps he can comment?

I’ve made this: https://itunes.apple.com/us/app/egg-catcher-cv/id855123929 with cocos2dx and OpenCV, it’s not that hard to implement, easier if you use NativeCamera. The hard part was making this work with threads (It was on cocos2dx 2.3). Once you integrate OpenCV you can get the camera like so (Note: That’s the front camera, for the back one remove the + 1):

    #ifdef ANDROID
    	VideoCapture camera(CV_CAP_ANDROID + 1);
    #elif IOS
    	VideoCapture camera(CV_CAP_AVFOUNDATION + 1);
    #else
    	VideoCapture camera(CV_CAP_ANY);
    #endif

After that you need to convert the captured frame(cv::Mat) to cocos2dx texture:

// Note: frame is cv::Mat
[CCTexture instnce]->initWithData(frame.data, kCCTexture2DPixelFormat_RGBA8888, frame.cols, frame.rows, CCSizeMake(frame.cols, frame.rows));

This is only example for cocos2d-x v2! With cocos2d-x v3 There may be much better way, and also this may not work.

So if i like to process image and save the result for presenting , what should i do ?
in iOS .
can i save image to disk in iOS app?

You can use FileUtils::getInstance()->getWritablePath() this will get you the Documents folder of your iOS app, where you can write/read files.

ok thanks ,
say i have image i like to process it with OpenCV ,
where should i save the processed image in memory ( what is the best way ? )
so i could present the processed image on screen ?

If I understand the question correctly, you want to convert it and store it in a cocos2dx Sprite. Maybe if you can explain exactly what you need to do I can help you more.

yes @dimi_t_d
something like that ,
take image i have run some openCV filter or effect on the image
the output present on Sprite .

Ok , so you can use the above example I gave. In cocos2dx v3 Something like this:

// Assuming frame is cv::Mat
auto texture = new Texture2D;
texture->initWithData(frame.data, 
		      frame.elemSize() * frame.cols * frame.rows, 
		      Texture2D::PixelFormat::RGBA8888, 
		      frame.cols, 
		      frame.rows, 
		      Size(frame.cols, frame.rows));

auto sprite = Sprite::createWithTexture(texture);

Thanks! i will try it