zadania 8 i 9

This commit is contained in:
BohdanBakhlul 2021-06-11 20:38:59 +02:00
parent b20080e25e
commit c61aff0e9a
3 changed files with 197 additions and 7 deletions

View File

@ -22,9 +22,128 @@ PNM* EdgeCanny::transform()
int upper_thresh = getParameter("upper_threshold").toInt(), int upper_thresh = getParameter("upper_threshold").toInt(),
lower_thresh = getParameter("lower_threshold").toInt(); lower_thresh = getParameter("lower_threshold").toInt();
PNM* newImage = new PNM(width, height, QImage::Format_Grayscale8); PNM* newImage = new PNM(width, height, QImage::Format_Grayscale8);
qDebug() << Q_FUNC_INFO << "Not implemented yet!"; //TO GRAY
ConversionGrayscale gray(image);
newImage = gray.transform();
//BLUR
BlurGaussian gauss(newImage);
gauss.setParameter("size", 3);
gauss.setParameter("sigma", 1.6);
newImage = gauss.transform();
//SOBEL DETECTION
EdgeSobel sobel(newImage);
math::matrix<float>* horizontal = sobel.rawHorizontalDetection();
math::matrix<float>* vertical = sobel.rawVerticalDetection();
math::matrix<float> strenghts(width, height);
math::matrix<int> angles(width, height);
for (int x=0;x<width;++x) {
for (int y=0;y<height;++y) {
float Gx = (*horizontal)[x][y];
float Gy = (*vertical)[x][y];
strenghts[x][y] = sqrt( pow(Gx,2.0) + pow(Gy,2.0) );
float angle = (atan2(Gy,Gx)/3.14159) * 180.0;
float a = 0;
if (((angle < 22.5) && (angle > -22.5)) || (angle > 157.5) || (angle < -157.5))
a = 0;
else if (((angle > 22.5) && (angle < 67.5)) || ((angle < -112.5) && (angle > -157.5)))
a = 1;
else if (((angle > 67.5) && (angle < 112.5)) || ((angle < -67.5) && (angle > -112.5)))
a = 2;
else if (((angle > 112.5) && (angle < 157.5)) || ((angle < -22.5) && (angle > -67.5)))
a = 3;
angles[x][y] = a;
}
}
for (int x=1;x<width-1;++x) {
for (int y=1;y<height-1;++y) {
int angle = angles[x][y];
float n1 = 0;
float n2 = 0;
float n = strenghts[x][y];
if (angle == 0) {
n1 = strenghts[x][y-1];
n2 = strenghts[x][y+1];
} else if (angle == 1) {
n1 = strenghts[x-1][y-1];
n2 = strenghts[x+1][y+1];
} else if (angle == 2) {
n1 = strenghts[x-1][y];
n2 = strenghts[x+1][y];
} else if (angle == 3) {
n1 = strenghts[x+1][y+1];
n2 = strenghts[x-1][y-1];
}
if (n > n1 && n > n2 && n > upper_thresh / 3.33){
n = n > 255 ? 255 : n;
newImage->setPixel(x, y, QColor(n, n, n).rgb());
} else
newImage->setPixel(x, y, QColor(0, 0, 0).rgb());
}
}
int highThresh = (upper_thresh / 3.33) * 255 / 100;
int lowThresh = lower_thresh * 255 / 100;
unsigned int high = QColor(highThresh,highThresh,highThresh).rgb();
unsigned int low = QColor(lowThresh,lowThresh,lowThresh).rgb();
unsigned int weak = QColor(128,128,128).rgb();
unsigned int strong = QColor(255,255,255).rgb();
for (int x=1;x<width-1;++x) {
for (int y=1;y<height-1;++y) {
if (newImage->pixel(x,y) >= high) {
newImage->setPixel(x, y, strong);
}
else if (newImage->pixel(x,y) < high && newImage->pixel(x,y) >= low) {
newImage->setPixel(x, y, weak);
}
else {
newImage->setPixel(x, y, QColor(0, 0, 0).rgb());
}
unsigned int white = QColor(255, 255, 255).rgb();
if (newImage->pixel(x, y) == weak) {
if (
(newImage->pixel(x+1, y-1) == white) ||
(newImage->pixel(x+1, y) == white) ||
(newImage->pixel(x+1, y+1) == white) ||
(newImage->pixel(x, y-1) == white) ||
(newImage->pixel(x, y+1) == white) ||
(newImage->pixel(x+1, y-1) == white) ||
(newImage->pixel(x-1, y-1) == white) ||
(newImage->pixel(x-1, y) == white) ||
(newImage->pixel(x-1, y+1) == white)
)
newImage->setPixel(x, y, QColor(255, 255, 255).rgb());
else
newImage->setPixel(x, y, QColor(0, 0, 0).rgb());
}
}
}
return newImage; return newImage;
} }

View File

@ -23,18 +23,37 @@ void EdgeSobel::prepareMatrices()
math::matrix<float>* EdgeSobel::rawHorizontalDetection() math::matrix<float>* EdgeSobel::rawHorizontalDetection()
{ {
math::matrix<float>* x_gradient = new math::matrix<float>(this->image->width(), this->image->height()); int width = image->width();
int height = image->height();
math::matrix<float>* x_gradient = new math::matrix<float>(width, height);
qDebug() << Q_FUNC_INFO << "Not implemented yet!"; PNM* newImage = convolute(g_x, RepeatEdge);
for (int x=0;x<width;++x) {
for (int y=0;y<height;++y) {
(*x_gradient)[x][y] = qGray(newImage->pixel(x,y));
}
}
delete newImage;
return x_gradient; return x_gradient;
} }
math::matrix<float>* EdgeSobel::rawVerticalDetection() math::matrix<float>* EdgeSobel::rawVerticalDetection()
{ {
math::matrix<float>* y_gradient = new math::matrix<float>(this->image->width(), this->image->height()); int width = image->width();
int height = image->height();
math::matrix<float>* y_gradient = new math::matrix<float>(width, height);
qDebug() << Q_FUNC_INFO << "Not implemented yet!";
PNM* newImage = convolute(g_y, RepeatEdge);
for (int x=0;x<width;++x) {
for (int y=0;y<height;++y) {
(*y_gradient)[x][y] = qGray(newImage->pixel(x,y));
}
}
delete newImage;
return y_gradient; return y_gradient;
} }

View File

@ -2,6 +2,7 @@
#include "conversion_grayscale.h" #include "conversion_grayscale.h"
#include "edge_laplacian.h" #include "edge_laplacian.h"
#include <math.h>
Hough::Hough(PNM* img) : Hough::Hough(PNM* img) :
Transformation(img) Transformation(img)
@ -16,8 +17,59 @@ Hough::Hough(PNM* img, ImageViewer* super) :
PNM* Hough::transform() PNM* Hough::transform()
{ {
int thetaDensity = getParameter("theta_density").toInt(); int thetaDensity = getParameter("theta_density").toInt();
bool skip_edge = getParameter("skip_edge_detection").toBool();
qDebug() << Q_FUNC_INFO << "Not implemented yet!"; ConversionGrayscale gray(image);
PNM* newImage = new PNM(image->width(), image->height(), QImage::Format_Grayscale8);
newImage = gray.transform();
if (skip_edge == false) {
EdgeLaplacian laplacian(newImage);
laplacian.setParameter("size", 5);
newImage = laplacian.transform();
}
float diag = sqrt( pow(image->width(), 2) + pow(image->height(), 2) );
float thetaSize = 180 * thetaDensity;
int width = thetaSize;
int height = (diag * 2) + 1;
PNM* resultImage = new PNM(width, height, QImage::Format_Grayscale8);
math::matrix<float> houghtMatrix(width, height, 0.0);
math::matrix<float> theta(width, height);
for (int x=0;x<image->width();++x) {
for (int y=0;y<image->height();++y) {
if (qGray(newImage->pixel(x,y)) > 0) {
for (int k = 0; k < thetaSize; ++k) {
float theta = (k * M_PI) / (thetaDensity * 180.0);
float p = (x * cos(theta)) + (y * sin(theta));
int second = diag + p;
houghtMatrix[k][second] += 1;
}
}
}
}
delete newImage;
float maxHough = 255 / houghtMatrix.max();
for (int x=0;x<width;++x) {
for (int y=0;y<height;++y) {
houghtMatrix[x][y] *= maxHough;
resultImage->setPixel(x,
y,
QColor(houghtMatrix[x][y],
houghtMatrix[x][y],
houghtMatrix[x][y]
).rgb());
}
}
return resultImage;
return 0;
} }