混合pyqt和qtcreator (2): Impl a image viewer (can show FIji ROI manager data)

# -*- coding: utf-8 -*-
"""
Simple demonstration of TreeWidget, which is an extension of QTreeWidget
that allows widgets to be added and dragged within the tree more easily.
"""
import initExample  ## Add path to library (just for examples; you do not need this)

import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui, uic
import numpy as np
import sys
import cv2
from read_roi import read_roi_zip
import os

pg.setConfigOption('imageAxisOrder','row-major')

def rand(n):
    data = np.random.random(n)
    data[int(n * 0.1):int(n * 0.13)] += .5
    data[int(n * 0.18)] += 2
    data[int(n * 0.1):int(n * 0.13)] *= 5
    data[int(n * 0.18)] *= 20
    data *= 1e-12
    return data, np.arange(n, n + len(data)) / float(n)

def plotRectData(x,y,w,h):
    return {'x':[x,x,x+w,x+w,x], 'y':[y, y+h, y+h, y,y]}

class MyWindow(QtGui.QMainWindow):
    def onListItemChanged(self, item):
        print (item.text())

        buff = item.text()
        roizip = buff.split('.')[0] + '.zip'
        rois={}
        if os.path.isfile(roizip):
            rois = read_roi_zip(roizip)

        if 0:
            pw = pg.PlotWidget(name='Plot1')
            self.horizontalLayout.addWidget(pw)
            p1 = pw.plot()
            p1.setPen((200, 200, 100))

            yd, xd = rand(10000)
            p1.setData(y=yd, x=xd)
        elif 0:
            imv = pg.ImageView()
            self.horizontalLayout.addWidget(imv)

            im = cv2.imread("buffer00.bmp")
            imv.setImage(im)
        else:

            plot = self.plot
            plot.clear()

            imagedata = cv2.imread(buff)
           # imagedata2 = np.swapaxes(imagedata,0,1)
            imagedata2 = imagedata
           # imagedata2 = np.fliplr(np.rot90(imagedata, 1))
            ii = pg.ImageItem(imagedata2)
            plot.addItem(ii)

            #dsts =plotRectData(100,200,300,100)
            #plot.plot(dsts['x'],dsts['y'], pen=(255,0,0), name="Red curve")

            for k in list(rois.keys()):
                roi = rois[k]
                if roi['type']=='rectangle':
                    [x,y,w,h]=[int(t)for t in [roi['left'], roi['top'], roi['width'], roi['height']]]
                    dsts = plotRectData(x,y,w,h)
                    plot.plot(dsts['x'], dsts['y'], pen=(255, 0, 0))

                    text = pg.TextItem(k)
                    plot.addItem(text)
                    text.setPos(x,y)
                elif roi['type']=='polygon':
                    y = [int(t)for t in roi['y']]
                    x = [int(t) for t in roi['x']]
                    y.append(y[0])
                    x.append(x[0])

                    plot.plot(x, y, pen=(255, 0, 0))

                    text = pg.TextItem(k)
                    plot.addItem(text)
                    text.setPos(x[0], y[0])

            #plot.hideAxis('left')
            #plot.hideAxis('bottom')

    def __init__(self):
        super(MyWindow, self).__init__()
        uic.loadUi('mywindow.ui', self)

        self.buffers=['buffer00.bmp','buffer01.bmp','buffer02.bmp']
        self.actionLoad_Image.triggered.connect(self.myactionLoad_Image)
        m_listeWidget = self.listWidget
        m_listeWidget.setViewMode(m_listeWidget.IconMode)

        m_listeWidget.setIconSize(QtCore.QSize(200, 200))

        m_listeWidget.setResizeMode(QtGui.QListWidget.Adjust)

        for buff in self.buffers:
            m_listeWidget.addItem(QtGui.QListWidgetItem(QtGui.QIcon(buff), buff))
        m_listeWidget.itemClicked.connect(self.onListItemChanged)

        win = pg.GraphicsWindow()
        self.horizontalLayout.addWidget(win)

        self.plot = win.addPlot()

        self.show()

    def myactionLoad_Image(self):
        print ('hello')

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    window = MyWindow()
    sys.exit(app.exec_())

 


posted on 2018-04-17 00:31  cutepig  阅读(455)  评论(0编辑  收藏  举报

导航