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. Convert QImage into binary Matrix

Convert QImage into binary Matrix

Scheduled Pinned Locked Moved Unsolved General and Desktop
qimagematrixconvert
11 Posts 3 Posters 5.9k 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.
  • V Offline
    V Offline
    VRonin
    wrote on 14 Jun 2016, 07:50 last edited by VRonin
    #2
    char **matrix = new char* [image.width()];
    for(int i=0;i<image.width();++i)
    matrix[i] = new char [image.height()];
    for(int i=0;i<image.width();++i){
    for(int j=0;j<image.height();++j)
    matrix[i][j] = QColor(image.pixel(i,j)).black() > 127 ? 0:1;
    }
    

    Remember to delete the matrix to avoid memory leak or use something like QScopedArrayPointer

    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
    ~Napoleon Bonaparte

    On a crusade to banish setIndexWidget() from the holy land of Qt

    A 1 Reply Last reply 14 Jun 2016, 08:09
    2
    • V VRonin
      14 Jun 2016, 07:50
      char **matrix = new char* [image.width()];
      for(int i=0;i<image.width();++i)
      matrix[i] = new char [image.height()];
      for(int i=0;i<image.width();++i){
      for(int j=0;j<image.height();++j)
      matrix[i][j] = QColor(image.pixel(i,j)).black() > 127 ? 0:1;
      }
      

      Remember to delete the matrix to avoid memory leak or use something like QScopedArrayPointer

      A Offline
      A Offline
      AlvaroS
      wrote on 14 Jun 2016, 08:09 last edited by
      #3

      @VRonin Thanks a lot for answering me my friend.

      Now I would like to write the image in a txt.
      i do this:

          QString outputFilename = QFileDialog::getSaveFileName(this, "Guardar TXT", "/home", "files TXT (*.txt)");
          QFile outputFile(outputFilename);
          outputFile.open(QIODevice::WriteOnly);
      
              if(!outputFile.isOpen()) //If the user have choosen a file then:
              {
                 //
              }
      
          /* Point a QTextStream object at the file */
          QTextStream outStream(&outputFile);
          outStream << &matrix;
      

      but it writes just the memory direction, not the matrix... coudl you help me?

      V 1 Reply Last reply 14 Jun 2016, 08:13
      0
      • A AlvaroS
        14 Jun 2016, 08:09

        @VRonin Thanks a lot for answering me my friend.

        Now I would like to write the image in a txt.
        i do this:

            QString outputFilename = QFileDialog::getSaveFileName(this, "Guardar TXT", "/home", "files TXT (*.txt)");
            QFile outputFile(outputFilename);
            outputFile.open(QIODevice::WriteOnly);
        
                if(!outputFile.isOpen()) //If the user have choosen a file then:
                {
                   //
                }
        
            /* Point a QTextStream object at the file */
            QTextStream outStream(&outputFile);
            outStream << &matrix;
        

        but it writes just the memory direction, not the matrix... coudl you help me?

        V Offline
        V Offline
        VRonin
        wrote on 14 Jun 2016, 08:13 last edited by VRonin
        #4

        @AlvaroS The answer requires you to specify the format of the file, I'll use a MS Excel compatible one:

        QTextStream outStream(&outputFile);
        for(int i=0;i<image.width();++i){
        for(int j=0;j<image.height();++j){
        if(j>0)
        outStream << '\t';
        outStream << matrix[i][j];
        }
        outStream << '\n';
        }
        
        

        Remember to delete the matrix!!!

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        A 1 Reply Last reply 14 Jun 2016, 08:15
        0
        • V VRonin
          14 Jun 2016, 08:13

          @AlvaroS The answer requires you to specify the format of the file, I'll use a MS Excel compatible one:

          QTextStream outStream(&outputFile);
          for(int i=0;i<image.width();++i){
          for(int j=0;j<image.height();++j){
          if(j>0)
          outStream << '\t';
          outStream << matrix[i][j];
          }
          outStream << '\n';
          }
          
          

          Remember to delete the matrix!!!

          A Offline
          A Offline
          AlvaroS
          wrote on 14 Jun 2016, 08:15 last edited by
          #5

          @VRonin Thanks a lot again!!

          The formar that I need is binary format

          V J 2 Replies Last reply 14 Jun 2016, 08:30
          0
          • A AlvaroS
            14 Jun 2016, 08:15

            @VRonin Thanks a lot again!!

            The formar that I need is binary format

            V Offline
            V Offline
            VRonin
            wrote on 14 Jun 2016, 08:30 last edited by
            #6

            @AlvaroS That is a bit generic... let's use an example:

            if the image is (b=black, w=white):
            bwb
            wwb

            what would you want to see in the file?

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            0
            • A AlvaroS
              14 Jun 2016, 08:15

              @VRonin Thanks a lot again!!

              The formar that I need is binary format

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 14 Jun 2016, 08:38 last edited by
              #7

              @AlvaroS To add to @VRonin : you could use 1 bit for each pixel and store 8 pixels in one byte. And you need to store the dimensions of the image (for example how many pixel in each row), else you will not know how to read the image later (except all images have the same dimentions).

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

              A 1 Reply Last reply 14 Jun 2016, 08:41
              0
              • J jsulm
                14 Jun 2016, 08:38

                @AlvaroS To add to @VRonin : you could use 1 bit for each pixel and store 8 pixels in one byte. And you need to store the dimensions of the image (for example how many pixel in each row), else you will not know how to read the image later (except all images have the same dimentions).

                A Offline
                A Offline
                AlvaroS
                wrote on 14 Jun 2016, 08:41 last edited by
                #8

                @jsulm yes, that is what I want.

                use 1 bit for each pixel and store 8 pixels in one byte...
                Can I do that just with a for?

                Thanks!

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 14 Jun 2016, 08:48 last edited by
                  #9

                  Well, you can: you can treat matrix as an one-dimentional array.

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

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 14 Jun 2016, 08:56 last edited by jsulm
                    #10

                    Something like (didn't test):

                    for(int i=0;i<(image.width() * image.height()) / 8; ++i)
                        quint8 byte = 0;
                        for (j=0; j < 8; ++j) {
                            byte |= (matrix[i*8 + j] ? 1 : 0)<<(7-j);
                        }
                        // Write byte to file
                    // Don't forget to write (image.width() * image.height()) % 8 last pixels
                    

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

                    1 Reply Last reply
                    1
                    • V Offline
                      V Offline
                      VRonin
                      wrote on 14 Jun 2016, 14:45 last edited by VRonin
                      #11

                      slightly more refined solution using the fact that QImage uses "a matrix" internally

                      QDataStream outStream(&outputFile);
                      outStream << image.height()<< image.width(); /* You need this to recover the image size, if not needed remove*/ 
                      QImage testImage= image.convertToFormat(QImage::Format_Mono,Qt::MonoOnly); /* 1 bit= 1 pixel*/
                      testImage.invertPixels(); /* black is 1 and white is 0 normally, you need the opposite so invert*/
                      const int bytesInWidth  =
                          testImage.width()/8
                          + (testImage.width()%8>0 ? 1:0);
                      /*This is image.width()/8  rounded up */
                      for(int i=0;i<testImage.height();++i)
                                  outStream.writeRawData((const char*)(testImage.constScanLine(i)),bytesInWidth);
                      
                      

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      1 Reply Last reply
                      1

                      11/11

                      14 Jun 2016, 14:45

                      • Login

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