323 lines
8.5 KiB
C++
323 lines
8.5 KiB
C++
// Dolaczamy plik naglowkowy naszej klasy MyWindow
|
|
#include "mywindow.h"
|
|
|
|
// Dolaczamy plik naglowkowy zawierajacy definicje GUI
|
|
// Plik ten jest generowany automatycznie
|
|
// z pliku XML "mywindow.ui"
|
|
#include "ui_mywindow.h"
|
|
|
|
#include <cmath>
|
|
#include <QColor>
|
|
#include <QDebug>
|
|
#include <QColorDialog>
|
|
#include <iostream>
|
|
#include <float.h> // for float,double macros
|
|
#include <string.h>
|
|
#include <stack>
|
|
#include <vector>
|
|
|
|
#define PI 3.1415
|
|
|
|
std::vector<float> matrixMul(std::vector<float> p, std::vector<float> m)
|
|
{
|
|
std::vector<float> res(3);
|
|
for(int i = 0; i < 3; i++)
|
|
{
|
|
res.push_back(0);
|
|
for(int j = 0; j < 3; j++)
|
|
{
|
|
res[i] += m[i * 3 + j] * p[j];
|
|
}
|
|
}
|
|
return res;
|
|
}
|
|
|
|
std::vector<float> matrixMul3x3(std::vector<float> m1, std::vector<float> m2)
|
|
{
|
|
std::vector<float> res(9, 0);
|
|
for(int i = 0; i < 3; i++)
|
|
{
|
|
for(int j = 0; j < 3; j++)
|
|
{
|
|
for(int k = 0; k < 3; k++)
|
|
{
|
|
res[i * 3 + j] += m1[i * 3 + k] * m2[k * 3 + j];
|
|
}
|
|
}
|
|
}
|
|
return res;
|
|
}
|
|
|
|
|
|
QColor mul(float v, QColor c){
|
|
int red = (int)(c.red() * v);
|
|
if(red > 255) red = 255;
|
|
int green = (int)(c.green() * v);
|
|
if(green > 255) green = 255;
|
|
int blue = (int)(c.blue() * v);
|
|
if(blue > 255) blue = 255;
|
|
return QColor(red, green, blue);
|
|
}
|
|
|
|
QColor add(QColor c1, QColor c2){
|
|
int red = (int)(c1.red() + c2.red());
|
|
if(red > 255) red = 255;
|
|
int green = (int)(c1.green() + c2.green());
|
|
if(green > 255) green = 255;
|
|
int blue = (int)(c1.blue() + c2.blue());
|
|
if(blue > 255) blue = 255;
|
|
return QColor(red, green, blue);
|
|
}
|
|
// Definicja konstruktora, wywolujemy najpierw
|
|
// konstruktor klasy nadrzednej, nastepnie tworzymy
|
|
// obiekt klasy Ui_MyWindow reprezentujacy GUI
|
|
MyWindow::MyWindow(QWidget *parent) :
|
|
QMainWindow(parent),
|
|
ui(new Ui::MyWindow)
|
|
{
|
|
// Wywolujemy funkcje tworzaca elementy GUI
|
|
// Jej definicja znajduje sie w pliku "ui_mywindow.h"
|
|
ui->setupUi(this);
|
|
|
|
// Pobieramy wymiary i wspolrzedne lewego gornego naroznika ramki
|
|
// i ustawiamy wartosci odpowiednich pol
|
|
// Uwaga: ramke "rysujFrame" wykorzystujemy tylko do
|
|
// wygodnego ustaiwenia tych wymiarow. Rysunek bedziemy wyswietlac
|
|
// bezposrednio w glownym oknie aplikacji.
|
|
szer = ui->rysujFrame->width();
|
|
wys = ui->rysujFrame->height();
|
|
poczX = ui->rysujFrame->x();
|
|
poczY = ui->rysujFrame->y();
|
|
|
|
// Tworzymy obiekt klasy QImage, o odpowiedniej szerokosci
|
|
// i wysokosci. Ustawiamy format bitmapy na 32 bitowe RGB
|
|
// (0xffRRGGBB).
|
|
img = new QImage(szer,wys,QImage::Format_RGB32);
|
|
img2 = new QImage(szer,wys,QImage::Format_RGB32);
|
|
|
|
loaded_img1 = new QImage("/Users/dawidpietrykowski/Desktop/projects/umk/GK/QT/morphology/c.jpg");
|
|
|
|
UpdateImage();
|
|
|
|
for(int x = 0; x < szer; x++)
|
|
for(int y = 0; y < wys; y++){
|
|
QColor color = GetPixel(img, x, y);
|
|
if((color.red() + color.green() + color.blue()) / 3.0f > 128.0f)
|
|
DrawPixel(img, x, y, QColor(255, 255, 255));
|
|
else
|
|
DrawPixel(img, x, y, QColor(0, 0, 0));
|
|
}
|
|
|
|
|
|
update();
|
|
}
|
|
|
|
// Definicja destruktora
|
|
MyWindow::~MyWindow()
|
|
{
|
|
delete ui;
|
|
}
|
|
|
|
// Funkcja (slot) wywolywana po nacisnieciu przycisku "Wyjscie" (exitButton)
|
|
// Uwaga: polaczenie tej funkcji z sygnalem "clicked"
|
|
// emitowanym przez przycisk jest realizowane
|
|
// za pomoca funkcji QMetaObject::connectSlotsByName(MyWindow)
|
|
// znajdujacej sie w automatycznie generowanym pliku "ui_mywindow.h"
|
|
// Nie musimy wiec sami wywolywac funkcji "connect"
|
|
void MyWindow::on_exitButton_clicked()
|
|
{
|
|
// qApp to globalny wskaznik do obiektu reprezentujacego aplikacje
|
|
// quit() to funkcja (slot) powodujaca zakonczenie aplikacji z kodem 0 (brak bledu)
|
|
qApp->quit();
|
|
}
|
|
|
|
void MyWindow::on_erosion_clicked(){
|
|
Erosion();
|
|
update();
|
|
// UpdateImage();
|
|
}
|
|
|
|
void MyWindow::on_dilation_clicked(){
|
|
Dilation();
|
|
update();
|
|
// UpdateImage();
|
|
}
|
|
|
|
void MyWindow::on_opening_clicked(){
|
|
Open();
|
|
update();
|
|
// UpdateImage();
|
|
}
|
|
|
|
void MyWindow::on_closing_clicked(){
|
|
Close();
|
|
update();
|
|
// UpdateImage();
|
|
}
|
|
|
|
void MyWindow::UpdateImage(){
|
|
for(int x = 0; x < szer; x++)
|
|
for(int y = 0; y < wys; y++){
|
|
float xw = (float) x / (float)szer;
|
|
float yh = (float) y / (float) wys;
|
|
DrawPixel(img, x, y, GetInterpolatedColor(loaded_img1, xw, yh));
|
|
}
|
|
|
|
update();
|
|
}
|
|
|
|
bool MyWindow::IsLit(QImage* im, int x, int y, int val){
|
|
int width = im->width();
|
|
int height = im->height();
|
|
if(x < 0 || y < 0 || x >= width || y >= height)
|
|
return false;
|
|
QColor pix = GetPixel(im, x, y);
|
|
return pix.red() == val && pix.green() == val && pix.blue() == val;
|
|
}
|
|
|
|
void MyWindow::Erosion(){
|
|
int width = img->width();
|
|
int height = img->height();
|
|
|
|
memcpy(img2->bits(), img->bits(), width * height * 4);
|
|
|
|
for(int x = 0; x < width; x++)
|
|
for(int y = 0; y < height; y++){
|
|
bool active = false;
|
|
for (int x2 = x - 1; x2 <= x + 1; x2++)
|
|
for (int y2 = y - 1; y2 <= y + 1; y2++)
|
|
if(IsLit(img, x2, y2, 0)){
|
|
active = true;
|
|
break;
|
|
}
|
|
if(active)
|
|
DrawPixel(img2, x, y, QColor(0, 0, 0));
|
|
}
|
|
memcpy(img->bits(), img2->bits(), width * height * 4);
|
|
}
|
|
|
|
void MyWindow::Dilation(){
|
|
int width = img->width();
|
|
int height = img->height();
|
|
|
|
memcpy(img2->bits(), img->bits(), width * height * 4);
|
|
|
|
for(int x = 0; x < width; x++)
|
|
for(int y = 0; y < height; y++){
|
|
bool active = false;
|
|
|
|
for (int x2 = x - 1; x2 <= x + 1; x2++){
|
|
for (int y2 = y - 1; y2 <= y + 1; y2++){
|
|
if(IsLit(img, x2, y2, 255)){
|
|
active = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
if(active)
|
|
DrawPixel(img2, x, y, QColor(255, 255, 255));
|
|
|
|
}
|
|
memcpy(img->bits(), img2->bits(), width * height * 4);
|
|
}
|
|
|
|
void MyWindow::Close(){
|
|
Dilation();
|
|
Erosion();
|
|
}
|
|
|
|
void MyWindow::Open(){
|
|
Erosion();
|
|
Dilation();
|
|
}
|
|
|
|
QColor MyWindow::GetPixel(QImage* img, int x, int y){
|
|
QColor res;
|
|
int width = img->width();
|
|
int height = img->height();
|
|
if(x >= width || y >= height || x < 0 || y < 0)
|
|
return res;
|
|
|
|
unsigned char* ptr = img->bits();
|
|
|
|
res.setBlue(ptr[width*4*y + 4*x]);
|
|
res.setGreen(ptr[width*4*y + 4*x + 1]);
|
|
res.setRed(ptr[width*4*y + 4*x + 2]);
|
|
res.setAlpha(ptr[width*4*y + 4*x + 3]);
|
|
|
|
return res;
|
|
}
|
|
|
|
|
|
QColor MyWindow::GetInterpolatedColor(QImage* img, float x, float y){
|
|
float width = img->width();
|
|
float height = img->height();
|
|
float xw = (x * width);
|
|
float yw = (y * height);
|
|
int x_f = (int) (x * width);
|
|
int y_f = (int) (y * height);
|
|
int x_c = ceil(x * width);
|
|
int y_c = ceil(y * height);
|
|
float a = (xw - (float) x_f);
|
|
float b = ((yw) - (float) y_f);
|
|
|
|
QColor P1 = GetPixel(img, x_f, y_c);
|
|
QColor P2 = GetPixel(img, x_c, y_c);
|
|
QColor P3 = GetPixel(img, x_c, y_f);
|
|
QColor P4 = GetPixel(img, x_f, y_f);
|
|
|
|
QColor v1 = mul(1.0f - a, P1);
|
|
QColor v2 = mul(a, P2);
|
|
QColor v3 = mul(a, P3);
|
|
QColor v4 = mul(1.0f - a, P4);
|
|
QColor top = add(v1, v2);
|
|
QColor bottom = add(v3, v4);
|
|
QColor btop = mul(b, top);
|
|
QColor bbottom = mul(1.0f - b, bottom);
|
|
QColor res = add(btop, bbottom);
|
|
|
|
return res;
|
|
}
|
|
|
|
// Funkcja "odmalowujaca" komponent
|
|
void MyWindow::paintEvent(QPaintEvent*)
|
|
{
|
|
// Obiekt klasy QPainter pozwala nam rysowac na komponentach
|
|
QPainter p(this);
|
|
|
|
// Rysuje obrazek "img" w punkcie (poczX,poczY)
|
|
// (tu bedzie lewy gorny naroznik)
|
|
p.drawImage(poczX,poczY,*img);
|
|
}
|
|
|
|
|
|
void MyWindow::DrawPixel(QImage* im, int x, int y, QColor color){
|
|
int width = im->width();
|
|
int height = im->height();
|
|
|
|
if(x >= width || y >= height || x < 0 || y < 0)
|
|
return;
|
|
|
|
unsigned char* ptr = im->bits();
|
|
|
|
ptr[width*4*y + 4*x] = color.blue();
|
|
ptr[width*4*y + 4*x + 1] = color.green();
|
|
ptr[width*4*y + 4*x + 2] = color.red();
|
|
ptr[width*4*y + 4*x + 3] = color.alpha();
|
|
}
|
|
|
|
|
|
void MyWindow::ClearImage(QImage *img){
|
|
unsigned char* empty_val = (unsigned char*)malloc(4);
|
|
empty_val[0] = 0;
|
|
empty_val[1] = 0;
|
|
empty_val[2] = 0;
|
|
empty_val[3] = 255;
|
|
unsigned char* ptr = img->bits();
|
|
for(int i = 0; i < img->width(); i++){
|
|
for(int j = 0; j < img->height(); j++){
|
|
memcpy(ptr + 4 * (i + j * img->width()), empty_val, 4);
|
|
}
|
|
}
|
|
}
|