Solved Picture
-
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)
-
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()); -
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
-
you never update point in your loop. it's always 0,0 it should be i,j
-
This post is deleted! -
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
-
Hi
- if in my rect the most principal color is grey
That is just values for R,G,B. still.
-
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(); }
-
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); }
-
Thank you very much !