From 419c01cf5968fd76e4f3e0c7787cecebcac7278e Mon Sep 17 00:00:00 2001 From: Dawid_Kreft Date: Mon, 6 Apr 2020 23:35:54 +0200 Subject: [PATCH 1/3] Zadanie_06_Dawid --- src/core/transformations/convolution.cpp | 26 ++++++++ src/core/transformations/noise_bilateral.cpp | 63 +++++++++++++++++--- src/core/transformations/noise_median.cpp | 47 ++++++++++++++- 3 files changed, 124 insertions(+), 12 deletions(-) diff --git a/src/core/transformations/convolution.cpp b/src/core/transformations/convolution.cpp index 71864b7..af164c6 100644 --- a/src/core/transformations/convolution.cpp +++ b/src/core/transformations/convolution.cpp @@ -35,6 +35,9 @@ PNM* Convolution::convolute(math::matrix mask, Mode mode = RepeatEdge) PNM* newImage = new PNM(width, height, image->format()); + + + qDebug() << Q_FUNC_INFO << "Not implemented yet!"; return newImage; @@ -50,6 +53,15 @@ const math::matrix Convolution::join(math::matrix A, math::matrix< qDebug() << Q_FUNC_INFO << "Not implemented yet!"; + + for(int x = 0; x < size ; x++){ + for(int y = 0; y < size ; y++){ + C(x,y) = A(x,y) * B(x,y); + + } + + } + return C; } @@ -58,8 +70,22 @@ const float Convolution::sum(const math::matrix A) { float sum = 0.0; + int size = A.rowno(); + + float t = 4.0; + ; + for(int x = 0; x < (int) A.size() ; x++){ + + + } + + + qDebug() << Q_FUNC_INFO << "Not implemented yet!"; + + + return sum; } diff --git a/src/core/transformations/noise_bilateral.cpp b/src/core/transformations/noise_bilateral.cpp index cc6579f..5db5562 100644 --- a/src/core/transformations/noise_bilateral.cpp +++ b/src/core/transformations/noise_bilateral.cpp @@ -21,28 +21,73 @@ PNM* NoiseBilateral::transform() sigma_r = getParameter("sigma_r").toInt(); radius = sigma_d; - qDebug() << Q_FUNC_INFO << "Not implemented yet!"; + for(int x = 0 ; x < width; x++){ + for (int y = 0 ; y < height; y++) { + + int g = calcVal(x,y, GChannel); + int r = calcVal(x,y, RChannel); + int b = calcVal(x,y, BChannel); + newImage->setPixel(x,y,QColor(r,g,b).rgba()); + } + } return newImage; } int NoiseBilateral::calcVal(int x, int y, Channel channel) { - qDebug() << Q_FUNC_INFO << "Not implemented yet!"; + double sumNumerator = 0; + double sumDenominator = 0; - return 0; + double colorClosenes = 0; + double spatialClosennes = 0; + int colorValue; + int colorValueRef; + + for ( int i = x- radius ; i <= x+radius ; i++) { + for ( int j = y- radius ; j <= y+radius ; j++) { + + if ( i+radius >= image->width() || + i-radius < 0 || + j+radius >= image->height() || + j-radius < 0 ) { + continue; + } + + if (channel==LChannel){ + colorValue = qAlpha(image->pixel(i,j)); + colorValueRef = qAlpha(image->pixel(x,y)); + } + if (channel==RChannel){ + colorValue = (qRed(image->pixel(i,j))); + colorValueRef = qRed(image->pixel(x,y)); + } + if (channel==GChannel){ + colorValueRef = qGreen(image->pixel(x,y)); + colorValue = (qGreen(image->pixel(i,j))); + } + if (channel==BChannel){ + colorValueRef = qBlue(image->pixel(x,y)); + colorValue = ( qBlue(image->pixel(i,j))); + } + + colorClosenes = colorCloseness(colorValue,colorValueRef); + spatialClosennes = spatialCloseness( QPoint(i,j),QPoint(x,y)); + + sumNumerator = sumNumerator +( colorValue* colorClosenes * spatialClosennes); + sumDenominator =sumDenominator + ( colorClosenes * spatialClosennes); + + } + } + return sumNumerator/sumDenominator; } float NoiseBilateral::colorCloseness(int val1, int val2) { - qDebug() << Q_FUNC_INFO << "Not implemented yet!"; - - return 0; + return exp(-(((val1-val2)^2)/(2*sigma_r^2))); } float NoiseBilateral::spatialCloseness(QPoint point1, QPoint point2) { - qDebug() << Q_FUNC_INFO << "Not implemented yet!"; - - return 0; + return exp(-((point1.x() - point2.x())^2 + ( point1.y() - point2.y())^2)/(2*sigma_d^2)); } diff --git a/src/core/transformations/noise_median.cpp b/src/core/transformations/noise_median.cpp index 1d8a9f7..0913853 100644 --- a/src/core/transformations/noise_median.cpp +++ b/src/core/transformations/noise_median.cpp @@ -17,16 +17,57 @@ PNM* NoiseMedian::transform() PNM* newImage = new PNM(width, height, image->format()); - qDebug() << Q_FUNC_INFO << "Not implemented yet!"; + for(int x = 0 ; x < width; x++){ + for (int y = 0 ; y < height; y++) { + int g = getMedian(x,y, GChannel); + int r = getMedian(x,y, RChannel); + int b = getMedian(x,y, BChannel); + newImage->setPixel(x,y,QColor(r,g,b).rgba()); + } + } return newImage; } int NoiseMedian::getMedian(int x, int y, Channel channel) { int radius = getParameter("radius").toInt(); + QSet set; - qDebug() << Q_FUNC_INFO << "Not implemented yet!"; + QList list = QList::fromSet(set); + std::sort(list.begin(), list.end()); - return 0; + + for ( int i = x- radius ; i <= x+radius ; i++) { + for ( int j = y- radius ; j <= y+radius ; j++) { + + if ( i+radius >= image->width() || + i-radius < 0 || + j+radius >= image->height() || + j-radius < 0 ) { + continue; + } + + if (channel==LChannel) set.insert(qAlpha(image->pixel(i,j))); + if (channel==RChannel) set.insert(qRed(image->pixel(i,j))); + if (channel==GChannel) set.insert(qGreen(image->pixel(i,j))); + if (channel==BChannel) set.insert( qBlue(image->pixel(i,j))); + + } + } + + QList data = QList::fromSet(set); + std::sort(data.begin(), data.end()); + int result =0 ; + + if( data.size() %2 == 1){ + result = data.at(data.size()/2); + }else if(data.size()>3){ + int first_med = data.at((data.size()/2 )-1); + int sec_dem = data.at((data.size()/2 )); + result = (first_med+sec_dem)/2; + } + data.clear(); + set.clear(); + return result; } From 56aaa6335469e1a6c6e1d6776d78fd19177c4329 Mon Sep 17 00:00:00 2001 From: Dawid_Kreft Date: Mon, 6 Apr 2020 23:43:40 +0200 Subject: [PATCH 2/3] convolution in my bad branch --- src/core/transformations/convolution.cpp | 129 ++++++++++++++++++----- 1 file changed, 103 insertions(+), 26 deletions(-) diff --git a/src/core/transformations/convolution.cpp b/src/core/transformations/convolution.cpp index af164c6..e3f3ec6 100644 --- a/src/core/transformations/convolution.cpp +++ b/src/core/transformations/convolution.cpp @@ -22,23 +22,98 @@ math::matrix Convolution::getMask(int size, Mode mode = Normalize) { math::matrix mask(size, size); - qDebug() << Q_FUNC_INFO << "Not implemented yet!"; + for (int x=0; x mask, Mode mode = RepeatEdge) +PNM* Convolution::convolute(math::matrix mask, Mode mode = CyclicEdge) { int width = image->width(), height = image->height(); + int size=mask.rowno(); PNM* newImage = new PNM(width, height, image->format()); + math::matrix red_window(size, size); + math::matrix blue_window(size, size); + math::matrix gray_window(size, size); + math::matrix green_window(size, size); + + float mask_weight=sum(mask); + float red_sum; + float green_sum; + float blue_sum; + float gray_sum; + + + + for (int x=0; x0) + { + red_sum=red_sum/mask_weight; + blue_sum=blue_sum/mask_weight; + green_sum=green_sum/mask_weight; + //gray_sum=gray_sum/mask_weight; + } - qDebug() << Q_FUNC_INFO << "Not implemented yet!"; + if(red_sum>=0 && red_sum<=255 && green_sum>=0 && green_sum<=255 && blue_sum>=0 && blue_sum<=255) + { + newImage->setPixel(x,y, QColor(red_sum,green_sum,blue_sum).rgb()); + } + else if (red_sum>=0 && red_sum<=255 && green_sum>=0 && green_sum<=255) + { + int old_blue = qBlue(newImage->pixel(x,y)); + newImage->setPixel(x,y, QColor(red_sum,green_sum,old_blue).rgb()); + } + else if (blue_sum>=0 && blue_sum<=255 && green_sum>=0 && green_sum<=255) + { + int old_red = qRed(newImage->pixel(x,y)); + newImage->setPixel(x,y, QColor(old_red,green_sum,blue_sum).rgb()); + } + else if (blue_sum>=0 && blue_sum<=255 && red_sum>=0 && red_sum<=255) + { + int old_green = qGreen(newImage->pixel(x,y)); + newImage->setPixel(x,y, QColor(red_sum,old_green,blue_sum).rgb()); + } + + + } + + return newImage; } @@ -51,17 +126,13 @@ const math::matrix Convolution::join(math::matrix A, math::matrix< int size = A.rowno(); math::matrix C(size, size); - qDebug() << Q_FUNC_INFO << "Not implemented yet!"; + for (int x=0; x A) { float sum = 0.0; - int size = A.rowno(); - - float t = 4.0; - ; - for(int x = 0; x < (int) A.size() ; x++){ - - - } - - - - qDebug() << Q_FUNC_INFO << "Not implemented yet!"; + int size=A.rowno(); + for (int x=0; x Convolution::reflection(const math::matrix A) { int size = A.rowno(); math::matrix C(size, size); + int counter_i=0; + int counter_j=0; + + + for (int x=size-1; x>=0; x--) + { + for (int y=size-1; y>=0; y--) + { + C[x][y]=A[counter_i][counter_j]; + counter_j++; + } + counter_i++; + } - qDebug() << Q_FUNC_INFO << "Not implemented yet!"; return C; -} +} \ No newline at end of file From 886566e62082b9e14a948dbf8a798d03b75aaf3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonasz=20=C5=9Awita=C5=82a?= Date: Mon, 27 Apr 2020 01:12:34 +0200 Subject: [PATCH 3/3] Zadanie_08_Jonasz --- src/core/transformations/edge_gradient.cpp | 37 +++++++++++- src/core/transformations/edge_gradient.h | 1 + src/core/transformations/edge_laplacian.cpp | 9 ++- .../edge_laplacian_of_gauss.cpp | 13 ++++- src/core/transformations/edge_prewitt.cpp | 32 ++++++++++- src/core/transformations/edge_roberts.cpp | 19 ++++++- src/core/transformations/edge_sobel.cpp | 28 ++++++++- src/core/transformations/edge_zero.cpp | 57 ++++++++++++++++++- src/core/transformations/edge_zero.h | 2 + 9 files changed, 188 insertions(+), 10 deletions(-) diff --git a/src/core/transformations/edge_gradient.cpp b/src/core/transformations/edge_gradient.cpp index a36a5b8..d9e87d7 100644 --- a/src/core/transformations/edge_gradient.cpp +++ b/src/core/transformations/edge_gradient.cpp @@ -22,10 +22,43 @@ PNM* EdgeGradient::horizontalDetection() PNM* EdgeGradient::transform() { + PNM* newImage = new PNM(image->width(), image->height(), image->format()); + PNM* imageX; + imageX=verticalDetection(); + PNM* imageY; + imageY=horizontalDetection(); - qDebug() << Q_FUNC_INFO << "Not implemented yet!"; - return newImage; + + for(int i=0;iwidth();i++) + { + for(int j=0;jheight();j++) + { + float imageX_red = qRed(imageX->pixel(i,j)); + float imageX_green = qGreen(imageX->pixel(i,j)); + float imageX_blue = qBlue(imageX->pixel(i,j)); + float imageY_red = qRed(imageY->pixel(i,j)); + float imageY_green = qGreen(imageY->pixel(i,j)); + float imageY_blue = qBlue(imageY->pixel(i,j)); + + float ImageNew_red=getEdgeValue(imageX_red, imageY_red); + float ImageNew_green=getEdgeValue(imageX_green, imageY_green); + float ImageNew_blue=getEdgeValue(imageX_blue, imageY_blue); + + newImage->setPixel(i,j, QColor(ImageNew_red, ImageNew_green, ImageNew_blue).rgb()); + + } + } + + return newImage; } +float EdgeGradient::getEdgeValue(int x, int y) +{ + + return sqrt(pow(x,2)+pow(y,2)); + +} + + diff --git a/src/core/transformations/edge_gradient.h b/src/core/transformations/edge_gradient.h index 331ccc1..158d64a 100644 --- a/src/core/transformations/edge_gradient.h +++ b/src/core/transformations/edge_gradient.h @@ -10,6 +10,7 @@ public: EdgeGradient(PNM*, ImageViewer*); virtual PNM* transform(); + static float getEdgeValue(int, int); PNM* verticalDetection(); PNM* horizontalDetection(); diff --git a/src/core/transformations/edge_laplacian.cpp b/src/core/transformations/edge_laplacian.cpp index 8c26718..9d7714c 100644 --- a/src/core/transformations/edge_laplacian.cpp +++ b/src/core/transformations/edge_laplacian.cpp @@ -14,8 +14,15 @@ math::matrix EdgeLaplacian::getMask(int, Mode) { int size = getParameter("size").toInt(); math::matrix mask(size, size); + int r=size/2; + + for (int x=0; x EdgeLaplaceOfGauss::getMask(int, Mode) double sigma = getParameter("sigma").toDouble(); math::matrix mask(size, size); + int r=size/2; + + for(int i=0;ig_x = math::matrix(size,size); + this->g_y = math::matrix(size,size); + + + g_x[0][0]=-1; + g_x[0][1]=0; + g_x[0][2]=1; + g_x[1][0]=-1; + g_x[1][1]=0; + g_x[1][2]=1; + g_x[2][0]=-1; + g_x[2][1]=-0; + g_x[2][2]=1; + + + g_y[0][0]=-1; + g_y[0][1]=-1; + g_y[0][2]=-1; + g_y[1][0]=0; + g_y[1][1]=0; + g_y[1][2]=0; + g_y[2][0]=1; + g_y[2][1]=1; + g_y[2][2]=1; + + + + } diff --git a/src/core/transformations/edge_roberts.cpp b/src/core/transformations/edge_roberts.cpp index 0428da9..99a29c8 100644 --- a/src/core/transformations/edge_roberts.cpp +++ b/src/core/transformations/edge_roberts.cpp @@ -14,5 +14,22 @@ EdgeRoberts::EdgeRoberts(PNM* img, ImageViewer* iv) : void EdgeRoberts::prepareMatrices() { - qDebug() << Q_FUNC_INFO << "Not implemented yet!"; + + int size = 2; + + + this->g_x = math::matrix(size,size); + this->g_y = math::matrix(size,size); + + + g_x[0][0]=1; + g_x[0][1]=0; + g_x[1][0]=0; + g_x[1][1]=-1; + + g_y[0][0]=0; + g_y[0][1]=1; + g_y[1][0]=-1; + g_y[1][1]=0; + } diff --git a/src/core/transformations/edge_sobel.cpp b/src/core/transformations/edge_sobel.cpp index 6ef4bff..d99a057 100644 --- a/src/core/transformations/edge_sobel.cpp +++ b/src/core/transformations/edge_sobel.cpp @@ -14,7 +14,33 @@ EdgeSobel::EdgeSobel(PNM* img) : void EdgeSobel::prepareMatrices() { - qDebug() << Q_FUNC_INFO << "Not implemented yet!"; + int size = 3; + + + this->g_x = math::matrix(size,size); + this->g_y = math::matrix(size,size); + + + g_x[0][0]=-1; + g_x[0][1]=0; + g_x[0][2]=1; + g_x[1][0]=-2; + g_x[1][1]=0; + g_x[1][2]=2; + g_x[2][0]=-1; + g_x[2][1]=-0; + g_x[2][2]=1; + + + g_y[0][0]=-1; + g_y[0][1]=-2; + g_y[0][2]=-1; + g_y[1][0]=0; + g_y[1][1]=0; + g_y[1][2]=0; + g_y[2][0]=1; + g_y[2][1]=2; + g_y[2][2]=1; } math::matrix* EdgeSobel::rawHorizontalDetection() diff --git a/src/core/transformations/edge_zero.cpp b/src/core/transformations/edge_zero.cpp index 541943f..b180705 100644 --- a/src/core/transformations/edge_zero.cpp +++ b/src/core/transformations/edge_zero.cpp @@ -20,11 +20,66 @@ PNM* EdgeZeroCrossing::transform() int size = getParameter("size").toInt(); double sigma = getParameter("sigma").toDouble(); int t = getParameter("threshold").toInt(); + int v = 128; + int r=size/2; PNM* newImage = new PNM(width, height, QImage::Format_Grayscale8); - qDebug() << Q_FUNC_INFO << "Not implemented yet!"; + EdgeLaplaceOfGauss * gaussImage = new EdgeLaplaceOfGauss(image); + + for (int x=0; x wR = gaussImage->getWindow(x,y,size,RChannel,CyclicEdge); + math::matrix wB = gaussImage->getWindow(x,y,size,BChannel,CyclicEdge); + math::matrix wG = gaussImage->getWindow(x,y,size,GChannel,CyclicEdge); + + float maxR=maxValue(wR,size); + float maxG=maxValue(wG,size); + float maxB=maxValue(wB,size); + float minR=minValue(wR,size); + float minG=minValue(wG,size); + float minB=minValue(wB,size); + + if (minR < v-t && maxR > v+t) + newImage->setPixel(x, y,gaussImage->getPixel(x,y,CyclicEdge)); + else if (minG < v-t && maxG>v+t) + newImage->setPixel(x, y, gaussImage->getPixel(x,y,CyclicEdge)); + else if (minB < v-t && maxB > v+t) + newImage->setPixel(x, y, gaussImage->getPixel(x,y,CyclicEdge)); + else + { + newImage->setPixel(x, y, 0); + } + } + + + + return newImage; } +float EdgeZeroCrossing::maxValue(math::matrix mask, int size) +{ + float max=mask[0][0]; + + for (int x=0; x max) + max=mask[x][y]; + + return max; +} + +float EdgeZeroCrossing::minValue(math::matrix mask, int size) +{ + float min=mask[0][0]; + + for (int x=0; x mask, int size); + float minValue(math::matrix mask, int size); }; #endif // EDGE_ZERO_H