Solved Picture
-
I have to declare my function,
say what they do
and call it.I say in my .cpp what she does :
QColor dominantColor(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(); y < maxBottom; ++y) { 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; }
I call her and i stock the result in a QColor
void MainWindow::on_push2_clicked() { QPoint point(0,0); QSize size(4,4); QColor domicolor=dominantColor(pixi,point,size); // pixi is my image declared in .h with QImage pixi; }
the problem that i dont know where i can declare her. I try every possibilities
-
ok, now we are making progress. you got what the dominant colour is, now you have to paint the rectange
-
-
-
Okay i fixed it
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } QColor dominantColor(const QImage& image, const QPoint& topLeft, const QSize& rectSize); void fillSection(QImage& image, const QPoint& topLeft, const QSize& rectSize, const QColor& colour); MainWindow::~MainWindow() { delete ui; }
So now with, fillSection, the problem i have is to declare something who correspond to QColor& colour
i check the code again, and i think its colourList no ?
-
sorry if don't listen to you very well but
No worries, I'm explicitly refraining from giving you the exact answer and rather trying to send you on the right path.
i check the code again, and i think its colourList no ?
Nope, colourList will die: http://www.cplusplus.com/doc/tutorial/namespaces/#scopes
-
Nope, colourList will die: http://www.cplusplus.com/doc/tutorial/namespaces/#scopes
Ok i got it, we will put the returned color from QColor dominantColor, thats right ?
-
Getting warmer and warmer
-
I got :
QColor domicolor=dominantColor(pixi,point,size);
The returned color is "domicolor", so i put domicolor in :
void fillSection(pixi, point, size, domicolor);
-
-
I call her with just :
fillSection(pixi,point,size,domicolor);
-
@Payx Bingo!
-
I try to change the rectSize with 100 x 100.
my first function is :
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()); ui->label_2->setText( "Size: " + QString::number(s.width()) +" "+ QString::number(s.height()) ); }
It load an image with an explorer file.
But the picture dont change at all when i click on the push2
-
I did not say you were done, just that you were on the right path.
from the code you posted it's clear you did not grasp scoping 100% so i'll repost the link: http://www.cplusplus.com/doc/tutorial/namespaces/#scopes
pix
andpixi
die as soon asMainWindow::on_push_clicked()
terminates -
@VRonin Yes but in my .h i add QImage pixi thats not correct?
-
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
-
-
@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
-
@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 ?
-
Ok, this has become too chatty, let's make it more structured before we both get banned.
- Put
dominantColor
andfillSection
code from either me or @mrjj at the top of your .cpp file, just below the#include
s no need to declare them in advance in this case - in the
private:
section of your .h file addQImage m_baseImag;
- in on_push_clicked load the image from file and store it in
m_baseImag
then useQPixmap::fromImage(m_baseImag)
to display the image in theQLabel
- 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 anotherQLabel
- Put