What is the `imrect` equivalent in Python? - python

I'm looking for a simple Python utility or module that:
I can give it an image, and it pops up a GUI window for the user to draw a box inside the image
after drawing the box, it returns the coordinates of the box
MATLAB users will realize that I am asking for imrect equivalent in Python.
Is there a simple utility out there to do this?

A quick implementation, for any future searchers: https://raw.github.com/cvondrick/pyvision/master/vision/drawer.py

You can try PIL - Python Imaging Library.
For GUI there's a module named tkinter, though there may be some better options too.

Since most people coming to Python from MATLAB will be making use of lots of SciPy, NumPy, and Matplotlib functions, I would think that the RectangleSelector function of Matplotlib would be the closest equivalent to the imrect function in MATLAB. It is probably a little easier to implement than the other examples if you are already making your plots using Matplotlib.
See this example: Widgets example code: rectangle_selector.py
And this page: class matplotlib.widgets.RectangleSelector
Note: Mac users will need to switch renderers to get this example to work, by including the following code:
import matplotlib
matplotlib.use('TkAgg')
Here is that example in action:
Unlike the imrect function in MATLAB, the default behavior is for the box to only stay in place while you are drawing it.

You may use OpenCV's Python implementation! It is fast and is integrated with PyQt 4!
OpenCV Homepage

Related

Attach matplotlib figure.Figure to pyplot

I have a function that returns a matplotlib.figure.Figure object given some parameters and I want to add it to the pyplot object (so that pyplot.show() shows the image).
Is there any way to do this?
Thanks!
Background:
My question may seem counterintuitive, this is why I need to do so. I'm developing a Python package that uses matplotlib to generate plots, so I'm using the OO matplotlib interface.
I'm writing tests (using matplotlib.testing, which lets me compare images with a decorator) to be sure that the functions return the correct plots. The problem is that such package uses the MATLAB-like interface and looks for plots in the pyplot object (using plt.get_fignums()) and then compares with images in files.
I could modify the matplotlib.testing code to suit my needs but using the code as is seems a better option, even though attaching a figure to pyplot seems hacky.
I ended up using another approach to solve my testing problem. Instead of using the matplotlib.testing built-in decorator, I'm using the function that compares the images:
from matplotlib.testing.compare import compare_images
compare_images(expected='expected.png', actual='actual.png', tol=13, in_decorator=True)
That returns None if the test was successful or a Dictionary with some data in case the images differed more than the threshold. I'm going to build a wrapper around that function to make it more testing friendly.

Can I get imshow() like behaviour in a wx.lib.plot.PlotCanvas?

I'm new to wxpython but have been a matplotlib user for some time now. I am developing a wxpython app in which I would like to have a simple frame with a single panel (actually, a wx.lib.plot.PlotCanvas instance). In the panel I want to show some data, which is in the form of a binary array (called imarr), like this one:
The array will always be of shape (64,N), and N can vary between about 400 and 1200 (I can sort out the panel sizing myself).
To do this in matplotlib I would use something like:
fig=figure(figsize=(12,2))
ax=fig.add_subplot(111)
ax.set_ylim(0,63)
ax.set_xlim(0,imarr.shape[0])
ax.set_aspect('equal')
matplotlib.imshow(imarr)
But I'm completely lost in wxpython. I have been looking into wx.lib.plot, but apart from the nice line and marker plotting capabilities, there doesn't seem to be an equivalent to imshow (or pcolor).
One (ugly) solution I have is to use matplotlib as above to create a png image and then use some wx magic to display the image on a panel (please set me straight if this won't be easy).
I'd much prefer to do it from within wxpython, for portability and general tidiness. Any advice on how to display a binary array on a wx.lib.plot.PlotCanvas (or a more generic wx.Panel) would be very useful!
Why don't you combine wxPython and matplotlib together? wx.lib.plot.PlotCanvas has limited functionality comparing to matplotlib. Have a look at an example in this answer and an example from the matplotlib documentation. Sorry that I didn't answer exactly what you are asking for but I think this is the right way to go.

Qt: Curve editor or something similar?

I'm using Qt4.8 via PyQt in a Python application. Everything is working fine :-) But I need to present the user with some kind of curve editor, these widgets commonly found in image editing programs for adjusting contrast or color or stuff like this.
So I basically need a widget, which displays a curve with axes and stuff like that and the user should be able to "drop" points onto the curve and adjust the curve.
I thought I could use matplotlib for this, but as far as I see it doesn't provide any "editing" capability...
I wrote a Qt curve editor with OpenGL widget, you can try it.
http://www.shannap.com/qt-curve-editor-en.html
What you probably want is the qwt toolbox. It's like matplotlib for Qt.
http://qwt.sourceforge.net/
It has all kind of neat funcitonality for plots and graphs. Though you won't get the exact thing you need without creating your own subclasses.
You can do curve editor with matplotlib. See for example this YouTube video: http://www.youtube.com/watch?v=nCv_MhaeFo8
And then check the event handling examples: http://matplotlib.sourceforge.net/examples/event_handling/index.html

Interactive plotting in Python?

Matlab and Mathematica both have features that allow the user to manipulate the contents of a plot using, say, a slider. For example, if you have a set of 3D data, it is easy to make a small program that lets you view 2D slices of a set of 3D data where the user can slide a slider to move through which slice is displayed. Is there anything in python that allows for this sort of capability without tons of effort? For example, is it possible to do such a thing in matplotlib, or something similar? I
Thanks.
My first thought would be Mayavi, which is great for data visualization, especially in 3D. It relies on VTK. It is included in the Enthought flavoured version of Python, together with Chaco for 2D plotting. To get an idea, look at Travis Vaught's nice screencast in Multidimensional Data Visualization in Python - Mixing Chaco and Mayavi.
It also possible to embed basic interactive functionalities (like slider) to Matplotlib, see matplotlib.widgets and the widget examples.
Finally, you can use rpy (or better, rpy2) and benefit from the R interface.
Have you looked at Vtk? http://www.vtk.org/ One of their demos does exactly what you're asking.
In principle, you can do it by help of MathGL. This is cross-platform GPL library for plotting.
For each mouse clicks you can find the x,y,z position in plot or clicked object and adjust (replot) some other information correspondingly. However you should handle mouse clicks by yourself (for example, using PyQt).
Another option is to use python within the SAGE computation interface/environment, which has the #interact command (see here for specific help on this command)

Python Graphing Utility for GUI with Animations

I am trying to create a GUI interface in VB to track... oh, nevermind.
Basically, I want to create a GUI in python to display data, but I am finding that mathplotlib is not suiting my needs. I would like to be able to highlight certain datapoints, have more freedom in the text drawn to the screen, have animations on data movement, and have dropdown menus for data points. From what I have seen, I do not believe that mathplotlib can do these things. What utility can I look into to better suit my needs?
I haven't used it myself but Chaco seems to fit some of your needs. It is more interactive than matplotlib and can be used to make quite interactive applications.
Chaco is a Python plotting application toolkit that facilitates writing plotting applications at all levels of complexity, from simple scripts with hard-coded data to large plotting programs with complex data interrelationships and a multitude of interactive tools. While Chaco generates attractive static plots for publication and presentation, it also works well for interactive data visualization and exploration.
(source: enthought.com)
QGraphicsScene/View from PyQt4 is a fantastic piece of code. Although your description makes me think that some upfront work will be necessary to make things work.
...don 't trust me, I'm biased ;) Get the library here and check the demos.
The equivalent of matplotlib in the PyQt world is PyQwt (matplotlib integrates with PyQt also, but with PyQwt the integration is smoother). Take a look at this comparison between matplotlib and PyQwt:
http://eli.thegreenplace.net/2009/06/05/plotting-in-python-matplotlib-vs-pyqwt/
PyQt + MathGL can do it easily. See this sample.

Categories