7

I am trying to combine two different RGB images into a single 6 channel image (Tiff is best) using nothing but Python.

What I have is an RGB image taken from a normal camera as well as another RGB image that is a normal map based on a SfM reconstruction. The images have identical dimensions and I simply need to overlay one image on the other so that I can run an image classification based on the combined channel information.

I've been looking into using openCV for this, but I'm getting hung up on the documentation. I'm a geologist, not a programmer so my math skills and programming knowledge is mediocre at best.

I've been doing some digging and what I've tried so far is using OpenCV to create an array for each image, then using numpy to concatenate the resulting matrices and using PIL to put them together into an image. Trouble is the image shows both images side by side or on top of one-another rather than as a 6 channel image.

I don't think PIL can do what I need it to do, but I'm not sure how to use the openCV mixChannels function or how to even create a MAT in Python as the Mat::create documentation is entirely in C++.

RGB Image

Normals Image

I've come across another thread on this site, but they weren't really answered either as far as I can tell:

See here

Community
  • 1
  • 1
tpubbsGIS
  • 367
  • 2
  • 4
  • 11

1 Answers1

8

NumPy has you covered.

Your inputs are probably both shape (1200, 900, 3) (check with .shape), and you want (1200, 900, 6) as output - this is asking to concatenate the two arrays along the third axis. Therefore

np.concatenate((im1, im2), axis=2) # axes are 0-indexed, i.e. 0, 1, 2

will do precisely what you want.

nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • Thank-you so much for that. It has gotten me a little bit further along, I was using axis=0 and axis=1. However, now that I have that how do I export that to an image that I can use? PIL won't allow me to use the Image.fromarray() method on it. – tpubbsGIS Apr 06 '17 at 20:18
  • You'll have to find a suitable TIFF library, like https://github.com/pearu/pylibtiff or http://www.lfd.uci.edu/%7Egohlke/code/tifffile.py.html. I don't know which library will work for your case, as I am not so familiar with this aspect of your question. – nneonneo Apr 06 '17 at 20:23
  • Why do you want to save a 6 channel image? It's unlikely that you can view it with any common image viewers. – Quang Hoang Apr 07 '17 at 02:32
  • I am not using normal viewers, I'm using ENVI to run multispectral analysis on it. – tpubbsGIS Apr 08 '17 at 23:24