# Syphon Texture to Surface

**URL:** <https://discourse.libcinder.org/t/syphon-texture-to-surface/1075>\
**Category:** Using Cinder\
**Created:** [March 2, 2018, 11:52am UTC](https://discourse.libcinder.org/t/syphon-texture-to-surface/1075 "2018-03-02T11:52:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Punk\_IPA](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/punk_ipa/32/538_2.png) [@Punk\_IPA](https://discourse.libcinder.org/u/Punk_IPA)\
**Post date:** [March 2, 2018, 11:52am UTC](https://discourse.libcinder.org/t/syphon-texture-to-surface/1075/1 "2018-03-02T11:52:48Z")

</div>

Hello.

I receive the video data from Syphon and I can draw it on screen as texture.

```
ciSyphon::ClientRef client;

gl::TextureRef mTex;

```

======

setup():

```
client = ciSyphon::Client::create();
client->setup();
client->bind();

```

======

update():

```
 mTex = client->fetchFrame();

```

======

draw():

```
gl::clear( Color(0, 0, 0 ) );
gl::draw(mTex);

```

================

It’s easy. But now I want to send mText to OpenCV for face detection. And that’s a problem because OpenCV works with Surface only. I don’t understand how to pass data from Texture to Surface. Maybe it’s not the best solution because of CPU and GPU but I don’t know another way. All examples I found doesn’t work. I would be very appreciate for any help.

---

<div class="post-metadata">

**Author:** ![xumo](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/xumo/32/75_2.png) [@xumo](https://discourse.libcinder.org/u/xumo)\
**Post date:** [March 2, 2018, 12:15pm UTC](https://discourse.libcinder.org/t/syphon-texture-to-surface/1075/2 "2018-03-02T12:15:30Z")

</div>

You can use the `createSource ()` method from [Texture2d](https://libcinder.org/docs/classcinder_1_1gl_1_1_texture2d.html) and the apply the `cv::toOcv` method to use it with OpenCV.

```
ci::Surface mySurface =mTex->createSource();
cv::Mat ocvImage = cv::toOcv(&mySurface); //May be just mySurface instead of &mySurface
```

---

<div class="post-metadata">

**Author:** ![Punk\_IPA](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/punk_ipa/32/538_2.png) [@Punk\_IPA](https://discourse.libcinder.org/u/Punk_IPA)\
**Post date:** [March 3, 2018, 6:33am UTC](https://discourse.libcinder.org/t/syphon-texture-to-surface/1075/3 "2018-03-03T06:33:17Z")

</div>

Thank you so much. The next step is to find my face 😀

---

<div class="post-metadata">

**Author:** ![lithium](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/lithium/32/38_2.png) [@lithium](https://discourse.libcinder.org/u/lithium)\
**Post date:** [March 3, 2018, 7:49am UTC](https://discourse.libcinder.org/t/syphon-texture-to-surface/1075/4 "2018-03-03T07:49:26Z")

</div>

Just a heads up, you may want to have a look at what ciSyphon is doing internally, because from my limited understanding of how it works, this solution will receive the frame as CPU side pixels (which is what you want), upload them to the GPU, only for you to pull the pixels back down again. All of this causes a lot of stalling, so you might be better off modifying the library to give you access to the pixels prior to creating an OpenGL texture if you find performance becomes a problem.

---

<div class="post-metadata">

**Author:** ![Punk\_IPA](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/punk_ipa/32/538_2.png) [@Punk\_IPA](https://discourse.libcinder.org/u/Punk_IPA)\
**Post date:** [March 3, 2018, 10:35am UTC](https://discourse.libcinder.org/t/syphon-texture-to-surface/1075/5 "2018-03-03T10:35:08Z")

</div>

Yes, you’re right. Frames are freezing, lagging and so on… performance is poor. But I’m afraid it would be very difficult for me to understand what is going on in Syphon under the hood. I have about 5 years experience in Python, but I’m total newbie in C++ and aspecially Obj-C. But I’ll try. Maybe you will see a new Syphon fork on git hub. 🕶

---

<div class="post-metadata">

**Author:** ![Punk\_IPA](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/punk_ipa/32/538_2.png) [@Punk\_IPA](https://discourse.libcinder.org/u/Punk_IPA)\
**Post date:** [March 3, 2018, 7:16pm UTC](https://discourse.libcinder.org/t/syphon-texture-to-surface/1075/6 "2018-03-03T19:16:08Z")

</div>

So, I spent a day for it.  
1). There is no way to get CPU pixels from Syphon.  
2). As far as I understood OpenCV can work on GPU but not in the current version for Cinder 0.9.1.  
3). I learned to write Syphon textures to Fbo, and I can draw it on screen as well. But I don’t know how it could be useful for my problem. 🙂 Maybe OpenCV can get frames from Fbo?  
4). Sorry for my English, it’s not my native language 🙂

---

<div class="post-metadata">

**Author:** ![gabor\_papp](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/gabor_papp/32/769_2.png) [@gabor\_papp](https://discourse.libcinder.org/u/gabor_papp)\
**Post date:** [March 3, 2018, 8:04pm UTC](https://discourse.libcinder.org/t/syphon-texture-to-surface/1075/7 "2018-03-03T20:04:25Z")

</div>

You are right, Syphon is GPU, that’s why it is fast. Why do you need it, though? Can’t you capture the camera stream directly from Cinder?

If not, look into PBO read back, that might help get rid of the lag you are experiencing. Check out this sample:

> **[cinder/Cinder](https://github.com/cinder/Cinder/tree/master/samples/_opengl/PBOReadBack)**
>
> Cinder is a community-developed, free and open source library for professional-quality creative coding in C++. - cinder/Cinder

-Gabor
