Search code examples
pythonplotscalecontourqwt

qwt/pyqt custom scale for image plot (pixel to mm conversion)


I've decided to use guiqwt as my main plot library in Python and it works quite well. However, I'm missing a contour plot feature, so I had to work out my own contours in my image plots. That was quite easy by using scikit. Now I have my plot showing the image and the contours on top. Scale unit in x and y-direction is pixel as the image raw data is given per pixel and the calculated contours as well.

My problem is to convert the pixel-scale into e. g. mm-scale without scaling the image. I want to replace the original scale with a scale that represents the measured distances. The distances are available in an array.

In my first attempt I tried to change the AxisScaleDivision by creating a new one and using QwtPlot::setAxisScaleDiv. But that seems to work like a zoom-function as the image is reduced to the new interval.

Here is my code for a small example:

from guiqwt.plot import ImageDialog
from guiqwt.builder import make
from skimage import measure
import numpy as np

data = np.random.rand(80,30)

contours = measure.find_contours(data, 0.1)

win = ImageDialog(edit=False, toolbar=True, wintitle="Contrast test", 
    options=dict(show_contrast=True))

img = make.image(data)

plot = win.get_plot()
plot.add_item(img)

for n, contour in enumerate(contours):
    curve = make.curve(contour[:, 1], contour[:, 0], 'k-')
    plot.add_item(curve)

win.show()

scaleEng = plot.axisScaleEngine(2)
scaleDiv = scaleEng.divideScale(20, 30, 5, 5, 0)

plot.setAxisScaleDiv(2, scaleDiv)
plot.replot()

The syntax is very close to qwt, so I think anybody who is familiar with qwt might be able to help me :)

The image zoom should stay unaltered. Only the axis should be recalculated to a mm-scale and afterwards, of course, adapted when the zoom function is used.


Solution

  • I solved the problem by using a completely different approach. I used the xyimage-function of guiqwt. However, I had to scale my contours too. I missed that the last time, that's why I posted the question.