First time here? Check out the FAQ!

Ask Your Question
2

image processing in sage

asked 14 years ago

ottom gravatar image

updated 13 years ago

Kelvin Li gravatar image

Dear all,

I have problems to call scipy.ndimage.binary_opening. Code, output and error messages in sage are as follows...

SAGE:

from numpy import *
import scipy.ndimage
import matplotlib.image
import matplotlib.pyplot
img=matplotlib.image.imread(DATA+'NbW-STEM.png')
type(img)
print "Image dtype: %s"%(img.dtype)
print "Image size: %6d"%(img.size)
print "Image shape: %3dx%3d"%(img.shape[0],img.shape[1])
print "Max value %1.2f at pixel %6d"%(img.max(),img.argmax())
print "Min value %1.2f at pixel %6d"%(img.min(),img.argmin())
print "Variance: %1.5f\nStandard deviation: %1.5f"%(img.var(),img.std())

OUTPUT:

Image dtype: float32
Image size: 261075
Image shape: 295x295
Max value 1.00 at pixel 226320
Min value 0.00 at pixel  17109
Variance: 0.02580
Standard deviation: 0.16062

SAGE:

BWatoms=img>0.62
BWatoms=scipy.ndimage.binary_opening(BWatoms,structure=ones((2,2)))

ERROR MESSAGE:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>

  File "_sage_input_27.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("QldhdG9tcz1pbWc+MC42MgpCV2F0b21zPXNjaXB5Lm5kaW1hZ2UuYmluYXJ5X29wZW5pbmcoQldhdG9tcyxzdHJ1Y3R1cmU9b25lcygoMiwyKSkp"),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))

  File "", line 1, in <module>

  File "/tmp/tmpaXpo0G/___code___.py", line 4, in <module>
    exec compile(u'BWatoms=scipy.ndimage.binary_opening(BWatoms,structure=ones((_sage_const_2 ,_sage_const_2 )))

  File "", line 1, in <module>

  File "/home/ottom/Documents/sage_build/sage-4.6.1/local/lib/python2.6/site-packages/scipy/ndimage/morphology.py", line 646, in binary_opening
    origin)

  File "/home/ottom/Documents/sage_build/sage-4.6.1/local/lib/python2.6/site-packages/scipy/ndimage/morphology.py", line 394, in binary_erosion
    output, border_value, origin, 0, brute_force)

  File "/home/ottom/Documents/sage_build/sage-4.6.1/local/lib/python2.6/site-packages/scipy/ndimage/morphology.py", line 229, in _binary_erosion
    raise RuntimeError, 'structure rank must equal input rank'

RuntimeError: structure rank must equal input rank

This code was also tested on the sage-online server (alpha), which failed as well. The code worked fine with Enthought 6.2-2 (pylab-mode). Any suggestions are welcome.

All the best

Otto

Preview: (hide)

4 Answers

Sort by » oldest newest most voted
1

answered 13 years ago

staffan gravatar image

To upload an image to reside in the Sages DATA directory for this particular worksheet you just select "upload" from the Data menu. It can be local or on your PC

Here is a correct version which should work when you have uploaded the picture.

import pylab; import numpy

img=pylab.imread(DATA + 'NbW-STEM.jpg')

print "Image dtype: %s"%(img.dtype)
print "Image size: %6d"%(img.size)
print "Image shape: %3dx%3d"%(img.shape[0],img.shape[1])
print "Max value %1.2f at pixel %6d"%(img.max(),img.argmax())
print "Min value %1.2f at pixel %6d"%(img.min(),img.argmin())
print "Variance: %1.5f\nStandard deviation: %1.5f"%(img.var(),img.std())
Preview: (hide)
link
0

answered 11 years ago

updated 11 years ago

tmonteil gravatar image

spam removed.

Preview: (hide)
link
0

answered 11 years ago

first you need to load images to the app and then do image processing . a lot of image libraries vb to be used. thank you for your examples.

Preview: (hide)
link
0

answered 13 years ago

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

Funny I also used this example and I might give some ideas. you have to upload the image to Sage before running the code, so this:

img=matplotlib.image.imread(DATA+'NbW-STEM.png')

fails. when using Numpy and Scipy in Sage try to use

import numpy as np
import scipy as sp

For your own sense and Sage. There are a tremendous amount of libraries so its easier not to get confused when you at least know the top level modeule. Then I got a simple example by getting Lena from scipy. Do you want to have a verbatim solution, which I don't recommend or do you want one which uses the strength of Sage and Python?

Preview: (hide)
link

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 14 years ago

Seen: 3,641 times

Last updated: Oct 30 '13