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
Forum Updated to NodeBB v4.3 + New Features

Render image from hsv color

Scheduled Pinned Locked Moved Solved General and Desktop
python3qpainterdrawpixmapqwidgetrendering
8 Posts 2 Posters 4.5k 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 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.

    jsulmJ 1 Reply Last reply
    0
    • A Offline
      A Offline
      Adam V
      wrote on 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

        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.

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on 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
        0
        • jsulmJ jsulm

          @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 last edited by
          #3

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

          jsulmJ 1 Reply Last reply
          0
          • A Adam V

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

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on 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
            1
            • jsulmJ jsulm

              @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 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()
              
              jsulmJ 1 Reply Last reply
              0
              • A Adam V

                @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()
                
                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on 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
                0
                • jsulmJ jsulm

                  @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 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 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

                    • Login

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