跳到內容
  • 版面
  • 最新
  • 標籤
  • 熱門
  • 使用者
  • 群組
  • 搜尋
  • Get Qt Extensions
  • Unsolved
Collapse
品牌標誌
  1. 首頁
  2. Qt Development
  3. General and Desktop
  4. Picture
Forum Updated to NodeBB v4.3 + New Features

Picture

已排程 已置頂 已鎖定 已移動 Solved General and Desktop
82 貼文 7 Posters 46.4k 瀏覽 3 Watching
  • 從舊到新
  • 從新到舊
  • 最多點贊
回覆
  • 在新貼文中回覆
登入後回覆
此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
  • VRoninV 離線
    VRoninV 離線
    VRonin
    寫於 最後由 VRonin 編輯
    #28

    These 2 should help you

    QColor dominantColour(const QImage& image,const QPoint& topLeft, const QSize& rectSize){
    const int maxRight = qMin(image.width(),topLeft.x() + rectSize.width());
    const int maxBottom = qMin(image.height(),topLeft.y() + rectSize.height());
    qint64 sumRed = 0;
    qint64 sumGreen = 0;
    qint64 sumBlue = 0;
    for(int x=topLeft.x();x<maxRight;++x){
    for(int y=topLeft.y();y<maxBottom;++y){
    const QColor tempColor=image.pixelColor(x,y);
    sumRed += tempColor.red();
    sumGreen += tempColor.green();
    sumBlue += tempColor.blue();
    }
    }
    if(sumRed >= sumGreen  && sumRed >= sumBlue)
    return Qt::red;
    if(sumGreen  >= sumBlue)
    return Qt::green;
    return Qt::blue;
    }
    
    void fillSection(QImage& image,const QPoint& topLeft, const QSize& rectSize, const QColor& colour){
    const int maxRight = qMin(image.width(),topLeft.x() + rectSize.width());
    const int maxBottom = qMin(image.height(),topLeft.y() + rectSize.height());
    for(int x=topLeft.x();x<maxRight;++x){
    for(int y=topLeft.y();y<maxBottom;++y){
    image.setPixelColor(x,y,colour);
    }}
    }
    

    @Payx said in Picture:

    i use a Qpixmap to display my picture, so i cant QImage pix; (the name of my picture is pix)

    http://doc.qt.io/qt-5/qimage.html#details

    Qt provides four classes for handling image data: QImage, QPixmap, QBitmap and QPicture. QImage is designed and optimized for I/O, and for direct pixel access and manipulation, while QPixmap is designed and optimized for showing images on screen. QBitmap is only a convenience class that inherits QPixmap, ensuring a depth of 1. Finally, the QPicture class is a paint device that records and replays QPainter commands.

    Use QImage for pixel manipulation. you can use QImage::fromPixmap() and QPixmap::fromImage() to convert between the two

    "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

    P 1 條回覆 最後回覆
    2
    • VRoninV VRonin

      These 2 should help you

      QColor dominantColour(const QImage& image,const QPoint& topLeft, const QSize& rectSize){
      const int maxRight = qMin(image.width(),topLeft.x() + rectSize.width());
      const int maxBottom = qMin(image.height(),topLeft.y() + rectSize.height());
      qint64 sumRed = 0;
      qint64 sumGreen = 0;
      qint64 sumBlue = 0;
      for(int x=topLeft.x();x<maxRight;++x){
      for(int y=topLeft.y();y<maxBottom;++y){
      const QColor tempColor=image.pixelColor(x,y);
      sumRed += tempColor.red();
      sumGreen += tempColor.green();
      sumBlue += tempColor.blue();
      }
      }
      if(sumRed >= sumGreen  && sumRed >= sumBlue)
      return Qt::red;
      if(sumGreen  >= sumBlue)
      return Qt::green;
      return Qt::blue;
      }
      
      void fillSection(QImage& image,const QPoint& topLeft, const QSize& rectSize, const QColor& colour){
      const int maxRight = qMin(image.width(),topLeft.x() + rectSize.width());
      const int maxBottom = qMin(image.height(),topLeft.y() + rectSize.height());
      for(int x=topLeft.x();x<maxRight;++x){
      for(int y=topLeft.y();y<maxBottom;++y){
      image.setPixelColor(x,y,colour);
      }}
      }
      

      @Payx said in Picture:

      i use a Qpixmap to display my picture, so i cant QImage pix; (the name of my picture is pix)

      http://doc.qt.io/qt-5/qimage.html#details

      Qt provides four classes for handling image data: QImage, QPixmap, QBitmap and QPicture. QImage is designed and optimized for I/O, and for direct pixel access and manipulation, while QPixmap is designed and optimized for showing images on screen. QBitmap is only a convenience class that inherits QPixmap, ensuring a depth of 1. Finally, the QPicture class is a paint device that records and replays QPainter commands.

      Use QImage for pixel manipulation. you can use QImage::fromPixmap() and QPixmap::fromImage() to convert between the two

      P 離線
      P 離線
      Payx
      寫於 最後由 編輯
      #29

      @VRonin Thanks for your answer, but thats very too complicated for me, and anyway i don't understand.

      Can you explain your code ?

      I tried something instead :

      void MainWindow::on_push2_clicked()
      {
      
      	QRgb pix(int x, int z);
      	int step=4;
      	for (int i=0;i<pixi.width()+1;i=i+step){
      		for (int j=0;j<pixi.height()+1;j=j+step){
      			for(int x=0; x<i;x=x+1){
      				for (int z=0; z<j; z=z+1){
      					QRgb pixa = pixi.pixel(x, z);
      
      the problem is here, how can i stock all the color of my square to after that compare them to know what is the dominant color
      
      
      			}
      	}
      }
      	}
      }
      
      mrjjM 1 條回覆 最後回覆
      0
      • VRoninV 離線
        VRoninV 離線
        VRonin
        寫於 最後由 編輯
        #30

        I simplified the code a bit.

        The first function takes an image, the coordinates of the top left corner and the size of the rectangle (4x4 in your case) and tells you what is the dominant primary colour there

        The second one takes an image, the coordinates of the top left corner and the size of the rectangle (4x4 in your case) and fills that rectangle with the colour in the argument

        "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 條回覆 最後回覆
        3
        • P Payx

          @VRonin Thanks for your answer, but thats very too complicated for me, and anyway i don't understand.

          Can you explain your code ?

          I tried something instead :

          void MainWindow::on_push2_clicked()
          {
          
          	QRgb pix(int x, int z);
          	int step=4;
          	for (int i=0;i<pixi.width()+1;i=i+step){
          		for (int j=0;j<pixi.height()+1;j=j+step){
          			for(int x=0; x<i;x=x+1){
          				for (int z=0; z<j; z=z+1){
          					QRgb pixa = pixi.pixel(x, z);
          
          the problem is here, how can i stock all the color of my square to after that compare them to know what is the dominant color
          
          
          			}
          	}
          }
          	}
          }
          
          mrjjM 離線
          mrjjM 離線
          mrjj
          Lifetime Qt Champion
          寫於 最後由 mrjj 編輯
          #31

          @Payx
          Hi, i added some comments. Its basically the same as what you tried , just broken up into good functions to produce clean code.

          // this function , you give the image and the start and size of the current rect you are processing
          QColor dominantColour(const QImage& image, const QPoint& topLeft, const QSize& rectSize) {
          // calculate start stop values for the rect
             const int maxRight = qMin(image.width(), topLeft.x() + rectSize.width());
            const int maxBottom = qMin(image.height(), topLeft.y() + rectSize.height());
            QVector<QColor> coloursList; // this is a list of all colors seen!
            for(int x = topLeft.x(); x < maxRight; ++x) {
              for(int y = topLeft.y(); x < maxBottom; ++x) {
                coloursList << image.pixelColor(x, y); // store all colors seen in this Rect
              }
            }
          
            // these are functions that count (accumulate) how many times RED, GREEN and blue been used ( using the list from before)
            const qint64 sumRed = std::accumulate(coloursList.constBegin(), coloursList.constEnd(), 0, [](qint64 strt,  const QColor & val)->qint64 {return strt + val.red()});
            const qint64 sumGreen = std::accumulate(coloursList.constBegin(), coloursList.constEnd(), 0, [](qint64 strt, const QColor & val)->qint64 {return strt + val.green()});
            const qint64 sumBlue = std::accumulate(coloursList.constBegin(), coloursList.constEnd(), 0, [](qint64 strt, const QColor & val)->qint64 {return strt + val.blue()});
            // now we check out which is most used
            if(sumRed >= sumGreen  && sumRed >= sumBlue)
              return Qt::red;
            if(sumGreen  >= sumBlue)
              return Qt::green;
            return Qt::blue;
          }
          
          // this function can fill an rect with a color. So its used with dominantColour to actually replace the colors
          void fillSection(QImage& image, const QPoint& topLeft, const QSize& rectSize, const QColor& colour) {
            // calculate start stop values for the rect
            const int maxRight = qMin(image.width(), topLeft.x() + rectSize.width());
            const int maxBottom = qMin(image.height(), topLeft.y() + rectSize.height());
            // go over all pixels in the rect
            for(int x = topLeft.x(); x < maxRight; ++x) {
              for(int y = topLeft.y(); x < maxBottom; ++x) {
                image.setPixelColor(x, y, colour); // replace color
              }
            }
          }
          
          
          1 條回覆 最後回覆
          3
          • P 離線
            P 離線
            Payx
            寫於 最後由 編輯
            #32

            Thanks for your help,

            I got one error

            undefined reference to 'MainWindow::dominantColor()'

            1 條回覆 最後回覆
            0
            • VRoninV 離線
              VRoninV 離線
              VRonin
              寫於 最後由 VRonin 編輯
              #33

              colour vs color

              "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 條回覆 最後回覆
              1
              • P 離線
                P 離線
                Payx
                寫於 最後由 編輯
                #34

                thats wasnt a simple letter that occured an error,

                undefined reference to 'MainWindow::dominantColour()'
                undefined reference to ''MainWindow::fillSection()'

                1 條回覆 最後回覆
                0
                • SGaistS 離線
                  SGaistS 離線
                  SGaist
                  Lifetime Qt Champion
                  寫於 最後由 編輯
                  #35

                  Because they are not part of MainWindow. Take a look again a the function signature.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  P 1 條回覆 最後回覆
                  1
                  • SGaistS SGaist

                    Because they are not part of MainWindow. Take a look again a the function signature.

                    P 離線
                    P 離線
                    Payx
                    寫於 最後由 編輯
                    #36

                    @SGaist

                    Here my window.h

                    private slots:
                    	void on_push_clicked();
                    	QColor	dominantColour();
                    	void fillSection();
                    
                    	void on_push2_clicked();
                    

                    You said my function is not part of MainWindow, so i try to add MainWindow::

                    void MainWindow::fillSection(QImage& pixi, const QPoint& topLeft, const QSize& rectSize, const QColor& colour) {
                    

                    and its said prototype for void Main...... does not match any in class 'MainWindow'

                    kshegunovK 1 條回覆 最後回覆
                    0
                    • SGaistS 離線
                      SGaistS 離線
                      SGaist
                      Lifetime Qt Champion
                      寫於 最後由 編輯
                      #37

                      Method signature mismatch.

                      Note: neither dominantColour nor fillSection make sense in the slots category.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 條回覆 最後回覆
                      2
                      • P Payx

                        @SGaist

                        Here my window.h

                        private slots:
                        	void on_push_clicked();
                        	QColor	dominantColour();
                        	void fillSection();
                        
                        	void on_push2_clicked();
                        

                        You said my function is not part of MainWindow, so i try to add MainWindow::

                        void MainWindow::fillSection(QImage& pixi, const QPoint& topLeft, const QSize& rectSize, const QColor& colour) {
                        

                        and its said prototype for void Main...... does not match any in class 'MainWindow'

                        kshegunovK 離線
                        kshegunovK 離線
                        kshegunov
                        Moderators
                        寫於 最後由 編輯
                        #38

                        @Payx said in Picture:

                        its said prototype for void Main...... does not match any in class 'MainWindow'

                        They don't match, the compiler's correct.

                        class MainWindow
                        {
                            // ...
                            void fillSection();
                            // ...
                        };
                        

                        in the header has different prototype from :

                        void MainWindow::fillSection(QImage& pixi, const QPoint& topLeft, const QSize& rectSize, const QColor& colour)
                        {
                            // ...
                        

                        in the source file. They must match, it's a requirement of the language.

                        Read and abide by the Qt Code of Conduct

                        1 條回覆 最後回覆
                        0
                        • P 離線
                          P 離線
                          Payx
                          寫於 最後由 編輯
                          #39

                          So where am i suppose to declare my functions ?

                          jsulmJ 1 條回覆 最後回覆
                          0
                          • P Payx

                            So where am i suppose to declare my functions ?

                            jsulmJ 離線
                            jsulmJ 離線
                            jsulm
                            Lifetime Qt Champion
                            寫於 最後由 編輯
                            #40

                            @Payx Again:

                            void fillSection()
                            

                            is NOT the same as

                            void fillSection(QImage& pixi, const QPoint& topLeft, const QSize& rectSize, const QColor& colour)
                            

                            So, either change void fillSection() to void fillSection(QImage& pixi, const QPoint& topLeft, const QSize& rectSize, const QColor& colour), or other way around. This is really basic C++.

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

                            1 條回覆 最後回覆
                            1
                            • Pradeep KumarP 離線
                              Pradeep KumarP 離線
                              Pradeep Kumar
                              寫於 最後由 Pradeep Kumar 編輯
                              #41

                              Hi,

                              1. Function arguments must match otherwise u will get error.
                              2. Declare your functions in Header File, provide the definition in source file.
                              3. Use slots only when u want to get the values when signal is emitted providing the connect(); . Otherwise declare in any one of below specifiers.
                              4. add function in either of the access specifier depending upon your need

                              private:
                              void fillSection();

                              public:
                              void fillSection();

                              protected:
                              void fillSection();

                              The arguments which provided in declaration should match with the definition along with data type.

                              Thanks,

                              Pradeep Kumar
                              Qt,QML Developer

                              1 條回覆 最後回覆
                              1
                              • P 離線
                                P 離線
                                Payx
                                寫於 最後由 Payx 編輯
                                #42

                                Thanks both of you, it works !
                                The problem i think is that i don't understand english very well, that's my problem.

                                So if i understood well,

                                i will have :

                                void MainWindow::on_push2_clicked()
                                {
                                
                                	QColor dominantColor(const QImage& pixi, const QPoint& topLeft, const QSize& rectSize);
                                	void fillSection(QImage& pixi, const QPoint& topLeft, const QSize& rectSize, const QColor& colour);
                                
                                }
                                

                                I check what the code do, if i'm right i have to define the
                                rectSize.height()
                                and rectSize.width()

                                and last question
                                when i have

                                for(int x = topLeft.x(); x < maxRight; ++x) {
                                		for(int y = topLeft.y(); x < maxBottom; ++x) {
                                

                                in the second line, it's not 'y' the variable who change?

                                and

                                const int maxRight = qMin(pixi.width(), topLeft.x() + rectSize.width());
                                for(int x = topLeft.x(); x < maxRight; ++x) {
                                

                                if we define rectSize (4*4) the for will go from 0 to 4 only no ? we need to change the rectsize at the end of the for no ?

                                VRoninV 1 條回覆 最後回覆
                                0
                                • P Payx

                                  Thanks both of you, it works !
                                  The problem i think is that i don't understand english very well, that's my problem.

                                  So if i understood well,

                                  i will have :

                                  void MainWindow::on_push2_clicked()
                                  {
                                  
                                  	QColor dominantColor(const QImage& pixi, const QPoint& topLeft, const QSize& rectSize);
                                  	void fillSection(QImage& pixi, const QPoint& topLeft, const QSize& rectSize, const QColor& colour);
                                  
                                  }
                                  

                                  I check what the code do, if i'm right i have to define the
                                  rectSize.height()
                                  and rectSize.width()

                                  and last question
                                  when i have

                                  for(int x = topLeft.x(); x < maxRight; ++x) {
                                  		for(int y = topLeft.y(); x < maxBottom; ++x) {
                                  

                                  in the second line, it's not 'y' the variable who change?

                                  and

                                  const int maxRight = qMin(pixi.width(), topLeft.x() + rectSize.width());
                                  for(int x = topLeft.x(); x < maxRight; ++x) {
                                  

                                  if we define rectSize (4*4) the for will go from 0 to 4 only no ? we need to change the rectsize at the end of the for no ?

                                  VRoninV 離線
                                  VRoninV 離線
                                  VRonin
                                  寫於 最後由 編輯
                                  #43

                                  @Payx said in Picture:

                                  i will have :
                                  void MainWindow::on_push2_clicked()
                                  {

                                  QColor dominantColor(const QImage& pixi, const QPoint& topLeft, const QSize& rectSize);
                                  void fillSection(QImage& pixi, const QPoint& topLeft, const QSize& rectSize, const QColor& colour);

                                  }

                                  this makes no sense

                                  in the second line, it's not 'y' the variable who change?

                                  yes, I corrected it but @mrjj was faster and copied the code before I could amend it

                                  the for will go from 0 to 4 only no ?

                                  no, it will go from 0 to 3, 4 pixels

                                  "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

                                  P kshegunovK 2 條回覆 最後回覆
                                  0
                                  • VRoninV VRonin

                                    @Payx said in Picture:

                                    i will have :
                                    void MainWindow::on_push2_clicked()
                                    {

                                    QColor dominantColor(const QImage& pixi, const QPoint& topLeft, const QSize& rectSize);
                                    void fillSection(QImage& pixi, const QPoint& topLeft, const QSize& rectSize, const QColor& colour);

                                    }

                                    this makes no sense

                                    in the second line, it's not 'y' the variable who change?

                                    yes, I corrected it but @mrjj was faster and copied the code before I could amend it

                                    the for will go from 0 to 4 only no ?

                                    no, it will go from 0 to 3, 4 pixels

                                    P 離線
                                    P 離線
                                    Payx
                                    寫於 最後由 VRonin 編輯
                                    #44

                                    @VRonin

                                    no, it will go from 0 to 3, 4 pixels

                                    Yes but if my picture is composed by 500 pixels, it will go from 0 to 3 pixels, and thats it, so i have to increase at the end of my "for" no ?

                                    this makes no sense

                                    When i want to call a function i have to write :

                                    fillSection();

                                    but as i said i got an error.

                                    so i change to fillSection(QImage& pixi, const QPoint& topLeft, const QSize& rectSize, const QColor& colour);

                                    why this makes no sense?

                                    1 條回覆 最後回覆
                                    0
                                    • VRoninV 離線
                                      VRoninV 離線
                                      VRonin
                                      寫於 最後由 編輯
                                      #45

                                      @Payx said in Picture:

                                      Yes but if my picture is composed by 500 pixels, it will go from 0 to 3 pixels, and thats it, so i have to increase at the end of my "for" no ?

                                      what would you do?

                                      @Payx said in Picture:

                                      why this makes no sense?

                                      www.cplusplus.com/doc/tutorial/functions/

                                      www.cplusplus.com/doc/tutorial/classes/

                                      "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

                                      P 1 條回覆 最後回覆
                                      0
                                      • VRoninV VRonin

                                        @Payx said in Picture:

                                        Yes but if my picture is composed by 500 pixels, it will go from 0 to 3 pixels, and thats it, so i have to increase at the end of my "for" no ?

                                        what would you do?

                                        @Payx said in Picture:

                                        why this makes no sense?

                                        www.cplusplus.com/doc/tutorial/functions/

                                        www.cplusplus.com/doc/tutorial/classes/

                                        P 離線
                                        P 離線
                                        Payx
                                        寫於 最後由 Payx 編輯
                                        #46

                                        @VRonin I got rectSize = 4*4

                                        the function will calculate the color in the rect 44, if my picture is 500500, i have to increase at the end of my "for" the rectSize.

                                        like say that my maxRight isnt

                                        qMin(pixi.width(), topLeft.x() + rectSize.width());
                                        

                                        anymore

                                        and it's like qMin(pixi.width(), rectSize.width()+4)

                                        and at the end of my for i will add a line to increase again and again the maxRight

                                        1 條回覆 最後回覆
                                        0
                                        • VRoninV 離線
                                          VRoninV 離線
                                          VRonin
                                          寫於 最後由 編輯
                                          #47

                                          those to functions are designed to work on a single 4x4 area, you need to build something around them to make them work over the whole picture

                                          "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 條回覆 最後回覆
                                          0

                                          • 登入

                                          • Login or register to search.
                                          • 第一個貼文
                                            最後的貼文
                                          0
                                          • 版面
                                          • 最新
                                          • 標籤
                                          • 熱門
                                          • 使用者
                                          • 群組
                                          • 搜尋
                                          • Get Qt Extensions
                                          • Unsolved