# Scaling a matrix or bgr values

**URL:** <https://forum.opencv.org/t/scaling-a-matrix-or-bgr-values/7028>\
**Category:** Python\
**Tags:** numpy\
**Created:** [January 7, 2022, 7:59pm UTC](https://forum.opencv.org/t/scaling-a-matrix-or-bgr-values/7028 "2022-01-07T19:59:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bob](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/bob/32/4170_2.png) [@Bob](https://forum.opencv.org/u/Bob)\
**Post date:** [January 7, 2022, 7:59pm UTC](https://forum.opencv.org/t/scaling-a-matrix-or-bgr-values/7028/1 "2022-01-07T19:59:13Z")

</div>

I am trying to multiply every pixel in a frame by a double.  
However each pixel is. bgr and uint8

```auto
def scaleVideo(bgr_dataset, avgs, fit):
    print("Shape of bgr is ", bgr_dataset.shape);
    nframes = bgr_dataset.shape[0]
    #then Width, Height, 3 
    for f in range(0,nframes-1,1):
        print("Scaling frame ", f, "\r")
        cf = fit[f]
        a = avgs[f]
        scale = a / cf
        for y in range(0, bgr_dataset.shape[1],1):
            for x in range(0, bgr_dataset.shape[2], 1):
                b = bgr_dataset[f,y,x,0]
                g = bgr_dataset[f,y,x,1]
                r = bgr_dataset[f,y,x,2]
                b *= scale
                g *= scale
                r *= scale
                bgr_dataset[f,y,x,0] = b
                bgr_dataset[f,y,x,1] = g
                bgr_dataset[f,y,x,2] = r
    return bgr_dataset

```

This routine will take ‘forever’ to run.  
However:

bgr\_dataset[f,:,:,:] \*= scale

Does not work.

How can i scale each pixel value and ensure the scaled value is rounded and bounded?

---

<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 8, 2022, 1:10am UTC](https://forum.opencv.org/t/scaling-a-matrix-or-bgr-values/7028/2 "2022-01-08T01:10:26Z")

</div>

it’s numpy. it’s like matlab. just multiply the entire matrix.

remove the two inner loops.

> [@Bob](#):
>
> bgr\_dataset[f,:,:,:] \*= scale
> 
> Does not work.

explain “does not work”.
