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
// (0xffRRGGBB).
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_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));
}
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();
}
@ -133,23 +132,27 @@ void MyWindow::on_exitButton_clicked()
}
void MyWindow::on_erosion_clicked(){
Erosion(loaded_img1);
UpdateImage();
Erosion();
update();
// UpdateImage();
}
void MyWindow::on_dilation_clicked(){
Dilation(loaded_img1);
UpdateImage();
Dilation();
update();
// UpdateImage();
}
void MyWindow::on_opening_clicked(){
Open(loaded_img1);
UpdateImage();
Open();
update();
// UpdateImage();
}
void MyWindow::on_closing_clicked(){
Close(loaded_img1);
UpdateImage();
Close();
update();
// 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;
}
void MyWindow::Erosion(QImage* im){
int width = im->width();
int height = im->height();
void MyWindow::Erosion(){
int width = img->width();
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 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(loaded_img1, x2, y2, 0)){
DrawPixel(loaded_img2, x, y, QColor(0, 0, 0));
if(IsLit(img, x2, y2, 0)){
active = true;
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){
int width = im->width();
int height = im->height();
void MyWindow::Dilation(){
int width = img->width();
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 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(loaded_img1, x2, y2, 255)){
DrawPixel(loaded_img2, x, y, QColor(255, 255, 255));
break;
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(loaded_img1->bits(), loaded_img2->bits(), width * height * 4);
memcpy(img->bits(), img2->bits(), width * height * 4);
}
void MyWindow::Close(QImage* im){
Dilation(im);
Erosion(im);
void MyWindow::Close(){
Dilation();
Erosion();
}
void MyWindow::Open(QImage* im){
Erosion(im);
Dilation(im);
void MyWindow::Open(){
Erosion();
Dilation();
}
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
QPainter p(this);
UpdateImage();
// Rysuje obrazek "img" w punkcie (poczX,poczY)
// (tu bedzie lewy gorny naroznik)
p.drawImage(poczX,poczY,*img);

View File

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