Пример #1
0
    def update_image_and_drawings(self,
                                  id_val,
                                  image,
                                  format=None,
                                  points=None,
                                  linesegs=None,
                                  point_colors=None,
                                  point_radii=None,
                                  lineseg_colors=None,
                                  lineseg_widths=None,
                                  xoffset=0,
                                  yoffset=0,
                                  doresize=None):
        """update the displayed image

        **Arguments**

        id_val : string
            An identifier for the particular source being updated
        image : numpy array
            The image data to update

        **Optional keyword arguments**

        format : string
            The image format (e.g. 'MONO8', 'RGB8', or 'YUV422')
        points : list of points
            Points to display (e.g. [(x0,y0),(x1,y1)])
        linesegs : list of line segments
            Line segments to display (e.g. [(x0,y0,x1,y1),(x1,y1,x2,y2)])
        """

        # create bitmap, don't paint on screen
        if points is None:
            points = []
        if linesegs is None:
            linesegs = []
        if format is None:
            format='MONO8'
            warnings.warn('format unspecified - assuming MONO8')

        # if doresize is not input, then use the default value
        if doresize is None:
            doresize = self.doresize

        rgb8 = imops.to_rgb8(format,image)

        if doresize:
            from scipy.misc.pilutil import imresize

            # how much should we resize the image
            windowwidth = self.GetRect().GetWidth()
            windowheight = self.GetRect().GetHeight()
            imagewidth = rgb8.shape[1]
            imageheight = rgb8.shape[0]
            resizew = float(windowwidth) / float(imagewidth)
            resizeh = float(windowheight) / float(imageheight)
            self.resize = min(resizew,resizeh)
            # resize the image
            rgb8 = imresize(rgb8,self.resize)
            # scale all the points and lines
            pointscp = []
            for pt in points:
                pointscp.append([pt[0]*self.resize,pt[1]*self.resize])
            points = pointscp
            linesegscp = []
            for line in linesegs:
                linesegscp.append([line[0]*self.resize,line[1]*self.resize,line[2]*self.resize,line[3]*self.resize])
            linesegs = linesegscp

        if self.id_val is None:
            self.id_val = id_val
        if id_val != self.id_val:
            raise NotImplementedError("only 1 image source currently supported")

        h,w,three = rgb8.shape
        # get full image
        if self.full_image_numpy is not None:
            full_h, full_w, tmp = self.full_image_numpy.shape
            if h<full_h or w<full_w:
                self.full_image_numpy[yoffset:yoffset+h,xoffset:xoffset+w,:] = rgb8
                rgb8 = self.full_image_numpy
                h,w = full_h, full_w
        else:
            self.full_image_numpy = rgb8

        image = wx.EmptyImage(w,h)

        # XXX TODO could eliminate data copy here?
        image.SetData( rgb8.tostring() )
        bmp = wx.BitmapFromImage(image)

        # now draw into bmp

        drawDC = wx.MemoryDC()
        #assert drawDC.Ok(), "drawDC not OK"
        drawDC.SelectObject( bmp ) # draw into bmp
        drawDC.SetBrush(wx.Brush(wx.Colour(255,255,255), wx.TRANSPARENT))

        if self.do_draw_points and points is not None and len(points) > 0:
            if point_radii is None:
                point_radii = [ 8 ] * len(points)
            if point_colors is None:
                point_colors = [ (0,1,0) ]*len(points)
        if self.do_draw_points and linesegs is not None and len(linesegs) > 0:
            if lineseg_widths is None:
                lineseg_widths = [ 1 ] * len(linesegs)
            if lineseg_colors is None:
                lineseg_colors = [ (0,1,0) ]*len(linesegs)

        # fixing drawing point colors!!!
        if self.do_draw_points:
            for i in range(len(points)):

                # point
                pt = points[i]

                # point color
                ptcolor = point_colors[i]
                wxptcolor = wx.Colour(round(ptcolor[0]*255),
                                      round(ptcolor[1]*255),
                                      round(ptcolor[2]*255))

                # radius of point
                ptradius = point_radii[i]

                # draw it
                drawDC.SetPen(wx.Pen(colour=wxptcolor,
                                     width=1))
                drawDC.DrawCircle(int(pt[0]),int(pt[1]),ptradius)

            for i in range(len(linesegs)):
                lineseg = linesegs[i]
                linesegcolor = lineseg_colors[i]
                wxlinesegcolor = wx.Colour(round(linesegcolor[0]*255),
                                           round(linesegcolor[1]*255),
                                           round(linesegcolor[2]*255))
                linesegwidth = lineseg_widths[i]

                drawDC.SetPen(wx.Pen(colour=wxlinesegcolor,
                                     width=linesegwidth))
                if len(lineseg)<=4:
                    drawDC.DrawLine(*lineseg)
                else:
                    for start_idx in range(0, len(lineseg)-3, 2):
                        this_seg = lineseg[start_idx:start_idx+4]
                        drawDC.DrawLine(*this_seg)

        if id_val in self.lbrt:
            drawDC.SetPen(wx.Pen('GREEN',width=1))
            l,b,r,t = self.lbrt[id_val]
            drawDC.DrawLine(l,b, r,b)
            drawDC.DrawLine(r,b, r,t)
            drawDC.DrawLine(r,t, l,t)
            drawDC.DrawLine(l,t, l,b)

        img = wx.ImageFromBitmap(bmp)
        if self.mirror_display:
            if not self.display_rotate_180:
                img = img.Rotate90()
                img = img.Rotate90()
        else:
            img = img.Mirror(True)
            if not self.display_rotate_180:
                img = img.Rotate90()
                img = img.Rotate90()
        bmp = wx.BitmapFromImage(img)

        self.bitmap = bmp
Пример #2
0
    def update_image_and_drawings(
        self,
        id_val,
        image,
        format=None,
        points=None,
        linesegs=None,
        point_colors=None,
        point_radii=None,
        lineseg_colors=None,
        lineseg_widths=None,
        xoffset=0,
        yoffset=0,
        doresize=None,
    ):

        # create bitmap, don't paint on screen
        if points is None:
            points = []
        if linesegs is None:
            linesegs = []
        if format is None:
            raise ValueError("must specify format")

        # if doresize is not input, then use the default value
        if doresize is None:
            doresize = self.doresize

        rgb8 = imops.to_rgb8(format, image)

        if doresize:
            # how much should we resize the image
            windowwidth = self.GetRect().GetWidth()
            windowheight = self.GetRect().GetHeight()
            imagewidth = rgb8.shape[1]
            imageheight = rgb8.shape[0]
            resizew = float(windowwidth) / float(imagewidth)
            resizeh = float(windowheight) / float(imageheight)
            self.resize = min(resizew, resizeh)
            # resize the image
            rgb8 = imresize(rgb8, self.resize)
            # scale all the points and lines
            pointscp = []
            for pt in points:
                pointscp.append([pt[0] * self.resize, pt[1] * self.resize])
            points = pointscp
            linesegscp = []
            for line in linesegs:
                linesegscp.append(
                    [line[0] * self.resize, line[1] * self.resize, line[2] * self.resize, line[3] * self.resize]
                )
            linesegs = linesegscp

        if self.id_val is None:
            self.id_val = id_val
        if id_val != self.id_val:
            raise NotImplementedError("only 1 image source currently supported")

        h, w, three = rgb8.shape
        # get full image
        if self.full_image_numpy is not None:
            full_h, full_w, tmp = self.full_image_numpy.shape
            if h < full_h or w < full_w:
                self.full_image_numpy[yoffset : yoffset + h, xoffset : xoffset + w, :] = rgb8
                rgb8 = self.full_image_numpy
                h, w = full_h, full_w
        else:
            self.full_image_numpy = rgb8

        image = wx.EmptyImage(w, h)

        # XXX TODO could eliminate data copy here?
        image.SetData(rgb8.tostring())
        bmp = wx.BitmapFromImage(image)

        # now draw into bmp

        drawDC = wx.MemoryDC()
        # assert drawDC.Ok(), "drawDC not OK"
        drawDC.SelectObject(bmp)  # draw into bmp
        drawDC.SetBrush(wx.Brush(wx.Colour(255, 255, 255), wx.TRANSPARENT))

        if self.do_draw_points and points is not None and len(points) > 0:
            if point_radii is None:
                point_radii = [8] * len(points)
            if point_colors is None:
                point_colors = [(0, 1, 0)] * len(points)
        if self.do_draw_points and linesegs is not None and len(linesegs) > 0:
            if lineseg_widths is None:
                lineseg_widths = [1] * len(linesegs)
            if lineseg_colors is None:
                lineseg_colors = [(0, 1, 0)] * len(linesegs)

        # point_radius=8
        # fixing drawing point colors!!!
        if self.do_draw_points:
            for i in range(len(points)):

                # point
                pt = points[i]

                # point color
                ptcolor = point_colors[i]
                wxptcolor = wx.Colour(round(ptcolor[0] * 255), round(ptcolor[1] * 255), round(ptcolor[2] * 255))

                # radius of point
                ptradius = point_radii[i]

                # draw it
                drawDC.SetPen(wx.Pen(colour=wxptcolor, width=ptradius))
                drawDC.DrawCircle(int(pt[0]), int(pt[1]), ptradius)

            for i in range(len(linesegs)):
                lineseg = linesegs[i]
                linesegcolor = lineseg_colors[i]
                wxlinesegcolor = wx.Colour(
                    round(linesegcolor[0] * 255), round(linesegcolor[1] * 255), round(linesegcolor[2] * 255)
                )
                linesegwidth = lineseg_widths[i]

                drawDC.SetPen(wx.Pen(colour=wxlinesegcolor, width=linesegwidth))
                drawDC.DrawLine(*lineseg)

        if id_val in self.lbrt:
            drawDC.SetPen(wx.Pen("GREEN", width=1))
            l, b, r, t = self.lbrt[id_val]
            drawDC.DrawLine(l, b, r, b)
            drawDC.DrawLine(r, b, r, t)
            drawDC.DrawLine(r, t, l, t)
            drawDC.DrawLine(l, t, l, b)

        img = wx.ImageFromBitmap(bmp)
        if self.mirror_display:
            if not self.display_rotate_180:
                img = img.Rotate90()
                img = img.Rotate90()
        else:
            img = img.Mirror(True)
            if not self.display_rotate_180:
                img = img.Rotate90()
                img = img.Rotate90()
        bmp = wx.BitmapFromImage(img)

        self.bitmap = bmp