Compare commits

...

5 Commits

Author SHA1 Message Date
Jonasz Świtała
b11f6cfcdb Zadanie_12_Jonasz 2020-05-24 20:26:35 +02:00
Dawid_Kreft
33a7f9b2af Zadanie 11 - lines 2020-05-18 00:12:00 +02:00
Dawid_Kreft
354eba6479 Zad_11_ 2020-05-17 23:23:09 +02:00
Dawid_Kreft
0aba7c500d Zad_11_ 2020-05-17 23:21:17 +02:00
e0c1938fcc Merge branch 'Dawid_zad_9' of s452126/lpo-projekt2020 into master 2020-05-11 21:25:53 +00:00
9 changed files with 231 additions and 15 deletions

View File

@ -17,8 +17,42 @@ PNM* BinarizationGradient::transform()
PNM* newImage = new PNM(width, height, QImage::Format_Mono); PNM* newImage = new PNM(width, height, QImage::Format_Mono);
qDebug() << Q_FUNC_INFO << "Not implemented yet!"; int Gmax = 0;
int numerator = 0;
int denominator = 0;
for(int x=1; x<width-1; x++){
for(int y=1; y<height-1; y++){
int t = (qRed(image->pixel(x,y)) + qGreen(image->pixel(x,y)) + qBlue(image->pixel(x,y))) / 3;
int Gxa = (qRed(image->pixel(x-1,y)) + qGreen(image->pixel(x-1,y)) + qBlue(image->pixel(x-1,y))) / 3;
int Gxb = (qRed(image->pixel(x+1,y)) + qGreen(image->pixel(x+1,y)) + qBlue(image->pixel(x+1,y))) / 3;
int Gya = (qRed(image->pixel(x,y+1)) + qGreen(image->pixel(x,y+1)) + qBlue(image->pixel(x,y+1))) / 3;
int Gyb= (qRed(image->pixel(x,y-1)) + qGreen(image->pixel(x,y-1)) + qBlue(image->pixel(x,y-1))) / 3;
int Gx = Gxa - Gxb;
int Gy = Gya - Gyb;
if (Gx > Gy) Gmax = Gx;
else Gmax = Gy;
numerator += t * Gmax;
denominator += Gmax;
}
}
int T = numerator/denominator;
// create new image
for(int i=0; i<width; i++){
for(int j=0; j<height; j++){
QRgb pixel = image->pixel(i,j);
int tempValue = (qRed(pixel) + qGreen(pixel) + qBlue(pixel)) / 3;
if ( tempValue > T ){
newImage->setPixel(i, j, Qt::color1);
}
else {
newImage->setPixel(i, j, Qt::color0);
}
}
}
return newImage; return newImage;
} }

View File

@ -41,9 +41,8 @@ math::matrix<float> BlurGaussian::getMask(int size, Mode)
} }
float BlurGaussian::getGauss(int x, int y, float sigma) float BlurGaussian::getGauss(int x, int y, float sigma)
{ {
float gauss= exp(-(x*x+y*y)/(2*sigma*sigma))/(2*M_PI*sigma*sigma); float gauss= exp(-(x*x+y*y)/(2*sigma*sigma))/(2*M_PI*sigma*sigma);
return gauss; return gauss;
} }

View File

@ -137,7 +137,7 @@ const math::matrix<float> Convolution::join(math::matrix<float> A, math::matrix<
for (int x=0; x<size; x++) for (int x=0; x<size; x++)
for (int y=0; y<size; y++) for (int y=0; y<size; y++)
{ {
C[x][y]=A[x][y]*B[x][y]; C[x][y]=A[x][y]*B[x][y];
} }
@ -181,4 +181,5 @@ const math::matrix<float> Convolution::reflection(const math::matrix<float> A)
} }
return C; return C;
} }

View File

@ -24,9 +24,115 @@ PNM* CornerHarris::transform()
int width = image->width(), int width = image->width(),
height = image->height(); height = image->height();
//Krok1 Zdef i wypelij zerami
math::matrix<float> Ixx(width, height);
math::matrix<float> Iyy(width, height);
math::matrix<float> Ixy(width, height);
math::matrix<float> CC(width, height);
math::matrix<float> CNS(width, height);
for (int x=0; x<width; x++)
for (int y=0; y<height; y++)
{
Ixx[x][y]=0;
Iyy[x][y]=0;
Ixy[x][y]=0;
CC[x][y]=0;
CNS[x][y]=0;
}
//Krok 2 skala szarosci
ConversionGrayscale cg(image);
PNM* gray_image=cg.transform();
//Krok 3 rozmyj
BlurGaussian bg(gray_image);
bg.setParameter("size",3);
bg.setParameter("sigma",1.6);
PNM* blur_image=bg.transform();
//Krok 4 sobel
EdgeSobel es(blur_image);
math::matrix<float> *Gx=es.rawHorizontalDetection();
math::matrix<float> *Gy=es.rawVerticalDetection();
//Krok 5 wzorek
for (int x=0; x<width; x++)
for (int y=0; y<height; y++)
{
Ixx[x][y]=(*Gx)[x][y] * (*Gx)[x][y];
Iyy[x][y]=(*Gy)[x][y] * (*Gy)[x][y];
Ixy[x][y]=(*Gx)[x][y] * (*Gy)[x][y];
}
//Krok 6 wzorek
for (int x=1; x<width-1; x++)
for (int y=1; y<height-1; y++)
{
float Sxx=0;
float Syy=0;
float Sxy=0;
math::matrix<float> H(2, 2);
for(int z=-1;z<=1;z++) //Znacznik sumy
for(int a=-1;a<=1;a++)
{
Sxx+=Ixx[x+z][y+a]*BlurGaussian::getGauss(z,a,sigma);
Syy+=Iyy[x+z][y+a]*BlurGaussian::getGauss(z,a,sigma);
Sxy+=Ixy[x+z][y+a]*BlurGaussian::getGauss(z,a,sigma);
}
Sxx=Sxx/sigma_weight;
Syy=Syy/sigma_weight;
Sxy=Sxy/sigma_weight;
H[0][0]=Sxx;
H[0][1]=Sxy;
H[1][0]=Sxy;
H[1][1]=Syy;
float detH=H[0][0]*H[1][1]-H[0][1]*H[1][0];
//Wyznaczkik + suma po przekatnej kwadratowej
float r = detH - k_param * pow(H[0][0] + H[1][1], 2);
if(r>threshold) CC[x][y]=r;
}
bool search=1;
//Krok 8
while(search==1)
{
search=0;
for (int x=1; x<width-1; x++)
for (int y=1; y<height-1; y++)
{
float max=CC[x][y];
if(max > CC[x][y-1] && max > CC[x][y+1] && max > CC[x+1][y-1] && max > CC[x+1][y] && max > CC[x+1][y+1] && max > CC[x-1][y-1] && max > CC[x-1][y] && max > CC[x-1][y+1])
CNS[x][y]=CC[x][y];
else {
if(CC[x][y]>0)
{
CNS[x][y]=0;
search=1;
}
}
}
CC=CNS;
}
PNM* newImage = new PNM(width, height, QImage::Format_Mono); PNM* newImage = new PNM(width, height, QImage::Format_Mono);
qDebug() << Q_FUNC_INFO << "Not implemented yet!"; //Krok 9
for (int x=1; x<width-1; x++)
for (int y=1; y<height-1; y++)
{
if(CC[x][y]==0) newImage->setPixel(x,y,0);
else newImage->setPixel(x,y,1);
}
return newImage; return newImage;
} }

View File

@ -10,9 +10,12 @@ EdgeLaplacian::EdgeLaplacian(PNM* img, ImageViewer* iv) :
{ {
} }
math::matrix<float> EdgeLaplacian::getMask(int, Mode) math::matrix<float> EdgeLaplacian::getMask(int size , Mode)
{ {
int size = getParameter("size").toInt();
if( getParameter("size").toInt() > size )
size =getParameter("size").toInt();
math::matrix<float> mask(size, size); math::matrix<float> mask(size, size);
int r=size/2; int r=size/2;

View File

@ -12,11 +12,14 @@ EdgeLaplaceOfGauss::EdgeLaplaceOfGauss(PNM* img, ImageViewer* iv) :
{ {
} }
math::matrix<float> EdgeLaplaceOfGauss::getMask(int, Mode) math::matrix<float> EdgeLaplaceOfGauss::getMask(int size, Mode)
{ {
size = getParameter("size").toInt(); if( getParameter("size").toInt() > size )
size =getParameter("size").toInt();
double sigma = getParameter("sigma").toDouble(); double sigma = getParameter("sigma").toDouble();
math::matrix<float> mask(size, size); math::matrix<float> mask(size, size);
int r=size/2; int r=size/2;

View File

@ -51,7 +51,7 @@ math::matrix<float>* EdgeSobel::rawHorizontalDetection()
for (int y = 0; y < height; y++) for (int y = 0; y < height; y++)
{ {
math::matrix<float> temp = getWindow(x, y, 3, LChannel, NullEdge); math::matrix<float> temp = getWindow(x, y, 3, LChannel, NullEdge);
x_gradient[x][y] = Convolution::sum(Convolution::join(g_x, temp)); (*x_gradient)(x, y) = Convolution::sum(Convolution::join(g_x, temp));
} }
} }
@ -71,7 +71,7 @@ math::matrix<float>* EdgeSobel::rawVerticalDetection()
for (int y = 0; y < height; y++) for (int y = 0; y < height; y++)
{ {
math::matrix<float> temp = getWindow(x, y, 3, LChannel, NullEdge); math::matrix<float> temp = getWindow(x, y, 3, LChannel, NullEdge);
y_gradient[x][y] = Convolution::sum(Convolution::join(g_x, temp)); (*y_gradient)(x, y) = Convolution::sum(Convolution::join(g_x, temp));
} }
} }

View File

@ -17,7 +17,50 @@ PNM* Hough::transform()
{ {
int thetaDensity = getParameter("theta_density").toInt(); int thetaDensity = getParameter("theta_density").toInt();
qDebug() << Q_FUNC_INFO << "Not implemented yet!"; int width = image->width();
int height = image->height();
PNM* tempImage = new PNM(width, height, QImage::Format_Indexed8);
tempImage = ConversionGrayscale(image).transform();
return 0; if (!getParameter("skip_edge_detection").toBool()) {
tempImage = EdgeLaplacian(tempImage).transform();
}
int pmax =int(sqrt(height*height + width*width));
int thetaSize= 180 * thetaDensity;
int lenght = pmax * 2 + 1;
double theta, p;
int max = 0;
math::matrix<int> hough(thetaSize,lenght);
for (int i = 0; i < thetaSize; i++) {
for (int j = 0 ; j < lenght ; j++) {
hough[i][j] = 0;
}
}
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
if (qGray(tempImage->pixel(i, j)) > 0) {
for (int k = 0; k < thetaSize; k++) {
theta = (k * 3.14) / (180 * thetaDensity);
p = i*cos(theta) + j*sin(theta);
hough[k][p+pmax]++;
if (hough[k][p+pmax] > max) {
max = hough[k][p+pmax];
}
}
}
}
}
PNM* newImage = new PNM(thetaSize,lenght, QImage::Format_Grayscale8);
for (int i = 0; i < thetaSize; i++) {
for (int j = 0; j < lenght; j++) {
int value = int(((hough[i][j] * 255)/ max));
newImage->setPixel(i, j, QColor(value,value,value).rgb());
}
}
return newImage;
} }

View File

@ -24,7 +24,34 @@ PNM* HoughLines::transform()
PNM* newImage = new PNM(image->copy()); PNM* newImage = new PNM(image->copy());
qDebug() << Q_FUNC_INFO << "Not implemented yet!"; // step 1
EdgeLaplacian* edgeLA = new EdgeLaplacian(image);
edgeLA->setParameter("size", 3);
PNM* tempImage = edgeLA->transform();
// step 2
BinarizationGradient* binarizationGradient = new BinarizationGradient(tempImage);
PNM* binImage = binarizationGradient->transform();
// step 3
Hough* hough = new Hough(binImage);
hough->setParameter("theta_density", 3);
hough->setParameter("skip_edge_detection", true);
tempImage = hough->transform();
// step 4
QPainter p(newImage);
p.setPen(Qt::red);
for(int i=0; i < tempImage->width(); i++) {
for(int j=0; j < tempImage->height(); j++) {
if(qGray(tempImage->pixel(i, j)) > threshold) {
double rtheta = ((double)i/3.0)*M_PI/180.0;
int rrho = j - tempImage->height()/2;
p.drawLine(0, round(rrho/sin(rtheta)), newImage->width()-1, round((rrho - (newImage->width()-1)*cos(rtheta))/sin(rtheta)));
}
if (!drawWholeLines == true){
if (qRed(binImage->pixel(i, j)) == 0){
newImage->setPixel(i, j, image->pixel(i, j));
}
}
}
}
return newImage; return newImage;
} }