跳到內容
  • 版面
  • 最新
  • 標籤
  • 熱門
  • 使用者
  • 群組
  • 搜尋
  • 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.5k 瀏覽 3 Watching
  • 從舊到新
  • 從新到舊
  • 最多點贊
回覆
  • 在新貼文中回覆
登入後回覆
此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
  • kshegunovK kshegunov

    @VRonin said in Picture:

    I'm surprised your compiler doesn't yell about redefinition

    Not redefinition, shadowing, hence no yelling. :)

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

    @kshegunov said in Picture:

    shadowing

    The case @kshegunov is explaining is in the 3rd code block of the link above:

    // inner block scopes
    #include <iostream>
    using namespace std;
    
    int main () {
      int x = 10;
      int y = 20;
      {
        int x;   // ok, inner scope.
        x = 50;  // sets value to inner x
        y = 50;  // sets value to (outer) y
        cout << "inner block:\n";
        cout << "x: " << x << '\n';
        cout << "y: " << y << '\n';
      }
      cout << "outer block:\n";
      cout << "x: " << x << '\n';
      cout << "y: " << y << '\n';
      return 0;
    }
    

    This means I should go back to that tutorial myself

    "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 條回覆 最後回覆
    2
    • VRoninV VRonin

      I'm surprised your compiler doesn't yell about redefinition (I probably know why but that's not the point here). I'send you to http://www.cplusplus.com/doc/tutorial/namespaces/#scopes once again

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

      @VRonin so if i want use my picture in an other function i have to declare it before my function as i did for

      QColor dominantColor(const QImage& image, const QPoint& topLeft, const QSize& rectSize);

      so i have to add a line code QImage pixi in my .cpp ?

      why not in my .h, and why

      QColor dominantColor
      

      dont work in my .h too ?

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

        Ok, this has become too chatty, let's make it more structured before we both get banned.

        • Put dominantColor and fillSection code from either me or @mrjj at the top of your .cpp file, just below the #includes no need to declare them in advance in this case
        • in the private: section of your .h file add QImage m_baseImag;
        • in on_push_clicked load the image from file and store it in m_baseImag then use QPixmap::fromImage(m_baseImag) to display the image in the QLabel
        • in on_push2_clicked iterate over every 4x4 square of your picture calling the two functions:
        for (int i=0;i<m_baseImag.width();i+=4){
                   for (int j=0;j<m_baseImag.height();j+=4){
        fillSection(m_baseImag,QPoint(i,j),QSize(4,4),dominantColour(m_baseImag,QPoint(i,j),QSize(4,4)));
        }}
        
        • use QPixmap::fromImage(m_baseImag) to display the new image in another QLabel

        "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
          寫於 最後由 Payx 編輯
          #73

          Hello,

          I have 0 error but it didn't work.

          here my code

          void MainWindow::on_push_clicked()
          {
          	QString fileName = QFileDialog::getOpenFileName(this,
          	tr("Open Image"), "/", tr("Image Files (*.png *.jpg *.bmp)"));
          	QPixmap pix(fileName);
          	ui->label->setPixmap(pix);
          	const QSize s = pix.size();
          	QImage pixi = QImage(pix.toImage());
          
          	
          
          }
          
          void MainWindow::on_push2_clicked()
          {
          	QPoint point(0,0);
          	QSize size(4,4);
          	for(int i=0;i<pixi.width();i+=4){
          		for(int j=0;j<pixi.height();j+=4){
          			fillSection(pixi,point,size,dominantColor(pixi,point,size));
          		}
          	}
          
          	QPixmap pixa=QPixmap::fromImage(pixi);
          
          	ui->label_2->setPixmap(pixa);
          }
          

          In my .h i declared

          private:
             Ui::MainWindow *ui;
             QImage pixi;
          

          pixi is the name of my picture (= m_baseImag)

          i display pixa (Qpix from pixi) in the same label as pixi (dont know if i can do that)

          1 條回覆 最後回覆
          0
          • mrjjM 離線
            mrjjM 離線
            mrjj
            Lifetime Qt Champion
            寫於 最後由 編輯
            #74

            Hi
            Good work.
            in here
            void MainWindow::on_push_clicked()
            {
            ......
            QImage pixi = QImage(pix.toImage()); // This looks like a local variable? ( die by scope)
            }

            Did you mean to use the one from .H ?

            so
            pixi = QImage(pix.toImage());

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

              i changed my code by using pixi = QImage(pix.toImage());

              and in my .h i got QImage pixi.

              I try to fix my rectSize to 15x15

              And my picture at the end is the same, she changed a little (she got a blue pixel in the top left

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

                you never update point in your loop. it's always 0,0 it should be i,j

                "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

                  you never update point in your loop. it's always 0,0 it should be i,j

                  P 離線
                  P 離線
                  Payx
                  寫於 最後由 Payx 編輯
                  #77
                  此回覆已被刪除!
                  1 條回覆 最後回覆
                  0
                  • P 離線
                    P 離線
                    Payx
                    寫於 最後由 編輯
                    #78

                    Ok after long hours, i fixed it.

                    Thank you very much for your time in my project.

                    But i have a little question :

                    In the code it only returns Red, Blue or Green.

                    Do you have a function who return a lot of color, like if in my rect the most principal color is grey (for example) what could i do to add a lot of color ?

                    At the end i will have a picture more pixelated but that i can recognize

                    kshegunovK 1 條回覆 最後回覆
                    0
                    • mrjjM 離線
                      mrjjM 離線
                      mrjj
                      Lifetime Qt Champion
                      寫於 最後由 編輯
                      #79

                      Hi

                      • if in my rect the most principal color is grey

                      That is just values for R,G,B. still.

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

                        Modify dominantColour with this one. It takes the most frequent colour that appears in the square and fills it with it

                        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());
                        	QHash<QColor,int> colourMap;
                        	for(int x=topLeft.x();x<maxRight;++x){
                        		for(int y=topLeft.y();y<maxBottom;++y){
                        			const QColor tempColor=image.pixelColor(x,y);
                        			auto colourIter=colourMap.find(tempColor);
                        			if(colourIter==colourMap.end())
                        				colourMap.insert(tempColor,1);
                        			else
                        				colourIter.value()++;
                        		}
                        	}
                        	if(colourMap.isEmpty())
                        		return Qt::black;
                        	return std::max_element(colourMap.constBegin(),colourMap.constEnd()).key();
                        }
                        

                        "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 Payx

                          Ok after long hours, i fixed it.

                          Thank you very much for your time in my project.

                          But i have a little question :

                          In the code it only returns Red, Blue or Green.

                          Do you have a function who return a lot of color, like if in my rect the most principal color is grey (for example) what could i do to add a lot of color ?

                          At the end i will have a picture more pixelated but that i can recognize

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

                          I suggest you average the color in the rectangle, e.g. (thanks to @VRonin for the scaffolding):

                          QColor dominantColour(const QImage & image,const QPoint & topLeft, const QSize & rectSize)
                          {
                              qint32 averageRed = 0, averageGreen = 0, averageBlue = 0;
                          
                              const qint32 maxX = qMin<qint32>(image.width(), topLeft.x() + rectSize.width());
                              const qint32 maxY = qMin<qint32>(image.height(), topLeft.y() + rectSize.height());
                              for (qint32 y = topLeft.y(); y < maxY; y++)  {
                                  for (qint32 x = topLeft.x(); x < maxX; x++)   {
                                      QRgb pixel = image.pixel(x, y);
                                      
                                      averageRed += qRed(pixel);
                                      averageGreen += qGreen(pixel);
                                      averageBlue += qBlue(pixel);
                                  }
                              }
                          
                              qint32 n = rectSize.width() * rectSize.height();
                          
                              Q_ASSERT(n);
                              if (n <= 0)
                                  return Qt::black;
                          
                              return QColor(averageRed / n, averageGreen / n, averageBlue / n);
                          }
                          

                          Read and abide by the Qt Code of Conduct

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

                            Thank you very much !

                            1 條回覆 最後回覆
                            0

                            • 登入

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