Solved Picture
-
@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 } } }
-
Thanks for your help,
I got one error
undefined reference to 'MainWindow::dominantColor()'
-
-
thats wasnt a simple letter that occured an error,
undefined reference to 'MainWindow::dominantColour()'
undefined reference to ''MainWindow::fillSection()' -
Because they are not part of MainWindow. Take a look again a the function signature.
-
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'
-
Note: neither
dominantColour
norfillSection
make sense in the slots category. -
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.
-
So where am i suppose to declare my functions ?
-
@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++.
-
Hi,
- Function arguments must match otherwise u will get error.
- Declare your functions in Header File, provide the definition in source file.
- 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.
- 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,
-
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 havefor(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 ?
-
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
-
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?
-
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?
why this makes no sense?
-
@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
-
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
-
Thanks for your link obviously what i need.
so i replace : i got
QColor dominantColor(pixi,point,size);
and i define pixi as my image (in mainwindow.h)
and QPoint point(0,0);
and QSize size(4,4); thats correct ?but i block for the QColor in void fillSection(QImage& image, const QPoint& topLeft, const QSize& rectSize, const QColor& colour)
-
@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
It's valid C++, although highly unusual. Consider this:
void userFunction1() { int x; testFunc(x); //< Compile error: undeclared identifier testFunc } void userFunction2() { void testFunc(int &); int x; testFunc(x); //< We are okay } void testFunc(int & z) { z = 0; }
C++ forbids function definitions to appear inside function bodies, but not declarations. On the other hand, class declarations and class definitions are valid inside functions, e.g.:
void terribleToReadFunction() { struct { int memberOfAnonymous() const { return 0; } } inst; int z = inst.memberOfAnonymous(); //< z is 0 }
-
@kshegunov
sorry i lost youin my
void MainWindow::on_push2_clicked() { QPoint point(0,0); QSize size(4,4); QColor dominantColor(pixi,point,size); }
i try this i give what function needed and i declared point and size before