Ask Your Question
2

image processing in sage

asked 2011-01-24 09:24:58 +0100

ottom gravatar image

updated 2011-06-04 00:05:50 +0100

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

edit retag flag offensive close merge delete

4 Answers

Sort by ยป oldest newest most voted
1

answered 2011-06-03 10:42:03 +0100

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())
edit flag offensive delete link more
0

answered 2013-09-29 06:58:51 +0100

updated 2013-10-30 07:28:46 +0100

tmonteil gravatar image

spam removed.

edit remove flag flag offensive (1) delete link more
0

answered 2013-06-19 04:29:25 +0100

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.

edit remove flag flag offensive (1) delete link more
0

answered 2011-06-03 07:44:08 +0100

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?

edit flag offensive delete link more

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: 2011-01-24 09:24:58 +0100

Seen: 3,548 times

Last updated: Oct 30 '13