E
E
Evgeny Petryaev2021-05-07 01:35:18
Qt
Evgeny Petryaev, 2021-05-07 01:35:18

This i3image does not open?

main.cpp

#include <iostream>
#include <QtWidgets/QApplication>
#include "MainWindow.h"


int main(int argc,char*argv[])
{
    QApplication a(argc, argv);
    QWidget qw;
    MainWindow mw(&qw);
    mw.show();
    mw.CreateImage("s");
    mw.CreateImage2("s");
    mw.OpenImage("s");
    mw.OpenImage2("s");
    system("testImage.png");
    system("testImage2.png");
    return a.exec();
}

MainWindow.h
#pragma once
#include <QtWidgets/qmainwindow.h>
#include <QtGui/qpicture.h>
#include <QtGui/qimage.h>
#include <QtGui/qpainter.h>
#include <QtCore/qdebug.h> 
#include <vector>
#include <iostream>
#include <fstream> // работа с файлами
#include <iomanip> 
//#include <iostream>
#pragma comment(lib,"Qt5Core.lib")
#pragma comment(lib,"Qt5Widgets.lib")
#pragma comment(lib,"Qt5Gui.lib")

namespace Ui {
    class MainWindow;
}
//Q_OBJECT
class MainWindow : public QMainWindow
{
    //Q_OBJECT
public:
    explicit MainWindow(QWidget* parent = 0);
    void CreateImage(QString path);
    void CreateImage2(QString path);
    void OpenImage(QString path);
    void OpenImage2(QString path);
    std::vector<int> x;
    // ~MainWindow();
protected:
    void paintEvent(QPaintEvent*); // пееопределение виртуальной функции
    
private:
    Ui::MainWindow* ui;
    
};

MainWindow.cpp
#include "MainWindow.h"
MainWindow::MainWindow(QWidget* parent)
    : QMainWindow(parent)
{
    setWindowTitle(tr("Neural"));
    setGeometry(0, 0, 1000, 700);
}
void MainWindow::paintEvent(QPaintEvent*)
{
    
    QImage img("testImage.png"); // загружаем картинку
    QPainter painter(this); // определяем объект painter, который обеспечивает рисование
    painter.drawImage(0, 0, img.scaled(this->size())); // рисуем наше изображение от 0,0 и растягиваем по всему виджету
    
}

void MainWindow::CreateImage(QString path)
{
    QImage image(QSize(400, 300), QImage::Format_RGB32);
    QPainter painter(&image);
    painter.fillRect(QRectF(0, 0, 400, 300), Qt::white);
    painter.setPen(QPen(Qt::black));
    painter.drawLine(0, 0, image.width() / 2, image.height());
    painter.drawLine(image.width() / 2, image.height(), image.width(), 0);

    image.save("testImage.png");
}
void MainWindow::CreateImage2(QString path)
{
    QImage image(QSize(400, 300), QImage::Format_RGB32);
    QPainter painter(&image);
    painter.fillRect(QRectF(0, 0, 400, 300), Qt::white);
    painter.setPen(QPen(Qt::black));
    painter.drawLine(0, 0, image.width() / 4, image.height());
    painter.drawLine(image.width() / 4, image.height() , image.width() / 2, 0);
    painter.drawLine(image.width() / 2, 0, 3 * image.width() / 4, image.height());
    painter.drawLine(3*image.width() / 4, image.height(), image.width() , 0);

    image.save("testImage2.png");
}

void MainWindow::OpenImage(QString path)
{
    QImage img("testImage.png"); // загружаем картинку
    QPoint qp;
    std::ofstream fout("data.txt", std::ios_base::out | std::ios_base::trunc);

    for (int i = 0; i < img.width(); i++)
    {
        for (int j = 0; j < img.height(); j++)
        {
            qp.setX(i);
            qp.setY(j);
            if (img.pixel(qp) == Qt::black)
            {
                x.push_back(j);
                qDebug() << j << " ";
                fout << j << std::endl;
                break;
            }
        }

    }
    fout.close();
}
void MainWindow::OpenImage2(QString path)
{

}

Is the vector x after the OpenImage() method empty?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel K, 2021-05-07
@Gremlin92

Good day.
img.pixel(qp) returns a QRgb and you compare it to the normal Qt::black enum.
I think it should be something like this:

....
QRgb qBlack = QColor(Qt::black).rgb();
....
if (img.pixel(qp) == qBlack) {
.....

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question