1
0

Zadanie 7

This commit is contained in:
Jarosław Wieczorek 2021-04-06 11:08:47 +02:00
parent e66815ec89
commit 094dcbc4a3
10 changed files with 503 additions and 16 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 341 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 348 KiB

View File

@ -0,0 +1,28 @@
#include "morph_dilate.h"
MorphDilate::MorphDilate(PNM* img) :
MorphologicalOperator(img)
{
}
MorphDilate::MorphDilate(PNM* img, ImageViewer* iv) :
MorphologicalOperator(img, iv)
{
}
const int MorphDilate::morph(math::matrix<float> window, math::matrix<bool> se)
{
float min = PIXEL_VAL_MAX + 1;
for (int i = 0; i < int(window.colsize()); i++)
{
for (int j = 0; j < int(window.rowsize()); j++)
{
if (se[i][j] == true && window[i][j] < min)
{
min = window[i][j];
}
}
}
return min;
}

View File

@ -0,0 +1,29 @@
#include "morph_erode.h"
MorphErode::MorphErode(PNM* img) :
MorphologicalOperator(img)
{
}
MorphErode::MorphErode(PNM* img, ImageViewer* iv) :
MorphologicalOperator(img, iv)
{
}
const int MorphErode::morph(math::matrix<float> window, math::matrix<bool> se)
{
float max = 0.0;
for (int i = 0; i < int(window.colsize()); i++)
{
for(int j = 0; j < int(window.rowsize()); j++)
{
if(se[i][j] == true && window[i][j] > max)
{
max = window[i][j];
}
}
}
return max;
}

View File

@ -0,0 +1,54 @@
#include "morph_openclose.h"
#include "morph_erode.h"
#include "morph_dilate.h"
MorphOpenClose::MorphOpenClose(PNM* img) :
MorphologicalOperator(img), m_type(Open)
{
}
MorphOpenClose::MorphOpenClose(PNM* img, ImageViewer* iv) :
MorphologicalOperator(img, iv), m_type(Open)
{
}
PNM* MorphOpenClose::transform()
{
int size = getParameter("size").toInt();;
SE shape = (SE) getParameter("shape").toInt();
m_type = (Type) getParameter("type").toInt();
if (m_type == Open)
{
return dilate(erode(image, size, shape), size, shape);
}
else if (m_type == Close)
{
return erode(dilate(image, size, shape), size, shape);
}
else
{
return image;
}
}
PNM* MorphOpenClose::erode(PNM* image, int size, SE shape)
{
MorphErode e(image, getSupervisor());
e.setParameter("silent", true);
e.setParameter("shape", shape);
e.setParameter("size", size);
return e.transform();
}
PNM* MorphOpenClose::dilate(PNM* image, int size, SE shape)
{
MorphDilate e(image, getSupervisor());
e.setParameter("silent", true);
e.setParameter("shape", shape);
e.setParameter("size", size);
return e.transform();
}

View File

@ -0,0 +1,219 @@
#include "morphological_operator.h"
MorphologicalOperator::MorphologicalOperator(PNM* img) :
Transformation(img)
{
}
MorphologicalOperator::MorphologicalOperator(PNM* img, ImageViewer* iv) :
Transformation(img, iv)
{
}
// abstract
const int MorphologicalOperator::morph(math::matrix<float>, math::matrix<bool>)
{
return 0;
}
math::matrix<bool> MorphologicalOperator::getSE(int size, SE shape)
{
switch (shape)
{
case Square: return seSquare(size);
case Cross: return seCross(size);
case XCross: return seXCross(size);
case VLine: return seVLine(size);
case HLine: return seHLine(size);
default: return seSquare(size);
}
}
math::matrix<bool> MorphologicalOperator::seSquare(int size)
{
math::matrix<bool> ret(size, size);
// set true in each field
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
ret[i][j] = true;
}
}
return ret;
}
math::matrix<bool> MorphologicalOperator::seCross(int size)
{
math::matrix<bool> ret(size, size);
int half = size / 2;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (i == half || j == half)
{
ret[i][j] = true;
}
else
{
ret[i][j] = false;
}
}
}
return ret;
}
math::matrix<bool> MorphologicalOperator::seXCross(int size)
{
math::matrix<bool> ret(size, size);
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (i == j)
{
ret[i][j] = true;
}
else
{
ret[i][j] = false;
}
}
}
return ret;
}
math::matrix<bool> MorphologicalOperator::seVLine(int size)
{
math::matrix<bool> ret(size, size);
int half = size / 2;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (j == half)
{
ret[i][j] = true;
}
else
{
ret[i][j] = false;
}
}
}
return ret;
}
math::matrix<bool> MorphologicalOperator::seHLine(int size)
{
math::matrix<bool> ret(size, size);
int half = size / 2;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (i == half)
{
ret[i][j] = true;
}
else
{
ret[i][j] = false;
}
}
}
return ret;
}
PNM* MorphologicalOperator::transform()
{
int size = getParameter("size").toInt();
SE shape = (MorphologicalOperator::SE) getParameter("shape").toInt();
PNM* newImage = new PNM(image->width(), image->height(), QImage::Format_RGB32);
int width = image->width();
int height = image->height();
int half = int(size / 2);
if (image->format() == QImage::Format_Mono)
{
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
math::matrix<float> L_matrix = getWindow(x, y, size, LChannel, RepeatEdge);
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
L_matrix[i][j] = qGray(getPixel((x - half) + i, (y - half) + j, RepeatEdge));
}
}
int l = morph(L_matrix, getSE(size, shape));
newImage->setPixel(x, y, l);
}
}
}
else
{
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
math::matrix<float> R_matrix = getWindow(x, y, size, RChannel, RepeatEdge);
math::matrix<float> G_matrix = getWindow(x, y, size, GChannel, RepeatEdge);
math::matrix<float> B_matrix = getWindow(x, y, size, BChannel, RepeatEdge);
QRgb pixel = image->pixel(x,y);
int r = qRed(pixel);
int g = qGreen(pixel);
int b = qBlue(pixel);
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
R_matrix[i][j] = qRed(getPixel((x - half) + j, (y - half) + i, RepeatEdge));
B_matrix[i][j] = qBlue(getPixel((x - half) + j, (y - half) + i, RepeatEdge));
G_matrix[i][j] = qGreen(getPixel((x - half) + j, (y - half) + i, RepeatEdge));
}
}
r = morph(R_matrix, getSE(size, shape));
g = morph(G_matrix, getSE(size, shape));
b = morph(B_matrix, getSE(size, shape));
QColor newPixel = QColor(r,g,b);
newImage->setPixel(x, y, newPixel.rgb());
}
}
}
return newImage;
}

View File

@ -12,9 +12,17 @@ MorphDilate::MorphDilate(PNM* img, ImageViewer* iv) :
const int MorphDilate::morph(math::matrix<float> window, math::matrix<bool> se)
{
float min = PIXEL_VAL_MAX+1;
float min = PIXEL_VAL_MAX + 1;
qDebug() << Q_FUNC_INFO << "Not implemented yet!";
return 0;
for (int i = 0; i < int(window.colsize()); i++)
{
for (int j = 0; j < int(window.rowsize()); j++)
{
if (se[i][j] == true && window[i][j] < min)
{
min = window[i][j];
}
}
}
return min;
}

View File

@ -12,9 +12,18 @@ MorphErode::MorphErode(PNM* img, ImageViewer* iv) :
const int MorphErode::morph(math::matrix<float> window, math::matrix<bool> se)
{
float max=0.0;
float max = 0.0;
qDebug() << Q_FUNC_INFO << "Not implemented yet!";
for (int i = 0; i < int(window.colsize()); i++)
{
for(int j = 0; j < int(window.rowsize()); j++)
{
if(se[i][j] == true && window[i][j] > max)
{
max = window[i][j];
}
}
}
return 0;
return max;
}

View File

@ -19,9 +19,18 @@ PNM* MorphOpenClose::transform()
SE shape = (SE) getParameter("shape").toInt();
m_type = (Type) getParameter("type").toInt();
qDebug() << Q_FUNC_INFO << "Not implemented yet!";
return 0;
if (m_type == Open)
{
return dilate(erode(image, size, shape), size, shape);
}
else if (m_type == Close)
{
return erode(dilate(image, size, shape), size, shape);
}
else
{
return image;
}
}
PNM* MorphOpenClose::erode(PNM* image, int size, SE shape)

View File

@ -34,7 +34,14 @@ math::matrix<bool> MorphologicalOperator::seSquare(int size)
{
math::matrix<bool> ret(size, size);
qDebug() << Q_FUNC_INFO << "Not implemented yet!";
// set true in each field
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
ret[i][j] = true;
}
}
return ret;
}
@ -43,7 +50,23 @@ math::matrix<bool> MorphologicalOperator::seCross(int size)
{
math::matrix<bool> ret(size, size);
qDebug() << Q_FUNC_INFO << "Not implemented yet!";
int half = size / 2;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (i == half || j == half)
{
ret[i][j] = true;
}
else
{
ret[i][j] = false;
}
}
}
return ret;
}
@ -52,7 +75,21 @@ math::matrix<bool> MorphologicalOperator::seXCross(int size)
{
math::matrix<bool> ret(size, size);
qDebug() << Q_FUNC_INFO << "Not implemented yet!";
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (i == j)
{
ret[i][j] = true;
}
else
{
ret[i][j] = false;
}
}
}
return ret;
}
@ -61,7 +98,23 @@ math::matrix<bool> MorphologicalOperator::seVLine(int size)
{
math::matrix<bool> ret(size, size);
qDebug() << Q_FUNC_INFO << "Not implemented yet!";
int half = size / 2;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (j == half)
{
ret[i][j] = true;
}
else
{
ret[i][j] = false;
}
}
}
return ret;
}
@ -70,7 +123,24 @@ math::matrix<bool> MorphologicalOperator::seHLine(int size)
{
math::matrix<bool> ret(size, size);
qDebug() << Q_FUNC_INFO << "Not implemented yet!";
int half = size / 2;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (i == half)
{
ret[i][j] = true;
}
else
{
ret[i][j] = false;
}
}
}
return ret;
}
@ -82,7 +152,68 @@ PNM* MorphologicalOperator::transform()
PNM* newImage = new PNM(image->width(), image->height(), QImage::Format_RGB32);
qDebug() << Q_FUNC_INFO << "Not implemented yet!";
int width = image->width();
int height = image->height();
int half = int(size / 2);
if (image->format() == QImage::Format_Mono)
{
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
math::matrix<float> L_matrix = getWindow(x, y, size, LChannel, RepeatEdge);
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
L_matrix[i][j] = qGray(getPixel((x - half) + i, (y - half) + j, RepeatEdge));
}
}
int l = morph(L_matrix, getSE(size, shape));
newImage->setPixel(x, y, l);
}
}
}
else
{
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
math::matrix<float> R_matrix = getWindow(x, y, size, RChannel, RepeatEdge);
math::matrix<float> G_matrix = getWindow(x, y, size, GChannel, RepeatEdge);
math::matrix<float> B_matrix = getWindow(x, y, size, BChannel, RepeatEdge);
QRgb pixel = image->pixel(x,y);
int r = qRed(pixel);
int g = qGreen(pixel);
int b = qBlue(pixel);
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
R_matrix[i][j] = qRed(getPixel((x - half) + j, (y - half) + i, RepeatEdge));
B_matrix[i][j] = qBlue(getPixel((x - half) + j, (y - half) + i, RepeatEdge));
G_matrix[i][j] = qGreen(getPixel((x - half) + j, (y - half) + i, RepeatEdge));
}
}
r = morph(R_matrix, getSE(size, shape));
g = morph(G_matrix, getSE(size, shape));
b = morph(B_matrix, getSE(size, shape));
QColor newPixel = QColor(r,g,b);
newImage->setPixel(x, y, newPixel.rgb());
}
}
}
return newImage;
}