Fixed morphology project

This commit is contained in:
Dawid Pietrykowski 2023-05-02 23:18:34 +02:00
parent 37c653d065
commit 619adc3a0b
3 changed files with 60 additions and 50 deletions

BIN
QT/morphology/c.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 MiB

View File

@ -93,23 +93,22 @@ MyWindow::MyWindow(QWidget *parent) :
// i wysokosci. Ustawiamy format bitmapy na 32 bitowe RGB // i wysokosci. Ustawiamy format bitmapy na 32 bitowe RGB
// (0xffRRGGBB). // (0xffRRGGBB).
img = new QImage(szer,wys,QImage::Format_RGB32); img = new QImage(szer,wys,QImage::Format_RGB32);
img2 = new QImage(szer,wys,QImage::Format_RGB32);
loaded_img1 = new QImage("/home/davp/umk/2022_2023/GK/QT/alpha-blending/f.png"); loaded_img1 = new QImage("/Users/dawidpietrykowski/Desktop/projects/umk/GK/QT/morphology/c.jpg");
loaded_img2 = new QImage("/home/davp/umk/2022_2023/GK/QT/alpha-blending/f.png");
int width = loaded_img1->width();
int height = loaded_img1->height();
for(int x = 0; x < width; x++)
for(int y = 0; y < height; y++){
QColor color = GetPixel(loaded_img1, x, y);
if((color.red() + color.green() + color.blue()) / 3.0f > 128.0f)
DrawPixel(loaded_img1, x, y, QColor(255, 255, 255));
else
DrawPixel(loaded_img1, x, y, QColor(0, 0, 0));
}
UpdateImage(); 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(); update();
} }
@ -133,23 +132,27 @@ void MyWindow::on_exitButton_clicked()
} }
void MyWindow::on_erosion_clicked(){ void MyWindow::on_erosion_clicked(){
Erosion(loaded_img1); Erosion();
UpdateImage(); update();
// UpdateImage();
} }
void MyWindow::on_dilation_clicked(){ void MyWindow::on_dilation_clicked(){
Dilation(loaded_img1); Dilation();
UpdateImage(); update();
// UpdateImage();
} }
void MyWindow::on_opening_clicked(){ void MyWindow::on_opening_clicked(){
Open(loaded_img1); Open();
UpdateImage(); update();
// UpdateImage();
} }
void MyWindow::on_closing_clicked(){ void MyWindow::on_closing_clicked(){
Close(loaded_img1); Close();
UpdateImage(); update();
// UpdateImage();
} }
void MyWindow::UpdateImage(){ void MyWindow::UpdateImage(){
@ -172,52 +175,60 @@ bool MyWindow::IsLit(QImage* im, int x, int y, int val){
return pix.red() == val && pix.green() == val && pix.blue() == val; return pix.red() == val && pix.green() == val && pix.blue() == val;
} }
void MyWindow::Erosion(QImage* im){ void MyWindow::Erosion(){
int width = im->width(); int width = img->width();
int height = im->height(); int height = img->height();
memcpy(loaded_img2->bits(), loaded_img1->bits(), width * height * 4); memcpy(img2->bits(), img->bits(), width * height * 4);
for(int x = 0; x < width; x++) for(int x = 0; x < width; x++)
for(int y = 0; y < height; y++){ for(int y = 0; y < height; y++){
bool active = false; bool active = false;
for (int x2 = x - 1; x2 <= x + 1; x2++) for (int x2 = x - 1; x2 <= x + 1; x2++)
for (int y2 = y - 1; y2 <= y + 1; y2++) for (int y2 = y - 1; y2 <= y + 1; y2++)
if(IsLit(loaded_img1, x2, y2, 0)){ if(IsLit(img, x2, y2, 0)){
DrawPixel(loaded_img2, x, y, QColor(0, 0, 0)); active = true;
break; break;
} }
if(active)
DrawPixel(img2, x, y, QColor(0, 0, 0));
} }
memcpy(loaded_img1->bits(), loaded_img2->bits(), width * height * 4); memcpy(img->bits(), img2->bits(), width * height * 4);
} }
void MyWindow::Dilation(QImage* im){ void MyWindow::Dilation(){
int width = im->width(); int width = img->width();
int height = im->height(); int height = img->height();
memcpy(loaded_img2->bits(), loaded_img1->bits(), width * height * 4); memcpy(img2->bits(), img->bits(), width * height * 4);
for(int x = 0; x < width; x++) for(int x = 0; x < width; x++)
for(int y = 0; y < height; y++){ for(int y = 0; y < height; y++){
bool active = false; bool active = false;
for (int x2 = x - 1; x2 <= x + 1; x2++)
for (int y2 = y - 1; y2 <= y + 1; y2++) for (int x2 = x - 1; x2 <= x + 1; x2++){
if(IsLit(loaded_img1, x2, y2, 255)){ for (int y2 = y - 1; y2 <= y + 1; y2++){
DrawPixel(loaded_img2, x, y, QColor(255, 255, 255)); if(IsLit(img, x2, y2, 255)){
break; active = true;
} }
}
}
if(active)
DrawPixel(img2, x, y, QColor(255, 255, 255));
} }
memcpy(loaded_img1->bits(), loaded_img2->bits(), width * height * 4); memcpy(img->bits(), img2->bits(), width * height * 4);
} }
void MyWindow::Close(QImage* im){ void MyWindow::Close(){
Dilation(im); Dilation();
Erosion(im); Erosion();
} }
void MyWindow::Open(QImage* im){ void MyWindow::Open(){
Erosion(im); Erosion();
Dilation(im); Dilation();
} }
QColor MyWindow::GetPixel(QImage* img, int x, int y){ QColor MyWindow::GetPixel(QImage* img, int x, int y){
@ -274,8 +285,6 @@ void MyWindow::paintEvent(QPaintEvent*)
// Obiekt klasy QPainter pozwala nam rysowac na komponentach // Obiekt klasy QPainter pozwala nam rysowac na komponentach
QPainter p(this); QPainter p(this);
UpdateImage();
// Rysuje obrazek "img" w punkcie (poczX,poczY) // Rysuje obrazek "img" w punkcie (poczX,poczY)
// (tu bedzie lewy gorny naroznik) // (tu bedzie lewy gorny naroznik)
p.drawImage(poczX,poczY,*img); p.drawImage(poczX,poczY,*img);

View File

@ -69,6 +69,7 @@ private:
// Pole przechowujace obrazek // Pole przechowujace obrazek
QImage *img; QImage *img;
QImage *img2;
QImage *loaded_img1; QImage *loaded_img1;
QImage *loaded_img2; QImage *loaded_img2;
@ -115,10 +116,10 @@ private:
bool IsLit(QImage* im, int x, int y, int val); bool IsLit(QImage* im, int x, int y, int val);
void Erosion(QImage* im); void Erosion();
void Dilation(QImage* im); void Dilation();
void Close(QImage* im); void Close();
void Open(QImage* im); void Open();
// Deklaracje slotow, czyli funkcji wywolywanych // Deklaracje slotow, czyli funkcji wywolywanych
// po wystapieniu zdarzen zwiazanych z GUI // po wystapieniu zdarzen zwiazanych z GUI