Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "mainwindow.h"
- #include <QImage>
- #include <QPixmap>
- #include <QImageReader>
- #include <QFileDialog>
- #include "cmath"
- #include "iostream"
- #include "algorithm"
- MainWindow::MainWindow(QWidget *parent) :
- QMainWindow(parent)
- {
- draw_widget = new QLabel("", this);
- hlayout = new QHBoxLayout();
- open_button = new QPushButton("Open Image", this);
- hlayout->addWidget(open_button);
- for (int i = 0; i < BUTTONS_COUNT; ++i) {
- buttons[i] = new QPushButton("Button " + QString::number(i + 1), this);
- hlayout->addWidget(buttons[i]);
- }
- vlayout = new QVBoxLayout();
- vlayout->addWidget(draw_widget);
- vlayout->addLayout(hlayout);
- QWidget *central = new QWidget(this);
- central->setLayout(vlayout);
- setCentralWidget(central);
- setFixedSize(800, 600);
- connect(open_button, SIGNAL(clicked(bool)), this, SLOT(open_image()));
- connect(buttons[0], SIGNAL(clicked(bool)), this, SLOT(button0_slot()));
- connect(buttons[1], SIGNAL(clicked(bool)), this, SLOT(button1_slot()));
- }
- MainWindow::~MainWindow()
- {
- delete open_button;
- delete draw_widget;
- for (int i = 0; i < BUTTONS_COUNT; ++i)
- delete buttons[i];
- delete hlayout;
- delete vlayout;
- if (image) delete image;
- }
- void MainWindow::open_image() {
- QFileDialog dialog(this);
- dialog.setNameFilter(tr("Images (*.png *.bmp *.jpg)"));
- dialog.setViewMode(QFileDialog::Detail);
- QString filename = QFileDialog::getOpenFileName(this, tr("Open File"),
- "C:/",
- tr("Images (*.png *.xpm *.jpg)"));
- if (filename == "") image=nullptr;
- QImageReader reader(filename);
- reader.setAutoTransform(true);
- QImage newImage = reader.read();
- if (newImage.isNull()) image = nullptr;
- image = new QImage(newImage);
- update_image();
- }
- void MainWindow::update_image() {
- if (!image) return;
- draw_widget->setPixmap(QPixmap::fromImage(*image));
- }
- void MainWindow::button0_slot() {
- if (!image) return;
- for (int i = 0; i < image->width(); ++i)
- for (int j = 0; j < image->height() / 2; ++j) {
- QRgb x = image->pixel(i, j);
- QRgb y = image->pixel(i, image->height() - 1 - j);
- image->setPixel(i, j, y);
- image->setPixel(i, image->height() - 1 - j, x);
- }
- update_image();
- }
- bool MainWindow::inImage(int x, int y){
- int n = image->width();
- int m = image->height();
- return 0 <= x && x < n && 0 <= y && y < m;
- }
- bool inField(int x, int y, QImage* image){
- int n = image->width();
- int m = image->height();
- return 0 <= x && x < n && 0 <= y && y < m;
- }
- QImage erosionImage(QImage *image, int matrixSizeFix = 3, int color = 0){
- QImage newImage = *image;
- matrixSizeFix /= 2;
- for (int i = 0; i < image->width(); ++i){
- for (int j = 0; j < image->height(); ++j) {
- bool isOkThisColor = 0;
- int toPaint = 0;
- for (int shiftI = -matrixSizeFix; shiftI < matrixSizeFix; shiftI++){
- for (int shiftJ = -matrixSizeFix; shiftJ < matrixSizeFix; shiftJ++){
- int ni = i + shiftI, nj = j + shiftJ;
- QRgb x;
- if (!inField(ni, nj, image)){
- x = image->pixel(i, j);
- }
- else{
- x = image->pixel(ni, nj);
- }
- if (qGray(x) != color){
- isOkThisColor = 1;
- toPaint = qGray(x);
- }
- }
- }
- if (isOkThisColor){
- QColor newPixel(toPaint, toPaint, toPaint);
- newImage.setPixelColor(i, j, newPixel);
- }
- else{
- QColor newPixel(color, color, color);
- newImage.setPixelColor(i, j, newPixel);
- }
- }
- }
- return newImage;
- }
- int edge(QImage *image){
- int ser = 0, ppl = 0;
- for (int i = 0; i < image->width(); ++i){
- for (int j = 0; j < image->height(); ++j) {
- QRgb x = image->pixel(i, j);
- int sr = qGray(x);
- ser += sr;
- ppl++;
- }
- }
- return ser / ppl;
- }
- QImage blackAndWhite(QImage *image){
- int edgeImg = edge(image);
- for (int i = 0; i < image->width(); ++i){
- for (int j = 0; j < image->height(); ++j) {
- QRgb x = image->pixel(i, j);
- int sr = qGray(x);
- if (sr <= edgeImg){
- QColor newPixel(0, 0, 0);
- image->setPixelColor(i, j, newPixel);
- }
- else{
- QColor newPixel(255, 255, 255);
- image->setPixelColor(i, j, newPixel);
- }
- }
- }
- return *image;
- }
- QImage fixImage(QImage *image){
- *image = erosionImage(image, 3, 0);
- return *image;
- }
- void MainWindow::button1_slot() {
- if (!image) return;
- *image = blackAndWhite(image);
- //*image = fixImage(image);
- update_image();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement