Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Render image from hsv color
QtWS25 Last Chance

Render image from hsv color

Scheduled Pinned Locked Moved Solved General and Desktop
python3qpainterdrawpixmapqwidgetrendering
8 Posts 2 Posters 4.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Adam V
    wrote on 17 Sept 2017, 22:10 last edited by
    #1

    I've looking into a way to render an image on screen from a saturation map.

    I'm currently using a regular widget (because I couldn't get transparency to work with the openGL one).
    My paint even look something like

        painter = QPainter()
        painter.begin(self)
        color = QColor()
        for x, y in np.transpose(np.nonzero(self.im)):
            color.setHsv(168, self.im[x, y], 180)
            painter.setPen(color)
            painter.drawPoint(QPoint(x, y))
        painter.end()
    

    where I iterate over the whole (nonzero) saturation map "self.im", change the QColor to that, update the painter with that color and paint that point.

    But this is terribly inefficient and slow, how can I make this work?
    Is there a way to make a hsv/hsl Image and render that?
    I can layer the (constant) hue and value/saturation to a make a full hw3 numpy array if that helps.

    J 1 Reply Last reply 18 Sept 2017, 07:23
    0
    • A Offline
      A Offline
      Adam V
      wrote on 18 Sept 2017, 09:17 last edited by Adam V
      #8

      I have found a solution that kind of does the job, although takes three times as much memory.
      Every update I calculate the difference between the new and the old saturation matrix, this eliminates 60% of the nonzero values, which means it gets drawn that much faster.

      As a second layer of optimization I call repaint with a rectangle describing the most important zone, and call update every 60ms.

      1 Reply Last reply
      1
      • A Adam V
        17 Sept 2017, 22:10

        I've looking into a way to render an image on screen from a saturation map.

        I'm currently using a regular widget (because I couldn't get transparency to work with the openGL one).
        My paint even look something like

            painter = QPainter()
            painter.begin(self)
            color = QColor()
            for x, y in np.transpose(np.nonzero(self.im)):
                color.setHsv(168, self.im[x, y], 180)
                painter.setPen(color)
                painter.drawPoint(QPoint(x, y))
            painter.end()
        

        where I iterate over the whole (nonzero) saturation map "self.im", change the QColor to that, update the painter with that color and paint that point.

        But this is terribly inefficient and slow, how can I make this work?
        Is there a way to make a hsv/hsl Image and render that?
        I can layer the (constant) hue and value/saturation to a make a full hw3 numpy array if that helps.

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 18 Sept 2017, 07:23 last edited by
        #2

        @Adam-V Maybe it would help to write the pixel data into a QImage first and then draw this QImage once?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        A 1 Reply Last reply 18 Sept 2017, 08:08
        0
        • J jsulm
          18 Sept 2017, 07:23

          @Adam-V Maybe it would help to write the pixel data into a QImage first and then draw this QImage once?

          A Offline
          A Offline
          Adam V
          wrote on 18 Sept 2017, 08:08 last edited by
          #3

          @jsulm How do I construct a QImage from hsv/hsl data?

          J 1 Reply Last reply 18 Sept 2017, 08:09
          0
          • A Adam V
            18 Sept 2017, 08:08

            @jsulm How do I construct a QImage from hsv/hsl data?

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 18 Sept 2017, 08:09 last edited by
            #4

            @Adam-V In the same way you're drawing it now. See http://doc.qt.io/qt-5/qimage.html#setPixel-1

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            A 1 Reply Last reply 18 Sept 2017, 08:40
            1
            • J jsulm
              18 Sept 2017, 08:09

              @Adam-V In the same way you're drawing it now. See http://doc.qt.io/qt-5/qimage.html#setPixel-1

              A Offline
              A Offline
              Adam V
              wrote on 18 Sept 2017, 08:40 last edited by Adam V
              #5

              @jsulm While this does draw faster but still 90ms paintEvent (improved from 180).
              Implementation:

                  image = QImage(self.width(), self.height(), QImage.Format_ARGB32)
                  painter = QPainter()
                  painter.begin(self)
                  color = QColor()
                  for x, y in np.transpose(np.nonzero(self.im)):
                      color.setHsv(168, self.im[x, y], 180)
                      image.setPixelColor(x, y, color)
                  painter.drawImage(self.rect(), image)
                  painter.end()
              
              J 1 Reply Last reply 18 Sept 2017, 08:54
              0
              • A Adam V
                18 Sept 2017, 08:40

                @jsulm While this does draw faster but still 90ms paintEvent (improved from 180).
                Implementation:

                    image = QImage(self.width(), self.height(), QImage.Format_ARGB32)
                    painter = QPainter()
                    painter.begin(self)
                    color = QColor()
                    for x, y in np.transpose(np.nonzero(self.im)):
                        color.setHsv(168, self.im[x, y], 180)
                        image.setPixelColor(x, y, color)
                    painter.drawImage(self.rect(), image)
                    painter.end()
                
                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 18 Sept 2017, 08:54 last edited by
                #6

                @Adam-V Do you have to calculate the image each time paintEvent is called? If not then just do it when needed and just draw already calculated image in peintEvent.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                A 1 Reply Last reply 18 Sept 2017, 08:58
                0
                • J jsulm
                  18 Sept 2017, 08:54

                  @Adam-V Do you have to calculate the image each time paintEvent is called? If not then just do it when needed and just draw already calculated image in peintEvent.

                  A Offline
                  A Offline
                  Adam V
                  wrote on 18 Sept 2017, 08:58 last edited by Adam V
                  #7

                  @jsulm
                  Sadly I have to.
                  It only gets drawn on a touch event, and in that case the whole image needs to be recalculated.
                  Also, the profiler indicates that almost all of the time goes to setHsv and setPixelColor.

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    Adam V
                    wrote on 18 Sept 2017, 09:17 last edited by Adam V
                    #8

                    I have found a solution that kind of does the job, although takes three times as much memory.
                    Every update I calculate the difference between the new and the old saturation matrix, this eliminates 60% of the nonzero values, which means it gets drawn that much faster.

                    As a second layer of optimization I call repaint with a rectangle describing the most important zone, and call update every 60ms.

                    1 Reply Last reply
                    1

                    8/8

                    18 Sept 2017, 09:17

                    • Login

                    • Login or register to search.
                    8 out of 8
                    • First post
                      8/8
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved