1
0

Zadanie 11

This commit is contained in:
Jarosław Wieczorek 2021-04-15 20:34:13 +02:00
parent b801fbda96
commit 87f95e8e34
7 changed files with 405 additions and 7 deletions

View File

@ -0,0 +1,83 @@
#include "bin_gradient.h"
using namespace std;
BinarizationGradient::BinarizationGradient(PNM* img) :
Transformation(img)
{
}
BinarizationGradient::BinarizationGradient(PNM* img, ImageViewer* iv) :
Transformation(img, iv)
{
}
PNM* BinarizationGradient::transform()
{
int width = image->width();
int height = image->height();
PNM* newImage = new PNM(width, height, QImage::Format_Mono);
int numerator = 0;
int denominator = 0;
// Gx
int Gx = 0;
// Gy
int Gy = 0;
int G = 0;
int T = 0;
//PNM* newImage = new PNM(width, height, QImage::Format_Mono);
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
Mode mode = Transformation::RepeatEdge;
int I = qGray(getPixel(x, y, mode));
// Get Gx and Gy
Gx = qGray(getPixel(x + 1, y, mode)) - qGray(getPixel(x - 1, y, mode));
Gy = qGray(getPixel(x, y + 1, mode)) - qGray(getPixel(x, y - 1, mode));
if (Gx > Gy)
{
G = Gx;
}
else
{
G = Gy;
}
numerator += I * G;
denominator += G;
}
}
T = numerator / denominator;
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
Mode mode = Transformation::RepeatEdge;
QRgb pixel = getPixel(x, y, mode);
int val = qGray(pixel);
if (val < T)
{
newImage->setPixel(x, y, 0);
}
else
{
newImage->setPixel(x, y, 1);
}
}
}
return newImage;
}

View File

@ -0,0 +1,93 @@
#include "hough.h"
#include "iostream"
#include "conversion_grayscale.h"
#include "edge_laplacian.h"
Hough::Hough(PNM* img) :
Transformation(img)
{
}
Hough::Hough(PNM* img, ImageViewer* super) :
Transformation(img, super)
{
}
PNM* Hough::transform()
{
int width = image->width();
int height = image->height();
int theta_density = getParameter("theta_density").toInt();
bool skip_edge = getParameter("skip_edge_detection").toBool();
std::cout << "skip_edge: " << skip_edge << std::endl;
int theta_size = 180 * theta_density;
double max_ro = sqrt(pow(width, 2) + pow(height, 2));
ConversionGrayscale* gray_scale = new ConversionGrayscale(image);
image = gray_scale->transform();
PNM* newImage = new PNM(theta_size, max_ro * 2 + 1, QImage::Format_Grayscale8);
int new_image_width = newImage->width();
int new_image_height = newImage->height();
math::matrix<float> hough(new_image_width, new_image_height);
if (skip_edge == false)
{
EdgeLaplacian *el = new EdgeLaplacian(image);
el->setParameter("size", 7);
image = el->transform();
}
for (int x = 0; x < new_image_width; x++)
{
for (int y = 0; y < new_image_height; y++)
{
hough[x][y] = 0;
}
}
double theta = 0.0;
double ro = 0;
float max = 0;
double max_z = 0;
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
if (qGray(image->pixel(i, j)) > 0)
{
for (int k = 0; k < theta_size; k++)
{
theta = (k * M_PI) / (theta_density * 180);
ro = i * cos(theta) + j * sin(theta);
hough[k][ro + max_ro]++;
}
}
}
}
max = hough.max();
int z;
for (int x = 0; x < new_image_width; x++)
{
for (int y = 0; y < new_image_height; y++)
{
z = hough[x][y] * (max / 255);
newImage->setPixel(x, y, QColor(z, z, z).rgb());
}
}
std::cout << "max_z: " << max_z << std::endl;
return newImage;
}

View File

@ -0,0 +1,91 @@
#include "hough_lines.h"
#include "bin_gradient.h"
#include "edge_laplacian.h"
#include "hough.h"
#include <iostream>
#include <Qt>
#include <QPainter>
HoughLines::HoughLines(PNM* img) :
Transformation(img)
{
}
HoughLines::HoughLines(PNM* img, ImageViewer* iv) :
Transformation(img, iv)
{
}
PNM* HoughLines::transform()
{
// Cut of value from the image;
int threshold = getParameter("threshold").toInt();
bool drawWholeLines = getParameter("draw_whole_lines").toBool();
int theta_density = 3;
PNM* newImage = new PNM(image->copy());
EdgeLaplacian el(image);
el.setParameter("size", 3);
PNM* image_edge = el.transform();
PNM* image_bin = BinarizationGradient(image_edge).transform();
Hough h(image_edge);
h.setParameter("theta_density", theta_density);
h.setParameter("skip_edge_detection", true);
PNM* image_hough = h.transform();
int hough_width = image_hough->width();
int hough_height = image_hough->height();
int width = image_bin->width();
int height = image_bin->height();
QPainter qPainter (newImage);
qPainter.setPen(Qt::red);
for (int theta = 0; theta < hough_width; theta++)
{
for (int rho = 0; rho < hough_height; rho++)
{
int val = (int) qGray(image_hough->pixel(theta, rho));
if (val > threshold)
{
double rtheta = ((double)theta / 3.0) * M_PI / 180.0;
int rrho = rho - hough_height / 2;
qPainter.drawLine(0, round(rrho / sin(rtheta)), width - 1, round((rrho - (width - 1) * cos(rtheta)) / sin(rtheta)));
}
}
}
int counter_pixels = 0;
if (!drawWholeLines)
{
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
QRgb pixel = image_bin->pixel(x, y);
if (qGray(pixel) == 0)
{
counter_pixels++;
newImage->setPixel(x, y, image->pixel(x, y));
}
}
}
std::cout << "counter " << counter_pixels << std::endl;
}
return newImage;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.8 KiB

View File

@ -1,5 +1,5 @@
#include "hough.h" #include "hough.h"
#include "iostream"
#include "conversion_grayscale.h" #include "conversion_grayscale.h"
#include "edge_laplacian.h" #include "edge_laplacian.h"
@ -15,9 +15,79 @@ Hough::Hough(PNM* img, ImageViewer* super) :
PNM* Hough::transform() PNM* Hough::transform()
{ {
int thetaDensity = getParameter("theta_density").toInt(); int width = image->width();
int height = image->height();
qDebug() << Q_FUNC_INFO << "Not implemented yet!"; int theta_density = getParameter("theta_density").toInt();
bool skip_edge = getParameter("skip_edge_detection").toBool();
return 0; std::cout << "skip_edge: " << skip_edge << std::endl;
int theta_size = 180 * theta_density;
double max_ro = sqrt(pow(width, 2) + pow(height, 2));
ConversionGrayscale* gray_scale = new ConversionGrayscale(image);
image = gray_scale->transform();
PNM* newImage = new PNM(theta_size, max_ro * 2 + 1, QImage::Format_Grayscale8);
int new_image_width = newImage->width();
int new_image_height = newImage->height();
math::matrix<float> hough(new_image_width, new_image_height);
if (skip_edge == false)
{
EdgeLaplacian *el = new EdgeLaplacian(image);
el->setParameter("size", 7);
image = el->transform();
}
for (int x = 0; x < new_image_width; x++)
{
for (int y = 0; y < new_image_height; y++)
{
hough[x][y] = 0;
}
}
double theta = 0.0;
double ro = 0;
float max = 0;
double max_z = 0;
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
if (qGray(image->pixel(i, j)) > 0)
{
for (int k = 0; k < theta_size; k++)
{
theta = (k * M_PI) / (theta_density * 180);
ro = i * cos(theta) + j * sin(theta);
hough[k][ro + max_ro]++;
}
}
}
}
max = hough.max();
int z;
for (int x = 0; x < new_image_width; x++)
{
for (int y = 0; y < new_image_height; y++)
{
z = hough[x][y] * (max / 255);
newImage->setPixel(x, y, QColor(z, z, z).rgb());
}
}
std::cout << "max_z: " << max_z << std::endl;
return newImage;
} }

View File

@ -1,9 +1,10 @@
#include "hough_lines.h" #include "hough_lines.h"
#include "bin_gradient.h" #include "bin_gradient.h"
#include "edge_laplacian.h" #include "edge_laplacian.h"
#include "hough.h"
#include "hough.h"
#include <iostream>
#include <Qt>
#include <QPainter> #include <QPainter>
HoughLines::HoughLines(PNM* img) : HoughLines::HoughLines(PNM* img) :
@ -16,15 +17,75 @@ HoughLines::HoughLines(PNM* img, ImageViewer* iv) :
{ {
} }
PNM* HoughLines::transform() PNM* HoughLines::transform()
{ {
// Cut of value from the image; // Cut of value from the image;
int threshold = getParameter("threshold").toInt(); int threshold = getParameter("threshold").toInt();
bool drawWholeLines = getParameter("draw_whole_lines").toBool(); bool drawWholeLines = getParameter("draw_whole_lines").toBool();
int theta_density = 3;
PNM* newImage = new PNM(image->copy()); PNM* newImage = new PNM(image->copy());
qDebug() << Q_FUNC_INFO << "Not implemented yet!"; EdgeLaplacian el(image);
el.setParameter("size", 3);
PNM* image_edge = el.transform();
PNM* image_bin = BinarizationGradient(image_edge).transform();
Hough h(image_edge);
h.setParameter("theta_density", theta_density);
h.setParameter("skip_edge_detection", true);
PNM* image_hough = h.transform();
int hough_width = image_hough->width();
int hough_height = image_hough->height();
int width = image_bin->width();
int height = image_bin->height();
QPainter qPainter (newImage);
qPainter.setPen(Qt::red);
for (int theta = 0; theta < hough_width; theta++)
{
for (int rho = 0; rho < hough_height; rho++)
{
int val = (int) qGray(image_hough->pixel(theta, rho));
if (val > threshold)
{
double rtheta = ((double)theta / 3.0) * M_PI / 180.0;
int rrho = rho - hough_height / 2;
qPainter.drawLine(0, round(rrho / sin(rtheta)), width - 1, round((rrho - (width - 1) * cos(rtheta)) / sin(rtheta)));
}
}
}
int counter_pixels = 0;
if (!drawWholeLines)
{
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
QRgb pixel = image_bin->pixel(x, y);
if (qGray(pixel) == 0)
{
counter_pixels++;
newImage->setPixel(x, y, image->pixel(x, y));
}
}
}
std::cout << "counter " << counter_pixels << std::endl;
}
return newImage; return newImage;
} }