0


I started messing around with image processing and I want to make an image matrix, vector (one-dimensional array) and reverse each to image again, this is the code (taken from the example of openCV),In addition - how i will normalize the 1d array ? and what happen after i`m normalize this array, I can make it to image after normalizing?

import cv2.cv as cv
import numpy
img=cv.LoadImage("test3.JPG")
mat=cv.GetMat(img)
a = numpy.asarray(mat)
print a

the output is:

 [[[150 150 150]
 [171 171 171]
 [242 242 242]
 ..., 
 [252 252 252]
 [252 252 252]
 [252 252 252]]

 [[151 151 151]
 [170 170 170]
 [244 244 244]
 ..., 
 [252 252 252]
 [252 252 252]
 [252 252 252]]

 [[159 159 159]
 [172 172 172]
 [248 248 248]
  ..., 
 [252 252 252]
 [252 252 252]
 [252 252 252]]

 ..., 
[[251 251 251]
[251 251 251]
[251 251 251]
 ..., 
[249 249 249]
[248 248 248]
[248 248 248]]

what is the meaning of the three dots, its not printing all the values? this specific image is 125X150


Thanks.

EDIT

import cv2.cv as cv
import numpy
import Image
 def normalize(arr):
  for i in range(3):
    minval = arr[...,i].min()
    maxval = arr[...,i].max()

    if minval != maxval:
        arr[...,i] -= minval
        arr[...,i] *= (255.0/(maxval-minval))
  return arr


 img=cv.LoadImage("test3.JPG")
 mat=cv.GetMat(img)
 a = numpy.asarray(mat)
 b = normalize(a)

 print b


with open('1.txt.',"w") as f:
f.write("\n".join(" ".join(map(str, x)) for x in (b)))


 im = Image.fromarray(b)
 im.save("12.jpeg")
Ofir Attia
  • 1,237
  • 4
  • 21
  • 39
  • The three dots is informing you that it is not printing all of the values (as the results is too long). If you want to see all of the values you can a) output to a file or b) "flush" the output (take a look at this link: http://stackoverflow.com/questions/230751/how-to-flush-output-of-python-print) – david Mar 17 '13 at 19:44

1 Answers1

1
def normalize(arr):
    """
    Linear normalization
    http://en.wikipedia.org/wiki/Normalization_%28image_processing%29
    """
    # Do not touch the alpha channel
    for i in range(3):
        minval = arr[...,i].min()
        maxval = arr[...,i].max()
        if minval != maxval:
            arr[...,i] -= minval
            arr[...,i] *= (255.0/(maxval-minval))
    return arr

import numpy as np
import Image

def normalize(arr):
    for i in range(3):
        minval = arr[..., i].min()
        maxval = arr[..., i].max()

        if minval != maxval:
            arr[..., i] -= minval
            arr[..., i] *= (255.0 / (maxval - minval))
    return arr

img = Image.open('orig.jpg').convert('RGBA')
a = np.array(img)
b = normalize(a)

im = Image.fromarray(b)
im.save('output.jpg')

orig.jpg:

enter image description here

Running the script yields output.jpg:

enter image description here

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • just want to see how things work. i`m not really read this. after i`m normlize and revert it back to image, i will get a lower image? – Ofir Attia Mar 17 '13 at 19:59
  • I don't understand your question. What is a "lower image"? In any event, why not just try it and see? – unutbu Mar 17 '13 at 20:30
  • my mistake... after i normlized the image, i tried to revert it to image and i get the error TypeError: Cannot handle this data type. there is another way to revert the normalized image ? i want to see how its affects on the image, the normalization. – Ofir Attia Mar 17 '13 at 20:35
  • Okay, I had mistakenly changed the dtype of the array. I think I fixed that error. Try it now. – unutbu Mar 17 '13 at 20:42
  • Are you using `a = normalize(a)`? – unutbu Mar 17 '13 at 20:54
  • I've added to my post a runnable program with an example image (`orig.jpg`) and the output (`output.jpg`). – unutbu Mar 17 '13 at 21:17
  • do you know what is the connect between normalizing images and face recognition? – Ofir Attia Mar 17 '13 at 21:33
  • I don't know much about face recognition, but [this post](http://stackoverflow.com/a/4979278/190597) seems to explain it. Note that histogram equalization is not the same as the linear normalization I implemented above. – unutbu Mar 17 '13 at 21:45