zadanie-8 (canny)

This commit is contained in:
Patrycjusz Mania 2021-06-03 11:49:40 +02:00
parent 7861be11ca
commit 424df7fc49
3 changed files with 126 additions and 7 deletions

View File

@ -50,7 +50,8 @@ PNM* Convolution::convolute(math::matrix<float> mask, Mode mode = RepeatEdge)
int r = getOneChannelPixel(mask, x, y, mode, RChannel, weight);
int g = getOneChannelPixel(mask, x, y, mode, GChannel, weight);
int b = getOneChannelPixel(mask, x, y, mode, BChannel, weight);
newImage->setPixel(x, y, QColor(r,g,b).rgb());
int l = getOneChannelPixel(mask, x, y, mode, LChannel, weight);
newImage->setPixel(x, y, QColor(r,g,b,l).rgb());
}
}

View File

@ -19,12 +19,111 @@ PNM* EdgeCanny::transform()
int width = image->width(),
height = image->height();
int upper_thresh = getParameter("upper_threshold").toInt(),
int upper_thresh = getParameter("upper_threshold").toInt() / 3.33,
lower_thresh = getParameter("lower_threshold").toInt();
PNM* newImage = new PNM(width, height, QImage::Format_Grayscale8);
ConversionGrayscale gray(image);
newImage = gray.transform();
qDebug() << Q_FUNC_INFO << "Not implemented yet!";
BlurGaussian gauss(newImage);
gauss.setParameter("size", 3);
gauss.setParameter("sigma", 1.6);
newImage = gauss.transform();
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 thisAngle = (atan2(Gy,Gx)/3.14159) * 180.0;
float newAngle = 0;
if (((thisAngle < 22.5) && (thisAngle > -22.5)) || (thisAngle > 157.5) || (thisAngle < -157.5))
newAngle = 0;
else if (((thisAngle > 22.5) && (thisAngle < 67.5)) || ((thisAngle < -112.5) && (thisAngle > -157.5)))
newAngle = 45;
else if (((thisAngle > 67.5) && (thisAngle < 112.5)) || ((thisAngle < -67.5) && (thisAngle > -112.5)))
newAngle = 90;
else if (((thisAngle > 112.5) && (thisAngle < 157.5)) || ((thisAngle < -22.5) && (thisAngle > -67.5)))
newAngle = 135;
angles[x][y] = newAngle;
}
}
for (int x=1;x<width-1;++x) {
for (int y=1;y<height-1;++y) {
math::matrix<float> window = getWindow(x,y,3, LChannel, RepeatEdge);
int angle = angles[x][y];
float neighbour1 = 0;
float neighbour2 = 0;
float center = strenghts[x][y];
if (angle == 0) {
neighbour1 = strenghts[x][y-1];
neighbour2 = strenghts[x][y+1];
} else if (angle == 45) {
neighbour1 = strenghts[x-1][y-1];
neighbour2 = strenghts[x+1][y+1];
} else if (angle == 90) {
neighbour1 = strenghts[x-1][y];
neighbour2 = strenghts[x+1][y];
} else if (angle == 135) {
neighbour1 = strenghts[x+1][y+1];
neighbour2 = strenghts[x-1][y-1];
}
if (center > neighbour1 && center > neighbour2 && center > upper_thresh){
center = center > 255 ? 255 : center;
newImage->setPixel(x, y, QColor(center, center, center).rgb());
}else
newImage->setPixel(x, y, QColor(0, 0, 0).rgb());
}
}
int highThresh = upper_thresh * 255 / 100;
int lowThresh = highThresh * lower_thresh / 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;
}

View File

@ -28,18 +28,37 @@ void EdgeSobel::prepareMatrices()
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;
}
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;
}