# YUV2 pixel streaming without image

**URL:** <https://forum.opencv.org/t/yuv2-pixel-streaming-without-image/7305>\
**Category:** Uncategorized\
**Created:** [January 27, 2022, 6:53am UTC](https://forum.opencv.org/t/yuv2-pixel-streaming-without-image/7305 "2022-01-27T06:53:01Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![AKS](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@AKS](https://forum.opencv.org/u/AKS)\
**Post date:** [January 27, 2022, 6:53am UTC](https://forum.opencv.org/t/yuv2-pixel-streaming-without-image/7305/1 "2022-01-27T06:53:01Z")

</div>

Hi, I am working on a setup which Interfacing a qvga sensor and it gives me pixel streaming in YUV2 format since its interfaced as uvc. But I am not getting any image. I am new to this. What I can dig more to get the image? Please suggest~

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [January 27, 2022, 7:47am UTC](https://forum.opencv.org/t/yuv2-pixel-streaming-without-image/7305/2 "2022-01-27T07:47:34Z")

</div>

do you use `cv::VideoCapture`?

---

<div class="post-metadata">

**Author:** ![AKS](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@AKS](https://forum.opencv.org/u/AKS)\
**Post date:** [January 27, 2022, 9:06am UTC](https://forum.opencv.org/t/yuv2-pixel-streaming-without-image/7305/3 "2022-01-27T09:06:48Z")

</div>

@crackwitz Hi. I use _cap = cv2.VideoCapture(0)_ to access the data stream through USB.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [January 27, 2022, 9:32am UTC](https://forum.opencv.org/t/yuv2-pixel-streaming-without-image/7305/4 "2022-01-27T09:32:10Z")

</div>

ok then everything, including pixel format conversion, should have been done _for you_ so that `cap.read()` gives you the usual BGR data.

you could provide more detail. don’t assume that “not getting any image” is clear or debuggable.

---

<div class="post-metadata">

**Author:** ![AKS](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@AKS](https://forum.opencv.org/u/AKS)\
**Post date:** [January 27, 2022, 9:41am UTC](https://forum.opencv.org/t/yuv2-pixel-streaming-without-image/7305/5 "2022-01-27T09:41:47Z")

</div>

@crackwitz . I am getting this type data. the format seems yuv2 before sending to USB and this is what I am receiving at windows host application.

![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/4/4e3f62510c2a78d9c5757a2b82c8b81ae1bd3c3f.png)

any suggestions please~

Regards.

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [January 27, 2022, 10:15am UTC](https://forum.opencv.org/t/yuv2-pixel-streaming-without-image/7305/6 "2022-01-27T10:15:03Z")

</div>

please show your actual code

---

<div class="post-metadata">

**Author:** ![AKS](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@AKS](https://forum.opencv.org/u/AKS)\
**Post date:** [February 10, 2022, 7:13am UTC](https://forum.opencv.org/t/yuv2-pixel-streaming-without-image/7305/7 "2022-02-10T07:13:11Z")

</div>

@berak @crackwitz  
This is what I tried to get the raw gray-scale data, but no image is visible. please suggest~

```auto
import numpy as np
import cv2
cap = cv2.VideoCapture(0)

cols, rows = 340, 240
cap.set(cv2.CAP_PROP_FRAME_WIDTH, cols)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, rows)
cap.set(cv2.CAP_PROP_FPS, 30)

cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter.fourcc('Y','1','6',' '))
cap.set(cv2.CAP_PROP_CONVERT_RGB, 0)
cap.set(cv2.CAP_PROP_FORMAT, -1)

while(cap.isOpened()):  
        ret, frame = cap.read()
        if not ret:
            break

        frame = frame.reshape(rows, cols*2)
        frame = frame.astype(np.uint16)               
        frame = (frame[:, 0::2] << 8) + frame[:, 1::2]
        frame_roi = cv2.medianBlur(frame_roi, 3)
        frame_roi = frame_roi << 3

        normed = cv2.normalize(frame_roi, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)

        cv2.imshow('video',normed)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

cap.release()
cv2.destroyAllWindows()

```

This gives me below result of very very light shadow of hand (not clearly visible).  
 ![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/c/c48a5dd53a7f8748e79f8fd4736a2aedabc1085f.png)

Thanks.  
Best Regards.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [February 10, 2022, 12:15pm UTC](https://forum.opencv.org/t/yuv2-pixel-streaming-without-image/7305/8 "2022-02-10T12:15:13Z")

</div>

that looks like one U16 value is interpreted as two U8 values beside each other.

I see that you assemble the high and low bytes by hand, with the shifting. that is possible and should have worked.

you could use `.view()` instead of `.astype`. view does the equivalent of a “reinterpret\_cast”

could you use `np.save` on the raw frame from `cap.read` and upload that `npy` file somewhere? this forum doesn’t support attaching that file type.

---

<div class="post-metadata">

**Author:** ![AKS](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@AKS](https://forum.opencv.org/u/AKS)\
**Post date:** [February 11, 2022, 3:06am UTC](https://forum.opencv.org/t/yuv2-pixel-streaming-without-image/7305/9 "2022-02-11T03:06:40Z")

</div>

@crackwitz Thanks.  
I tried and here is the raw frame [Filebin | a4o0ksvxxhkyjyo6](https://filebin.net/a4o0ksvxxhkyjyo6)

.view() give me this.  
 ![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/4/4b859afe12a7d8de3b8ce9043ae2a299d2f66d4b.jpeg)

Please suggest~

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [February 11, 2022, 9:29am UTC](https://forum.opencv.org/t/yuv2-pixel-streaming-without-image/7305/10 "2022-02-11T09:29:17Z")

</div>

file appears to be just a numpy file header, and then no actual data. I can tell it was trying to save uint8 of shape (1, 163200)

could you try that again? it should have written a file that’s around 160 kB in size

---

<div class="post-metadata">

**Author:** ![AKS](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@AKS](https://forum.opencv.org/u/AKS)\
**Post date:** [February 11, 2022, 9:46am UTC](https://forum.opencv.org/t/yuv2-pixel-streaming-without-image/7305/11 "2022-02-11T09:46:07Z")

</div>

Hello @crackwitz . Thanks.  
yes, I tried again and attached it here [Filebin | a4o0ksvxxhkyjyo6](https://filebin.net/a4o0ksvxxhkyjyo6)

please check!

Regards.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [February 11, 2022, 11:48am UTC](https://forum.opencv.org/t/yuv2-pixel-streaming-without-image/7305/12 "2022-02-11T11:48:25Z")

</div>

okay I played around with the data and it’s “weird”.

I can see a 4-byte stride pattern. that agrees with your statement that this is supposed to be “YUV2”. so I don’t believe it’s Y16 (grayscale) exactly. I’ve taken this data apart into four planes of 1-byte data strided 4-bytes.

if this were Y16, at least one “plane” would show some interesting data or darkness (high bits), or wraparound artefacts (low bits). I don’t see any of that.

any byte-slice of stride 2 or 4 shows either noise, or fairly flat grayish data with some quantization/banding. I believe offsets 0 and 2 (of 4) are chroma. they show banding and they’re centered around a medium value. luma is all over the place, both of them.

- [Y16 yuv pixel format](https://www.fourcc.org/pixel-format/yuv-y16/)
- [YUY2 yuv pixel format](https://www.fourcc.org/pixel-format/yuv-yuy2/)
- [UYVY yuv pixel format](https://www.fourcc.org/pixel-format/yuv-uyvy/)

the frame data being 340x240… overscan. oh well.

I haven’t made the picture look like much yet. is it supposed to show anything? or just darkness/noise? what are the gain settings?

---

<div class="post-metadata">

**Author:** ![AKS](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@AKS](https://forum.opencv.org/u/AKS)\
**Post date:** [February 13, 2022, 12:58pm UTC](https://forum.opencv.org/t/yuv2-pixel-streaming-without-image/7305/13 "2022-02-13T12:58:55Z")

</div>

@crackwitz Thanks for your comment and sharing. The .npy file is the result of the code (shared in previous post) with ‘.astype’

**Host application flow**

- Data format

- Data order

It shows grayscale pattern with very very light shadow of the image (ex. hand). I tried it with CLAHE too to clear the contrast but nothing much improved. I didn’t apply any of the gain settings now.

Please suggest~

Best Regards.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [February 13, 2022, 2:29pm UTC](https://forum.opencv.org/t/yuv2-pixel-streaming-without-image/7305/14 "2022-02-13T14:29:58Z")

</div>

> [@AKS](#):
>
> The .npy file is the result of the code (shared in previous post) with ‘.astype’

I was expecting to work with _exactly the data that comes out of `cap.read()`_, with no alterations at all. I won’t spend that time investigating again.

---

<div class="post-metadata">

**Author:** ![AKS](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@AKS](https://forum.opencv.org/u/AKS)\
**Post date:** [February 13, 2022, 2:54pm UTC](https://forum.opencv.org/t/yuv2-pixel-streaming-without-image/7305/15 "2022-02-13T14:54:09Z")

</div>

Hello @crackwitz  
yes, the data .npy is the direct data from “`ret, frame = cap.read()`”.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [February 13, 2022, 11:12pm UTC](https://forum.opencv.org/t/yuv2-pixel-streaming-without-image/7305/16 "2022-02-13T23:12:54Z")

</div>

then I don’t know what the data means. might be corrupted somehow. or you left the lens cover on the objective and the sensor raised the gain until it’s all just noise. impossible to tell.

---

<div class="post-metadata">

**Author:** ![AKS](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@AKS](https://forum.opencv.org/u/AKS)\
**Post date:** [February 13, 2022, 11:50pm UTC](https://forum.opencv.org/t/yuv2-pixel-streaming-without-image/7305/17 "2022-02-13T23:50:06Z")

</div>

@crackwitz Yes, there is lens cover on the the top of the sensor. Could it effect the data out this much? Do you think, should I test it without keeping lens on sensor? Does it make some sense?  
Please suggest~
