# Create a multi-channel image from pixel coordinates extracted from another image

**URL:** <https://forum.opencv.org/t/create-a-multi-channel-image-from-pixel-coordinates-extracted-from-another-image/1112>\
**Category:** Python\
**Tags:** numpy, imgproc, core\
**Created:** [January 25, 2021, 10:58am UTC](https://forum.opencv.org/t/create-a-multi-channel-image-from-pixel-coordinates-extracted-from-another-image/1112 "2021-01-25T10:58:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Razvan](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/razvan/32/423_2.png) [@Razvan](https://forum.opencv.org/u/Razvan)\
**Post date:** [January 25, 2021, 10:58am UTC](https://forum.opencv.org/t/create-a-multi-channel-image-from-pixel-coordinates-extracted-from-another-image/1112/1 "2021-01-25T10:58:16Z")

</div>

I have a list of mask images with the shape of(3100,3100,3),

I want to create a new image with 5 channels ( 3100,3100,5)

form each image to represent the different colours in each mask. I searched for the specific colours in each image to get the pixel coordinates of each colour,

after that I want to insert these pixels into the new 5 channel image:

![Q7val](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/1X/df5f18697b1ac19910a65c7ccfe702dc89168d03.jpeg)

```
# Define the colours

 green= [0,255,0]
 blue=[0, 0, 255]
 yel=[255, 255, 0]
 red=[255, 0, 0]
 white=[255, 255, 255]

  # create the new image with 5 channels
  new_img=np.zeros( ( np.array(mask).shape[0], np.array(mask).shape[1], 5 ) )

 # find each colour coordinate

 new_img[0,0,1]=np.where(np.all(mask==green,axis=2))
 new_img[0,0,2]=np.where(np.all(mask==blue,axis=2))
 new_img[0,0,3]=np.where(np.all(mask==yel,axis=2))
 new_img[0,0,4]=np.where(np.all(mask==red,axis=2))
 new_img[0,0,5]=np.where(np.all(mask==white,axis=2))

```

it shows this error, how can I create this 5 channel image?

```auto
    > TypeError Traceback (most recent call last)
     TypeError: float() argument must be a string or a number, not 'tuple'

     The above exception was the direct cause of the following exception:

     ValueError Traceback (most recent call last)
       <ipython-input-197-5b9c268857b2> in <module>
        19 new_img=np.zeros( ( np.array(mask).shape[0], np.array(mask).shape[1], 5 ) )
          20 
       ---> 21 new_img[0,0,1]=np.where(np.all(mask==benign,axis=2))
         22 new_img[0,0,2]=np.where(np.all(mask==blue,axis=2))
          23 new_img[0,0,3]=np.where(np.all(mask==yel,axis=2))

      ValueError: setting an array element with a sequence.

```

---

<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:** [January 25, 2021, 2:06pm UTC](https://forum.opencv.org/t/create-a-multi-channel-image-from-pixel-coordinates-extracted-from-another-image/1112/2 "2021-01-25T14:06:42Z")

</div>

This is the correct syntax:

```
new_img[:,:,1] = np.all(mask==green,axis=2).astype(np.uint8)

```

(you can omit the `.astype()` if you need only true/false.)

---

<div class="post-metadata">

**Author:** ![Razvan](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/razvan/32/423_2.png) [@Razvan](https://forum.opencv.org/u/Razvan)\
**Post date:** [January 25, 2021, 2:26pm UTC](https://forum.opencv.org/t/create-a-multi-channel-image-from-pixel-coordinates-extracted-from-another-image/1112/3 "2021-01-25T14:26:23Z")

</div>

> [@kbarni](#):
>
> `new_img[:,:,1] = np.all(mask==green,axis=2).astype(np.uint8)`

Thanks very much @kbarni

I tried to use cv2.merge to check whether I get the same image but I’m getting this image ?

```
  new_img[:,:,0] = np.all(mask==benign,axis=2).astype(np.uint8)
  new_img[:,:,1] = np.all(mask==blue,axis=2).astype(np.uint8)
  new_img[:,:,2] = np.all(mask==yel,axis=2).astype(np.uint8)
  new_img[:,:,3] = np.all(mask==red,axis=2).astype(np.uint8)
  new_img[:,:,4] = np.all(mask==white,axis=2).astype(np.uint8)

test_img=cv2.merge((new_img[:,:,3],new_img[:,:,4],new_img[:,:,1]))

plt.imshow(test_img)

```

: ![Capture](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/1X/3a9ebd5c6668bab4e93c694fb5c8fa5bc9a89288.jpeg)

---

<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:** [January 25, 2021, 2:32pm UTC](https://forum.opencv.org/t/create-a-multi-channel-image-from-pixel-coordinates-extracted-from-another-image/1112/4 "2021-01-25T14:32:26Z")

</div>

Yes. You have the red mask (3) as the red channel, the white mask (4) as the green channel and the blue mask (1) as the blue channel. This is the expected behaviour.

---

<div class="post-metadata">

**Author:** ![Razvan](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/razvan/32/423_2.png) [@Razvan](https://forum.opencv.org/u/Razvan)\
**Post date:** [January 25, 2021, 2:55pm UTC](https://forum.opencv.org/t/create-a-multi-channel-image-from-pixel-coordinates-extracted-from-another-image/1112/5 "2021-01-25T14:55:53Z")

</div>

So, there is no way to re-create the original image from the related channels ?

---

<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:** [January 25, 2021, 9:06pm UTC](https://forum.opencv.org/t/create-a-multi-channel-image-from-pixel-coordinates-extracted-from-another-image/1112/6 "2021-01-25T21:06:18Z")

</div>

The original image is a combination of masks of different colors. It makes sense to decompose it to different masks, but why recreate the _exact original_ image?

Anyway, if you really need it, it should be something like (not tested):

```
test_img=np.where(new_img[:,:,3],[255,0,0],[0,0,0])
          +np.where(new_img[:,:,4],[255,255,255],[0,0,0])
          +np.where(new_img[:,:,3],[0,255,0],[0,0,0])

```

This method would set the pixels defined by the third (white) mask to white.
