Advertisement
Guest User

Untitled

a guest
Oct 21st, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.16 KB | None | 0 0
  1. #include "mainwindow.h"
  2. #include <QImage>
  3. #include <QPixmap>
  4. #include <QImageReader>
  5. #include <QFileDialog>
  6. #include "cmath"
  7. #include "iostream"
  8. #include "algorithm"
  9.  
  10. MainWindow::MainWindow(QWidget *parent) :
  11. QMainWindow(parent)
  12. {
  13. draw_widget = new QLabel("", this);
  14.  
  15. hlayout = new QHBoxLayout();
  16. open_button = new QPushButton("Open Image", this);
  17. hlayout->addWidget(open_button);
  18. for (int i = 0; i < BUTTONS_COUNT; ++i) {
  19. buttons[i] = new QPushButton("Button " + QString::number(i + 1), this);
  20. hlayout->addWidget(buttons[i]);
  21. }
  22.  
  23. vlayout = new QVBoxLayout();
  24. vlayout->addWidget(draw_widget);
  25. vlayout->addLayout(hlayout);
  26.  
  27. QWidget *central = new QWidget(this);
  28. central->setLayout(vlayout);
  29. setCentralWidget(central);
  30.  
  31. setFixedSize(800, 600);
  32.  
  33. connect(open_button, SIGNAL(clicked(bool)), this, SLOT(open_image()));
  34. connect(buttons[0], SIGNAL(clicked(bool)), this, SLOT(button0_slot()));
  35. connect(buttons[1], SIGNAL(clicked(bool)), this, SLOT(button1_slot()));
  36. }
  37.  
  38. MainWindow::~MainWindow()
  39. {
  40. delete open_button;
  41. delete draw_widget;
  42. for (int i = 0; i < BUTTONS_COUNT; ++i)
  43. delete buttons[i];
  44. delete hlayout;
  45. delete vlayout;
  46. if (image) delete image;
  47. }
  48.  
  49. void MainWindow::open_image() {
  50. QFileDialog dialog(this);
  51. dialog.setNameFilter(tr("Images (*.png *.bmp *.jpg)"));
  52. dialog.setViewMode(QFileDialog::Detail);
  53. QString filename = QFileDialog::getOpenFileName(this, tr("Open File"),
  54. "C:/",
  55. tr("Images (*.png *.xpm *.jpg)"));
  56. if (filename == "") image=nullptr;
  57.  
  58. QImageReader reader(filename);
  59. reader.setAutoTransform(true);
  60. QImage newImage = reader.read();
  61. if (newImage.isNull()) image = nullptr;
  62. image = new QImage(newImage);
  63. update_image();
  64. }
  65.  
  66. void MainWindow::update_image() {
  67. if (!image) return;
  68. draw_widget->setPixmap(QPixmap::fromImage(*image));
  69. }
  70.  
  71. void MainWindow::button0_slot() {
  72. if (!image) return;
  73. for (int i = 0; i < image->width(); ++i)
  74. for (int j = 0; j < image->height() / 2; ++j) {
  75. QRgb x = image->pixel(i, j);
  76. QRgb y = image->pixel(i, image->height() - 1 - j);
  77.  
  78. image->setPixel(i, j, y);
  79. image->setPixel(i, image->height() - 1 - j, x);
  80. }
  81. update_image();
  82. }
  83.  
  84. bool MainWindow::inImage(int x, int y){
  85. int n = image->width();
  86. int m = image->height();
  87. return 0 <= x && x < n && 0 <= y && y < m;
  88. }
  89.  
  90. bool inField(int x, int y, QImage* image){
  91. int n = image->width();
  92. int m = image->height();
  93. return 0 <= x && x < n && 0 <= y && y < m;
  94. }
  95.  
  96.  
  97. QImage erosionImage(QImage *image, int matrixSizeFix = 3, int color = 0){
  98. QImage newImage = *image;
  99. matrixSizeFix /= 2;
  100. for (int i = 0; i < image->width(); ++i){
  101. for (int j = 0; j < image->height(); ++j) {
  102. bool isOkThisColor = 0;
  103. int toPaint = 0;
  104. for (int shiftI = -matrixSizeFix; shiftI < matrixSizeFix; shiftI++){
  105. for (int shiftJ = -matrixSizeFix; shiftJ < matrixSizeFix; shiftJ++){
  106. int ni = i + shiftI, nj = j + shiftJ;
  107. QRgb x;
  108. if (!inField(ni, nj, image)){
  109. x = image->pixel(i, j);
  110. }
  111. else{
  112. x = image->pixel(ni, nj);
  113. }
  114. if (qGray(x) != color){
  115. isOkThisColor = 1;
  116. toPaint = qGray(x);
  117. }
  118. }
  119. }
  120. if (isOkThisColor){
  121. QColor newPixel(toPaint, toPaint, toPaint);
  122. newImage.setPixelColor(i, j, newPixel);
  123. }
  124. else{
  125. QColor newPixel(color, color, color);
  126. newImage.setPixelColor(i, j, newPixel);
  127. }
  128. }
  129. }
  130. return newImage;
  131. }
  132.  
  133. int edge(QImage *image){
  134. int ser = 0, ppl = 0;
  135. for (int i = 0; i < image->width(); ++i){
  136. for (int j = 0; j < image->height(); ++j) {
  137. QRgb x = image->pixel(i, j);
  138. int sr = qGray(x);
  139. ser += sr;
  140. ppl++;
  141. }
  142. }
  143. return ser / ppl;
  144. }
  145.  
  146. QImage blackAndWhite(QImage *image){
  147.  
  148. int edgeImg = edge(image);
  149.  
  150. for (int i = 0; i < image->width(); ++i){
  151. for (int j = 0; j < image->height(); ++j) {
  152. QRgb x = image->pixel(i, j);
  153. int sr = qGray(x);
  154. if (sr <= edgeImg){
  155. QColor newPixel(0, 0, 0);
  156. image->setPixelColor(i, j, newPixel);
  157. }
  158. else{
  159. QColor newPixel(255, 255, 255);
  160. image->setPixelColor(i, j, newPixel);
  161. }
  162. }
  163. }
  164. return *image;
  165. }
  166.  
  167. QImage fixImage(QImage *image){
  168. *image = erosionImage(image, 3, 0);
  169. return *image;
  170. }
  171.  
  172. void MainWindow::button1_slot() {
  173. if (!image) return;
  174.  
  175. *image = blackAndWhite(image);
  176.  
  177. //*image = fixImage(image);
  178.  
  179. update_image();
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement