# Remove 6 bit from 16 bit depth pixels

**URL:** <https://forum.opencv.org/t/remove-6-bit-from-16-bit-depth-pixels/7938>\
**Category:** C++\
**Tags:** videoio, imgproc\
**Created:** [March 14, 2022, 5:32pm UTC](https://forum.opencv.org/t/remove-6-bit-from-16-bit-depth-pixels/7938 "2022-03-14T17:32:06Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![LB91](https://avatars.discourse-cdn.com/v4/letter/l/3da27b/32.png) [@LB91](https://forum.opencv.org/u/LB91)\
**Post date:** [March 14, 2022, 5:32pm UTC](https://forum.opencv.org/t/remove-6-bit-from-16-bit-depth-pixels/7938/1 "2022-03-14T17:32:06Z")

</div>

Hello,  
I am a newbie to opencv, so I try to learn from forum posts and videos. But now I reached a deadlock. So I decided to ask the community. I have a camera which streaming raw 10 bit image via USB, I have created a firmware which can send this stream as a 16 bit YUY video(UVC not supports raw rgb). But now I have to remove the extra bits from every pixel to show the video without distortion. Is openCV the proper tool for the task?

---

<div class="post-metadata">

**Author:** ![kbarni](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/kbarni/32/26_2.png) [@kbarni](https://forum.opencv.org/u/kbarni)\
**Post date:** [March 14, 2022, 9:49pm UTC](https://forum.opencv.org/t/remove-6-bit-from-16-bit-depth-pixels/7938/2 "2022-03-14T21:49:14Z")

</div>

It should be simple. You can create directly an image of arbitrary type from a raw buffer. You receive a `W*H*2` bytes image in the `buffer` variable.

```auto
uchar *buffer;
int W,H;
network.receive(buffer,&W,&H); //or something like this
Mat depth_image(H,W,CV_16UC1,buffer); //create a 1 channel 16 bit Mat from the buffer

```

Voilà, that’s all!

---

<div class="post-metadata">

**Author:** ![LB91](https://avatars.discourse-cdn.com/v4/letter/l/3da27b/32.png) [@LB91](https://forum.opencv.org/u/LB91)\
**Post date:** [March 15, 2022, 3:11pm UTC](https://forum.opencv.org/t/remove-6-bit-from-16-bit-depth-pixels/7938/3 "2022-03-15T15:11:57Z")

</div>

Thank you for your response,  
Maybe I misunderstand something, but I receive 3 channel color images(distorted but color). If I cast them into single channel, they will became grayscale, won’t they? Also can a 16 bit one channel image show the true colors of a 10 bit image?

Best Regards,  
Bence

---

<div class="post-metadata">

**Author:** ![kbarni](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/kbarni/32/26_2.png) [@kbarni](https://forum.opencv.org/u/kbarni)\
**Post date:** [March 16, 2022, 9:07am UTC](https://forum.opencv.org/t/remove-6-bit-from-16-bit-depth-pixels/7938/4 "2022-03-16T09:07:25Z")

</div>

I don’t really understand what is the format of your image. This is the solution for a 16 bits/pixel YUV 4:4:4 encoding.  
If you have 16 bits/channel color image (some kind of HDR image) or something similar, just create a `Mat` with the actual format, then use `cvtColor` or `split` to convert it to grayscale, which will contain the depth data.

```auto
Mat received_image(H,W,CV_16UC3,buffer);
Mat depth_image;
cvtColor(received_image,depth_image,COLOR_YUV2GRAY);

```

---

<div class="post-metadata">

**Author:** ![LB91](https://avatars.discourse-cdn.com/v4/letter/l/3da27b/32.png) [@LB91](https://forum.opencv.org/u/LB91)\
**Post date:** [March 18, 2022, 3:43pm UTC](https://forum.opencv.org/t/remove-6-bit-from-16-bit-depth-pixels/7938/5 "2022-03-18T15:43:34Z")

</div>

Thank you for your response, and sorry for my poor explanation as I mentioned i am pretty new to opencv and image formats. I found a python code which after a minimal modification resolved the image quality problem, but this code example reduce the channels and i got a grayscale image and if i try to recreate the colors I always got fake colors(the camera sensor original coloring a 10 bit raw RGB with bggr bayer). The code is well commented, but if i try to avoid the channel reduction i always messed up the image.  
Used Python code:

```auto
# Fetch undecoded RAW video streams
cap.set(cv2.CAP_PROP_FORMAT, -1) # Format of the Mat objects. Set value -1 to fetch undecoded RAW video streams (as Mat 8UC1)
while True:
    # Capture frame-by-frame
    ret, frame = cap.read()#read into np array with [1,320000] h*w*2 byte
    #print(frame.shape)
    if not ret:
        break
    # Convert the frame from uint8 elements to big-endian signed int16 format.
    frame = frame.reshape(rows, cols*2) # Reshape to 400*800
    frame = frame.astype(np.uint16) # Convert uint8 elements to uint16 elements
    frame = (frame[:, 0::2] << 8) + frame[:, 1::2] # Convert from little endian to big endian (apply byte swap), the result is 800x400.
    frame = frame.view(np.int16)

    # Apply some processing for disapply (this part is just "cosmetics"):
    frame_roi = frame[:, 10:-10] # Crop 400x400 (the left and right parts are not meant to be displayed).
    # frame_roi = cv2.medianBlur(frame_roi, 3) # Clean the dead pixels (just for better viewing the image).
    frame_roi = frame_roi << 6 # shift the 6 most left bits
    normed = cv2.normalize(frame_roi, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8UC3) # Convert to uint8 with normalizing (just for viewing the image).

```

I attached 2 images before the python script and after:  
 ![oripur](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/e/e914964c4823de844fa32e53eaa71b6a85cc848c.png)  
 ![gray](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/7/72b349f446a13b013f9b514ddcdd23c206b98fcf.png)

---

<div class="post-metadata">

**Author:** ![kbarni](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/kbarni/32/26_2.png) [@kbarni](https://forum.opencv.org/u/kbarni)\
**Post date:** [March 28, 2022, 9:00am UTC](https://forum.opencv.org/t/remove-6-bit-from-16-bit-depth-pixels/7938/6 "2022-03-28T09:00:07Z")

</div>

Okay, now I think I understand. But still don’t get why you are using RAW frames…  
In this case try to understand Bayer filters, RAW formats and color interpolation: [wikipedia](https://en.wikipedia.org/wiki/Bayer_filter)  
Anyway, your conversion should be:

```auto
ret,frame=cap.read()
colorframe=cv2.cvtColor(frame,cv2.COLOR_BayerBG2BGR)
#...now send colorframe on the network...

```

Anyway, unless it’s absolutely needed, you can set the capture mode directly to RGB color.

---

<div class="post-metadata">

**Author:** ![LB91](https://avatars.discourse-cdn.com/v4/letter/l/3da27b/32.png) [@LB91](https://forum.opencv.org/u/LB91)\
**Post date:** [March 28, 2022, 11:04am UTC](https://forum.opencv.org/t/remove-6-bit-from-16-bit-depth-pixels/7938/7 "2022-03-28T11:04:26Z")

</div>

Hello,  
Since the last post, I managed (with some help and hint and read) to create a C++ code snippet which is able to show the image and yes the COLOR\_BayerGB2BGR create an almost proper image, only with a yellowish overlay. But as much I know it is a common problem with the de-bayered images. Now I am working on this.  
 ![Display COLOR_BayerGB2BGR_screenshot_28.03.20222](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/c/c823a7b739c42a945124a9844d20f4d87e21e75d.png)

---

<div class="post-metadata">

**Author:** ![kbarni](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/kbarni/32/26_2.png) [@kbarni](https://forum.opencv.org/u/kbarni)\
**Post date:** [March 29, 2022, 9:39am UTC](https://forum.opencv.org/t/remove-6-bit-from-16-bit-depth-pixels/7938/8 "2022-03-29T09:39:51Z")

</div>

If the colors are off, you should check if you are using the correct debayering algorithm. Try the COLOR\_BayerBG2BGR, COLOR\_BayerGB2BGR, COLOR\_BayerRG2BGR and COLOR\_BayerGR2BGR until you get the correct colors.  
I prefer to use color pencils, color checker board or a colorful image to check if the colors are good.

---

<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:** [March 29, 2022, 3:23pm UTC](https://forum.opencv.org/t/remove-6-bit-from-16-bit-depth-pixels/7938/9 "2022-03-29T15:23:50Z")

</div>

picture would be “good”, if you subtracted roughly 0.5 and applied gamma compression for display (debayered values are probably still linear). that foamy stuff in the corners seems to be pink bubble wrap, and the top right corner shows some (bluish) daylight.

here’s the picture, [128,255] stretched to [0,255], and given an exponent of 0.45 (inverse of ~2.2):

![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/5/52a5559b67a73b91406c3446f8d557d1f9adada2.jpeg)

---

<div class="post-metadata">

**Author:** ![LB91](https://avatars.discourse-cdn.com/v4/letter/l/3da27b/32.png) [@LB91](https://forum.opencv.org/u/LB91)\
**Post date:** [March 30, 2022, 6:04am UTC](https://forum.opencv.org/t/remove-6-bit-from-16-bit-depth-pixels/7938/10 "2022-03-30T06:04:54Z")

</div>

Thank you for this hint. I tired them out and still think the COLOR\_BayerGB2BGR is the closest to the reality. Just need to tune the sensor or the image.  
**bg2bgr**  
 ![bg2bgr](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/a/a72febea421b6fe3f451493aaf517af12ac26fa7.png)  
**rg2bgr**  
 ![rg2bgr](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/f/fb7ba47b56704c0cf00bac14e79f3d56edb53324.png)  
**gb2bgr**  
 ![gb2bgr](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/5/518d577bca02843aa3302f9e536fc30b72aecee9.png)  
**gr2bgr**  
 ![gr2bgr](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/c/c066382249a76cb4240825f2d3cce3395a8199d7.png)

---

<div class="post-metadata">

**Author:** ![LB91](https://avatars.discourse-cdn.com/v4/letter/l/3da27b/32.png) [@LB91](https://forum.opencv.org/u/LB91)\
**Post date:** [March 30, 2022, 6:10am UTC](https://forum.opencv.org/t/remove-6-bit-from-16-bit-depth-pixels/7938/11 "2022-03-30T06:10:05Z")

</div>

wow so many information, thank you! I still need to learn the language of image processing, but thnaks for these hints I started to work on these parameters. I am eager to learn about this topic I started a book about OpenCV, but now I feel like I also need one about digital photography or something similar. Share with me if you have any recommendatiton.  
Also I created a dummy colorbar image:  
 ![gb2bgr](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/5/518d577bca02843aa3302f9e536fc30b72aecee9.png)

---

<div class="post-metadata">

**Author:** ![LB91](https://avatars.discourse-cdn.com/v4/letter/l/3da27b/32.png) [@LB91](https://forum.opencv.org/u/LB91)\
**Post date:** [April 4, 2022, 2:36pm UTC](https://forum.opencv.org/t/remove-6-bit-from-16-bit-depth-pixels/7938/12 "2022-04-04T14:36:56Z")

</div>

Hello,

Since my last post I worked around with the camera, and find out my pixel processing method was wrong and I lost too many information. And that was the cause of that pale color poor image. But with the reimplemented method I got pictures which are look like have too much gain I guess. In low light the colors are grate, but if I using any kind of light source the picutre saturate immedately.I attached a picture about the phenomena. Now I am working with the sensor gain ceiling and exposure ceiling parameters, but not sure about the reason of the picture error.  
 ![aeccf](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/d/dcffa5973b5fe3fc8a05a484ff3e2e1f3b03a1b0.png)
