Esempio n. 1
0
    def paint(self, painter):
        ''' Paint Frame'''
        if (self.currentFrame.map(QAbstractVideoBuffer.ReadOnly)):
            oldTransform = painter.transform()

        if (self.surfaceFormat().scanLineDirection() ==
                QVideoSurfaceFormat.BottomToTop):
            painter.scale(1, -1)
            painter.translate(0, -self.widget.height())

        self.image = QImage(self.currentFrame.bits(),
                            self.currentFrame.width(),
                            self.currentFrame.height(),
                            self.currentFrame.bytesPerLine(), self.imageFormat)

        if self.widget._filterSatate.grayColorFilter:
            self.image = filter.GrayFilter(self.image)

        if self.widget._filterSatate.MirroredHFilter:
            self.image = filter.MirrredFilter(self.image)

        if self.widget._filterSatate.monoFilter:
            self.image = filter.MonoFilter(self.image)
        # TODO : Probar en un thread distinto
        if self.widget._filterSatate.edgeDetectionFilter:
            try:
                self.image = filter.EdgeFilter(self.image)
            except Exception:
                None
        # TODO : Probar en un thread distinto
        if self.widget._filterSatate.contrastFilter:
            try:
                self.image = filter.AutoContrastFilter(self.image)
            except Exception:
                None
        if self.widget._filterSatate.invertColorFilter:
            self.image.invertPixels()

        painter.drawImage(self.targetRect, self.image, self.sourceRect)

        if self._interaction.objectTracking and self.widget._isinit:
            frame = convertQImageToMat(self.image)
            # Update tracker
            ok, bbox = self.widget.tracker.update(frame)
            # Draw bounding box
            if ok:
                #                 qgsu.showUserAndLogMessage(
                #                     "bbox : ", str(bbox), level=QGis.Warning)
                painter.setPen(Qt.blue)
                painter.drawRect(
                    QRect(int(bbox[0]), int(bbox[1]), int(bbox[2]),
                          int(bbox[3])))
            else:
                qgsu.showUserAndLogMessage("Tracking failure detected ",
                                           "",
                                           level=QGis.Warning)

        painter.setTransform(oldTransform)
        self.currentFrame.unmap()
        return self.painter
Esempio n. 2
0
    def NDVIFilter(image):
        ''' NDVI Filter '''
        original = convertQImageToMat(image)
        lowerLimit = 5

        # First, make containers
        oldHeight, oldWidth = original[:, :, 0].shape
        ndviImage = np.zeros((oldHeight, oldWidth, 3),
                             np.uint8)  # make a blank RGB image

        red = (original[:, :, 2]).astype('float')
        blue = (original[:, :, 0]).astype('float')

        # Perform NDVI calculation
        summ = red + blue
        summ[
            summ <
            lowerLimit] = lowerLimit  # do some saturation to prevent low intensity noise

        ndvi = (((red - blue) / (summ) + 1) * 127).astype('uint8')  # the index

        redSat = (ndvi - 128) * 2  # red channel
        bluSat = ((255 - ndvi) - 128) * 2  # blue channel
        redSat[ndvi < 128] = 0  # if the NDVI is negative, no red info
        bluSat[ndvi >= 128] = 0  # if the NDVI is positive, no blue info

        # Red Channel
        ndviImage[:, :, 2] = redSat
        # Blue Channel
        ndviImage[:, :, 0] = bluSat
        # Green Channel
        ndviImage[:, :, 1] = 255 - (bluSat + redSat)

        return convertMatToQImage(ndviImage)
Esempio n. 3
0
 def EdgeFilter(image, sigma=0.33):
     ''' Edge Image Filter '''
     gray = convertQImageToMat(image)
     v = np.median(gray)
     lower = int(max(0, (1.0 - sigma) * v))
     upper = int(min(255, (1.0 + sigma) * v))
     canny = Canny(gray, lower, upper)
     return convertMatToQImage(canny)
Esempio n. 4
0
    def mouseReleaseEvent(self, _):
        """
        @type event: QMouseEvent
        @param event:
        @return:
        """
        # Prevent draw on video if not started or finished
        # if self.parent.player.position() == 0:
        #    return

        # Censure Draw Interaction
        if self._interaction.censure:
            geom = self.Censure_RubberBand.geometry()
            self.Censure_RubberBand.hide()
            self.drawCesure.append([geom])

        # Object Tracking Interaction
        if self._interaction.objectTracking:
            geom = self.Tracking_Video_RubberBand.geometry()
            offset = self.surface.videoRect()
            bbox = (
                geom.x() -
                offset.x(),
                geom.y() -
                offset.y(),
                geom.width(),
                geom.height())
            img = self.currentFrame()
            frame = convertQImageToMat(img)
            # Remo rubberband on canvas and video
            self.Tracking_Video_RubberBand.hide()
            self.Track_Canvas_RubberBand.reset()

            self.tracker = TrackerMOSSE_create()
            result = resize(frame, (offset.width(), offset.height()))

            try:
                ok = self.tracker.init(result, bbox)
            except Exception:
                return
            if ok:
                self._isinit = True
                # Get Traker center
                xc = bbox[0] + (geom.width() / 2)
                yc = bbox[1] + (geom.height() / 2)
                p = QPoint(xc, yc)
                Longitude, Latitude, _ = vut.GetPointCommonCoords(
                    p, self.surface)
                # Draw Rubber Band on canvas
                self.Track_Canvas_RubberBand.addPoint(
                    QgsPointXY(Longitude, Latitude))
            else:
                self._isinit = False
Esempio n. 5
0
 def EdgeFilter(image, sigma=0.33):
     """Edge Image Filter
     @type image: QImage
     @param image:
     @return: QImage
     """
     gray = convertQImageToMat(image)
     v = np.median(gray)
     lower = int(max(0, (1.0 - sigma) * v))
     upper = int(min(255, (1.0 + sigma) * v))
     canny = Canny(gray, lower, upper)
     return convertMatToQImage(canny)
Esempio n. 6
0
    def AutoContrastFilter(image):
        ''' Auto Contrast Image Filter '''
        img = convertQImageToMat(image)
        clahe = createCLAHE(clipLimit=4., tileGridSize=(20, 20))
        # convert from BGR to LAB color space
        lab = cvtColor(img, COLOR_BGR2LAB)
        l, a, b = split(lab)  # split on 3 different channels

        l2 = clahe.apply(l)  # apply CLAHE to the L-channel

        lab = merge((l2, a, b))  # merge channels
        invert = cvtColor(lab, COLOR_LAB2BGR)  # convert from LAB to BGR
        return convertMatToQImage(invert)
Esempio n. 7
0
    def mouseReleaseEvent(self, _):
        """
        :type event: QMouseEvent
        :param event:
        :return:
        """
        if self._interaction.censure:
            geom = self.Tracking_RubberBand.geometry()
            self.Censure_RubberBand.hide()
            self.drawCesure.append([geom])

        if self._interaction.objectTracking:
            geom = self.Tracking_RubberBand.geometry()
            bbox = (geom.x(), geom.y(), geom.width(), geom.height())
            frame = convertQImageToMat(self.GetCurrentFrame())
            self.Tracking_RubberBand.hide()
            self.tracker = cv2.TrackerBoosting_create()
            self.tracker.clear()
            ok = self.tracker.init(frame, bbox)
            if ok:
                self._isinit = True
            else:
                self._isinit = False
Esempio n. 8
0
 def EdgeFilter(image):
     ''' Edge Image Filter '''
     gray = convertQImageToMat(VideoFilters.GrayFilter(image))
     canny = Canny(gray, 100, 150)
     return convertMatToQImage(canny)
Esempio n. 9
0
    def paintEvent(self, event):
        """
        @type event: QPaintEvent
        @param event:
        @return:
        """
        if not self.surface.isActive():
            return

        self.painter = QPainter(self)
        self.painter.setRenderHint(QPainter.HighQualityAntialiasing)

        region = event.region()
        self.painter.fillRect(region.boundingRect(), self.brush)  # Background painter color

        try:
            self.surface.paint(self.painter)
            SetImageSize(self.currentFrame().width(),
                         self.currentFrame().height())
        except Exception:
            None

        # Prevent draw on video if not started or finished
        if self.parent.player.position() == 0:
            self.painter.end()
            return

        self.gt = GetGCPGeoTransform()

        # Draw On Video
        draw.drawOnVideo(self.drawPtPos, self.drawLines, self.drawPolygon,
                         self.drawMeasureDistance, self.drawMeasureArea, self.drawCesure, self.painter, self.surface, self.gt)

        # Draw On Video Object tracking test
        if self._interaction.objectTracking and self._isinit:
            frame = convertQImageToMat(self.currentFrame())
            offset = self.surface.videoRect()
            # Update tracker
            result = resize(frame, (offset.width(), offset.height()))
            ok, bbox = self.tracker.update(result)
            # Draw bounding box
            if ok:
                # check negative values
                x = bbox[0] + offset.x()
                y = bbox[1] + offset.y()
                if vut.IsPointOnScreen(x, y, self.surface):
                    self.painter.setPen(self.blue_Pen)
                    self.painter.drawRect(x, y, bbox[2], bbox[3])

                    # Get Track object center
                    xc = x + (bbox[2] / 2)
                    yc = y + (bbox[3] / 2)
                    p = QPoint(xc, yc)
                    Longitude, Latitude, _ = vut.GetPointCommonCoords(
                        p, self.surface)
                    # Draw Rubber Band on canvas
                    self.Track_Canvas_RubberBand.addPoint(QgsPointXY(Longitude, Latitude))

            else:
                self._isinit = False
                del self.tracker

        # Magnifier Glass
        if self._interaction.magnifier and not self.dragPos.isNull():
            draw.drawMagnifierOnVideo(self, self.dragPos, self.currentFrame(), self.painter)

        # Stamp On Video
        if self._interaction.stamp:
            draw.drawStampOnVideo(self, self.painter)

        self.painter.end()
        return