17

I want to convert an image to 2D array with 5 columns where each row is of the form [r, g, b, x, y]. x, y is the position of the pixel and r,g,b are the pixel values. (I will be using this array as input to a machine learning model). Is there a more efficient implementation than this in python?

import Image
import numpy as np

im = Image.open("farm.jpg")
col,row =  im.size
data = np.zeros((row*col, 5))
pixels = im.load()
for i in range(row):
    for j in range(col):
        r,g,b =  pixels[i,j]
        data[i*col + j,:] = r,g,b,i,j
Sanket
  • 746
  • 3
  • 13
  • 26

4 Answers4

20

I had to write this recently and ended up with

indices = np.dstack(np.indices(im.shape[:2]))
data = np.concatenate((im, indices), axis=-1)

Where im is a numpy array. You are probably better off reading the images straight into numpy arrays with

from scipy.misc import imread
im = imread("farm.jpg")

Or, better still if you have Scikit Image installed

from skimage.io import imread
im = imread("farm.jpg")
AJP
  • 26,547
  • 23
  • 88
  • 127
YXD
  • 31,741
  • 15
  • 75
  • 115
7

I am not sure if this is the very efficient. But here you go, say arr = np.array(im); then you can do something like this.

>>> arr = np.arange(150).reshape(5, 10, 3)
>>> x, y, z = arr.shape
>>> indices = np.vstack(np.unravel_index(np.arange(x*y), (y, x))).T
#or indices = np.hstack((np.repeat(np.arange(y), x)[:,np.newaxis], np.tile(np.arange(x), y)[:,np.newaxis]))
>>> np.hstack((arr.reshape(x*y, z), indices))
array([[  0,   1,   2,   0,   0],
       [  3,   4,   5,   0,   1],
       [  6,   7,   8,   0,   2],
       [  9,  10,  11,   0,   3],
       [ 12,  13,  14,   0,   4],
       [ 15,  16,  17,   1,   0],
       [ 18,  19,  20,   1,   1],
       [ 21,  22,  23,   1,   2],
       [ 24,  25,  26,   1,   3],
       [ 27,  28,  29,   1,   4],
       [ 30,  31,  32,   2,   0],
       [ 33,  34,  35,   2,   1],
       [ 36,  37,  38,   2,   2],
       ...
       [129, 130, 131,   8,   3],
       [132, 133, 134,   8,   4],
       [135, 136, 137,   9,   0],
       [138, 139, 140,   9,   1],
       [141, 142, 143,   9,   2],
       [144, 145, 146,   9,   3],
       [147, 148, 149,   9,   4]])
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
3

I used "+" to combine two tuple, and use .append() to make "data" list.No need to use Numpy here.

row,col = im.size
data=[] #r,g,b,i,j
pixels=im.load()
for i in range(row):
  for j in range(col):
    data.append(pixels[i,j]+(i,j))
羅一中
  • 31
  • 1
1

steps are :

  1. convert images to grayscale (opencv)

  2. convert grayscale to binary image (opencv)

  3. convert to binary 2D matrix (scipy , pillow, numpy)

from scipy.ndimage import zoom
from PIL import Image
import numpy as np

srcImage = Image.open("image_in_binary_color.jpg")
grayImage = srcImage.convert('L')
array = np.array(grayImage)
array = zoom(array, 310/174)

np.savetxt("binarized.txt", array<128, fmt="%d")
print("\n\n Output Stored to binarized.txt.......#")
  1. store it in a file named binarized.txt

This is how i did it : https://github.com/jithi22/Imagery.git

baileythegreen
  • 1,126
  • 3
  • 16
jp22
  • 11
  • 2