PNM* NoiseBilateral::transform() { int width = image->width(); int height = image->height(); PNM* newImage = new PNM(width, height, image->format()); sigma_d = getParameter("sigma_d").toInt(); sigma_r = getParameter("sigma_r").toInt(); radius = sigma_d; if(image->format() == QImage::Format_Indexed8) { for(int x=0; x<width; x++) for(int y=0; y<height; y++) { int l = calcVal(x, y, LChannel); newImage->setPixel(x, y, l); } } else { for(int x=0; x<width; x++) for(int y=0; y<height; y++) { int r = calcVal(x, y, RChannel); int g = calcVal(x, y, GChannel); int b = calcVal(x, y, BChannel); QColor newPixel = QColor(r,g,b); newImage->setPixel(x, y, newPixel.rgb()); } } return newImage; }
PNM* Correction::transform() { float shift = getParameter("shift").toFloat(); float factor = getParameter("factor").toFloat(); float gamma = getParameter("gamma").toFloat(); int width = image->width(); int height = image->height(); PNM* newImage = new PNM(width, height, image->format()); for (int i = 0; i < 256; i++) { Correction::LUT[i] = (int)((((float)(pow((double)i, (double)gamma))) * factor) + shift); if (Correction::LUT[i] < 0) { Correction::LUT[i] = 0; } else if (Correction::LUT[i] > 255) { Correction::LUT[i] = 255; } } if (image->format() == QImage::Format_Indexed8){ for (int x = 0; x < width; x++){ for (int y = 0; y < height; y++) { QRgb pixel = image->pixel(x, y); // Getting the pixel(x,y) value int g = qGray(pixel); // Get the 0-255 value of the G channel g = Correction::LUT[g]; newImage->setPixel(x, y, g); } } } else{ for (int x = 0; x < width; x++) for (int y = 0; y < height; y++) { QRgb pixel = image->pixel(x, y); // Getting the pixel(x,y) value int r = qRed(pixel); // Get the 0-255 value of the R channel int g = qGreen(pixel); // Get the 0-255 value of the G channel int b = qBlue(pixel); // Get the 0-255 value of the B channel r = Correction::LUT[r]; g = Correction::LUT[g]; b = Correction::LUT[b]; QColor newPixel = QColor(r, g, b); newImage->setPixel(x, y, newPixel.rgb()); } } return newImage; }
PNM* Correction::transform() { float shift = getParameter("shift").toFloat(); float factor = getParameter("factor").toFloat(); float gamma = getParameter("gamma").toFloat(); int width = image->width(); int height = image->height(); PNM* newImage = new PNM(width, height, image->format()); // Inicjalizacja tablicy for(int i = 0; i<= PIXEL_VAL_MAX; i++) { LUT[i] = i; } // Tablicowanie for(int i = 0; i<= PIXEL_VAL_MAX; i++) { LUT[i] = std::pow(LUT[i], gamma) * factor + shift; LUT[i] = Correction::boundariesCheck(LUT[i]); } if (image->format() == QImage::Format_Indexed8) { for (int x=0; x<width; x++) for (int y=0; y<height; y++) { QRgb pixel = image->pixel(x,y); int v = qGray(pixel); newImage->setPixel(x, y, LUT[v]); } } else if (image->format() == QImage::Format_RGB32) { for (int x=0; x<width; x++) for (int y=0; y<height; y++) { QRgb pixel = image->pixel(x,y); int r = qRed(pixel); int g = qGreen(pixel); int b = qBlue(pixel); QColor newPixel = QColor(LUT[r], LUT[g], LUT[b]); newImage->setPixel(x, y, newPixel.rgb()); } } return newImage; }
PNM* BinarizationGradient::transform() { int width = image->width(); int height = image->height(); PNM* newImage = new PNM(width, height, QImage::Format_Mono); QRgb pixel; int top = 0; int bottom = 0; int gradientx = 0; int gradienty = 0; int gradient = 0; int temp = 0; for (int x=0; x<width; ++x) { for (int y=0; y<height; ++y) { QRgb pixel0 = image->pixel(x-1>=0 ? x-1 : x, y); QRgb pixel1 = image->pixel(x+1 < width ? x+1 : x, y); gradientx = qGray(pixel1) - qGray(pixel0); pixel0 = image->pixel(x, y-1>=0 ? y-1 : y); pixel1 = image->pixel(x, y+1<height ? y+1 : y); gradienty = qGray(pixel1) - qGray(pixel0); if (gradientx >= gradienty) { gradient = gradientx; }else{ gradient = gradienty; } pixel = image->pixel(x, y); top += qGray(pixel) * gradient; bottom += gradient; temp = bottom==0 ? 0 : top / bottom; if (qGray(pixel) >= temp){ newImage->setPixel(x, y, Qt::color1); }else{ newImage->setPixel(x, y, Qt::color0); } } } return newImage; }
PNM* MorphologicalOperator::transform() { int size = getParameter("size").toInt(); SE shape = (MorphologicalOperator::SE) getParameter("shape").toInt(); PNM* newImage = new PNM(image->width(), image->height(), QImage::Format_RGB32); int width = image->width(); int height = image->height(); math::matrix<bool> elementStrukturyzujacy = getSE(size, shape); for(int x = 0; x < width; x++) for(int y = 0; y < height; y++) { math::matrix<double> windowR = getWindow(x, y, size, RChannel, RepeatEdge); math::matrix<double> windowG = getWindow(x, y, size, GChannel, RepeatEdge); math::matrix<double> windowB = getWindow(x, y, size, BChannel, RepeatEdge); newImage->setPixel(x, y, qRgb(morph(windowR, elementStrukturyzujacy), morph(windowG, elementStrukturyzujacy), morph(windowB, elementStrukturyzujacy))); } return newImage; }
PNM* BinarizationGradient::transform() { int width = image->width(); int height = image->height(); PNM* newImage = new PNM(width, height, QImage::Format_Mono); Mode mode = RepeatEdge; int licz = 0, mian = 0; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { int value_pixel, G, Gx, Gy; QRgb pixel = getPixel(x, y, mode); value_pixel = qGray(pixel); Gx = qGray(this->getPixel(x + 1, y, mode)) - qGray(this->getPixel(x - 1, y, mode)); Gy = qGray(this->getPixel(x, y + 1, mode)) - qGray(this->getPixel(x, y - 1, mode)); if (Gx > Gy) { G = Gx; } else{ G = Gy; } licz += value_pixel * G; mian += G; } } int threshold = licz / mian; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { QRgb pixel = image->pixel(x, y); int value_pixel = qGray(pixel); if (value_pixel < threshold){ newImage->setPixel(x, y, 0); } else{ newImage->setPixel(x, y, 1); } } } return newImage; }
PNM* BinarizationGradient::transform() { int width = image->width(); int height = image->height(); PNM* newImage = new PNM(width, height, QImage::Format_Mono); int top = 0, bot = 0, tym = 0, grx = 0, gry = 0, gra = 0; for (int x = 0; x < width; ++x) { for (int y = 0; y < height; ++y) { QRgb pix, pix1, pix2; pix1 = getPixel(x - 1, y, RepeatEdge); pix2 = getPixel(x + 1, y, RepeatEdge); grx = qGray(pix2) - qGray(pix1); pix1 = getPixel(x, y - 1, RepeatEdge); pix2 = getPixel(x, y + 1, RepeatEdge); gry = qGray(pix2) - qGray(pix1); if (grx >= gry) { gra = grx; } else { gra = gry; } pix = getPixel(x, y, RepeatEdge); top += qGray(pix) * gra; bot += gra; tym = bot == 0 ? 0 : top / bot; if (qGray(pix) >= tym) { newImage->setPixel(x, y, Qt::color1); } else { newImage->setPixel(x, y, Qt::color0); } } } return newImage; }
PNM* ConversionGrayscale::transform() { // qDebug() << Q_FUNC_INFO << "Not implemented yet!"; int width = image->width(); int height = image->height(); PNM* newImage = new PNM(width, height, QImage::Format_Indexed8); if (image->format() == QImage::Format_Mono) { for (int x = 0; x<width; x++) for (int y = 0; y<height; y++) { QColor color = QColor::fromRgb(image->pixel(x, y)); // Getting the pixel(x,y) value newImage->setPixel(x, y, color == Qt::white ? Qt::color1 : Qt::color0); } } else // if (image->format() == QImage::Format_RGB32) { for (int x = 0; x<width; x++) for (int y = 0; y<height; y++) { QRgb pixel = image->pixel(x, y); // Getting the pixel(x,y) value int r = qRed(pixel); // Get the 0-255 value of the R channel int g = qGreen(pixel); // Get the 0-255 value of the G channel int b = qBlue(pixel); // Get the 0-255 value of the B channel r = (int) floor(r*0.3); g = (int) floor(g*0.6); b = (int) floor(b*0.1); //QColor newPixel = QColor(r, g, b); newImage->setPixel(x, y, r + g + b); } } return newImage; }
PNM* NoiseBilateral::transform() { int width = image->width(); int height = image->height(); PNM* newImage = new PNM(width, height, image->format()); sigma_d = getParameter("sigma_d").toInt(); sigma_r = getParameter("sigma_r").toInt(); radius = sigma_d; Mode mode = RepeatEdge; QRgb pixel; int r,g,b; if(image->format() == QImage::Format_RGB32){ for(int x=0;x<width;x++){ for(int y=0;y<height;y++){ pixel = getPixel(x,y,mode); r=calcVal(x,y,RChannel); g=calcVal(x,y,GChannel); b=calcVal(x,y,BChannel); QColor newPixel = QColor(r,g,b); newImage->setPixel(x,y,newPixel.rgb()); } } } else if(image->format() == QImage::Format_Indexed8){ for(int x=0;x<width;x++){ for(int y=0;y<height;y++){ int temp = calcVal(x,y,LChannel); newImage->setPixel(x,y,temp); } } } return newImage; }
PNM* MapNormal::transform() { int width = image->width(), height = image->height(); double strength = getParameter("strength").toDouble(); PNM* newImage = new PNM(width, height, image->format()); //qDebug() << Q_FUNC_INFO << "Not implemented yet!"; MapHeight* mh = new MapHeight(image); PNM* imgHeight = mh->transform(); EdgeSobel* es = new EdgeSobel(image); math::matrix<float>* Gx = es->rawHorizontalDetection(); math::matrix<float>* Gy = es->rawVerticalDetection(); for (int i = 0; i < width; i++) for (int j = 0; j < height; j++) { float dx = (*Gx)(i,j)/255; float dy = (*Gy)(i,j)/255; float dz = 1/strength; double dw = sqrt(dx*dx + dy*dy + dz*dz); dx = dx / dw; dy = dy / dw; dz = dz / dw; dx = (dx + 1.0)*(255/strength); dy = (dy + 1.0)*(255/strength); dz = (dz + 1.0)*(255/strength); QColor newPixel = QColor(dx,dy,dz); newImage->setPixel(i,j, newPixel.rgb()); } return newImage; }
PNM* MapNormal::transform() { int width = image->width(), height = image->height(); double strength = getParameter("strength").toDouble(); PNM* newImage = new PNM(width, height, image->format()); PNM* tempImage = MapHeight(image).transform(); EdgeSobel sobel(tempImage); math::matrix<float>* Gx = sobel.rawHorizontalDetection(); math::matrix<float>* Gy = sobel.rawVerticalDetection(); newImage = new PNM(width, height, QImage::Format_RGB32); for(int i=0; i<width; i++) { for(int j=0; j<height; j++) { double dx = (*Gx)(i,j)/PIXEL_VAL_MAX; double dy = (*Gy)(i,j)/PIXEL_VAL_MAX; double dz = 1/strength; double length = sqrt(dx*dx + dy*dy + dz*dz); dx = dx/length; dy = dy/length; double dZ = dz/length; double t = 255/strength; dx = (dx + 1.0)* t; dy = (dy + 1.0)* t; dZ = (dZ + 1.0)* t; newImage->setPixel(i,j,qRgb(dx,dy,dZ)); } } return newImage; }
PNM* MapNormal::transform() { int width = image->width(), height = image->height(); double strength = getParameter("strength").toDouble(); PNM* newImage = new PNM(width, height, image->format()); PNM* heightImage = MapHeight(image).transform(); math::matrix<double>* G_x = EdgeSobel(heightImage).rawHorizontalDetection(); math::matrix<double>* G_y = EdgeSobel(heightImage).rawVerticalDetection(); for (int i=0; i<width;i++) { for (int j=0; j<height;j++) { double gx = (*G_x)(i,j); double gy = (*G_y)(i,j); double dX = gx/255; double dY = gy/255; double dZ = 1/strength; double length = sqrt(pow(dX,2)+pow(dY,2)+pow(dZ,2)); dX/=length; dY/=length; dZ/=length; double r = (dX + 1.0)*(255/strength); double g = (dY + 1.0)*(255/strength); double b = (dZ + 1.0)*(255/strength); QColor newPixel = QColor(r,g,b); newImage->setPixel(i,j, newPixel.rgb()); } } return newImage; }
PNM* Correction::transform() { float shift = getParameter("shift").toFloat(); float factor = getParameter("factor").toFloat(); float gamma = getParameter("gamma").toFloat(); int width = image->width(); int height = image->height(); PNM* newImage = new PNM(width, height, image->format()); for (int i=0; i<PIXEL_VAL_MAX+1; i++) { LUT[i] = i; LUT[i] += shift; LUT[i] *= factor; LUT[i] = pow(LUT[i],gamma); LUT[i] = LUT[i] > 255 ? 255 : LUT[i]; LUT[i] = LUT[i] < 0 ? 0 : LUT[i]; } for (int x=0; x<width; x++) for (int y=0; y<height; y++) { QRgb pixel = image->pixel(x,y); // Getting the pixel(x,y) value int r = qRed(pixel); // Get the 0-255 value of the R channel int g = qGreen(pixel); // Get the 0-255 value of the G channel int b = qBlue(pixel); // Get the 0-255 value of the B channel QColor newPixel = QColor(LUT[r],LUT[g],LUT[b]); newImage->setPixel(x,y, newPixel.rgb()); } return newImage; }
PNM* HoughLines::transform() { // Cut of value from the image; int threshold = getParameter("threshold").toInt(); bool drawWholeLines = getParameter("draw_whole_lines").toBool(); PNM* newImage = new PNM(image->copy()); EdgeLaplacian* edgeLaplacian = new EdgeLaplacian(image); edgeLaplacian->setParameter("size", 3); image = edgeLaplacian->transform(); delete edgeLaplacian; BinarizationGradient* binGradient = new BinarizationGradient(image); image = binGradient->transform(); delete binGradient; Hough* hough = new Hough(image); hough->setParameter("theta_density" , 3); hough->setParameter("skip_edge_detection", true); image = hough->transform(); delete hough; int width = image->width(), height = image->height(); for (int theta = 0; theta < width; theta++) { for (int rho = 0; rho < height; rho++) { QRgb pixel = image->pixel(theta, rho); if(qGray(pixel) > threshold) newImage->setPixel(theta, rho, 1); } } return newImage; }
PNM* BinarizationIterBimodal::transform() { int width = image->width(); int height = image->height(); PNM* newImage = new PNM(width, height, QImage::Format_Mono); int T = 256/2; int T_new = 0; while (true){ int Tw = 0; int iw = 0; int Tb = 0; int ib = 0; for (int x=0; x<width; x++) for (int y=0; y<height; y++) { QRgb pixel = image->pixel(x,y); int val = qGray(pixel); if (val > T) { Tw += val; ++iw; } else { Tb += val; ++ib; } } if (iw == 0 && ib == 0) T_new = 0; else if (iw == 0) T_new = Tb/ib; else if (ib == 0) T_new = Tw/iw; else T_new = ((Tw/iw) + (Tb/ib)) / 2; if(T != T_new) T = T_new; else break; } for (int x=0; x<width; x++) for (int y=0; y<height; y++) { QRgb pixel = image->pixel(x,y); int val = qGray(pixel); if (val < T) newImage->setPixel(x, y, 0); else newImage->setPixel(x, y, 1); } return newImage; }
/** Does the convolution process for all pixels using the given mask. */ PNM* Convolution::convolute(math::matrix<float> mask, Mode mode = RepeatEdge) { int width = image->width(), height = image->height(); PNM* newImage = new PNM(width, height, image->format()); double weight = sum(mask); if (image->format() == QImage::Format_Indexed8){ for (int x = 0; x < width; x++){ for (int y = 0; y < height; y++){ math::matrix<float> okno = getWindow(x, y, mask.rowno(), LChannel, mode); math::matrix<float> akumulator = join(okno, reflection(mask)); float sum_aku = sum(akumulator); if (weight != 0){ sum_aku = sum_aku / weight; } if (sum_aku > 255){ sum_aku = 255; } if (sum_aku < 0){ sum_aku = 0; } newImage->setPixel(x, y, (int)floor(sum_aku)); } } } else if (image->format() == QImage::Format_RGB32){ for (int x = 0; x < width; x++){ for (int y = 0; y < height; y++){ math::matrix<float> okno = getWindow(x, y, mask.rowno(), RChannel, mode); math::matrix<float> akumulator = join(okno, reflection(mask)); float sum_aku_red = sum(akumulator); okno = getWindow(x, y, mask.rowno(), GChannel, mode); akumulator = join(okno, reflection(mask)); float sum_aku_green = sum(akumulator); okno = getWindow(x, y, mask.rowno(), BChannel, mode); akumulator = join(okno, reflection(mask)); float sum_aku_blue = sum(akumulator); if (weight != 0) { sum_aku_red = sum_aku_red / weight; sum_aku_green = sum_aku_green / weight; sum_aku_blue = sum_aku_blue / weight; } if (sum_aku_red > 255) { sum_aku_red = 255; } if (sum_aku_red < 0){ sum_aku_red = 0; } if (sum_aku_green > 255) { sum_aku_green = 255; } if (sum_aku_green < 0){ sum_aku_green = 0; } if (sum_aku_blue > 255) { sum_aku_blue = 255; } if (sum_aku_blue < 0){ sum_aku_blue = 0; } QColor new_value = QColor((int)floor(sum_aku_red), (int)floor(sum_aku_green), (int)floor(sum_aku_blue)); newImage->setPixel(x, y, new_value.rgb()); } } } return newImage; }
PNM* MapHorizon::transform() { int width = image->width(), height = image->height(); double scale = getParameter("scale").toDouble(); int sun_alpha = getParameter("alpha").toInt(); int dx, dy; switch (getParameter("direction").toInt()) { case NORTH: dx = 0; dy = -1; break; case SOUTH: dx = 0; dy = 1; break; case EAST: dx = 1; dy = 0; break; case WEST: dx = -1; dy = 0; break; default: qWarning() << "Unknown direction!"; dx = 0; dy = -1; } PNM* newImage = new PNM(width, height, QImage::Format_Indexed8); MapHeight* mapHeight = new MapHeight(image); image = mapHeight->transform(); delete mapHeight; for (int i = 0; i < width; i++) for (int j = 0; j < height; j++) { double alpha = 0.0; int curr_h = qGray(image->pixel(i, j)); int k = i + dx; int l = j + dy; while(k < width && l < height) { double ray_h = qGray(image->pixel(k,l)); if (curr_h < ray_h) { double dist = sqrt(pow(k - i, 2) + pow(l - j, 2)) * scale; double ray_alpha = atan((ray_h-curr_h)/dist)*180./3.14; if (ray_alpha > alpha) alpha = ray_alpha; } ++k; ++l; } double delta = alpha - sun_alpha; if (delta <= 0) newImage->setPixel(i,j, PIXEL_VAL_MAX); else newImage->setPixel(i,j, cos(delta*3.14/180.)*255); } return newImage; }
PNM* EdgeZeroCrossing::transform() { int width = image->width(), height = image->height(); int size = getParameter("size").toInt(); double sigma = getParameter("sigma").toDouble(); int t = getParameter("threshold").toInt(); EdgeLaplaceOfGauss ELOG(image); ELOG.setParameter("size", getParameter("size")); ELOG.setParameter("sigma", getParameter("sigma")); PNM* newImage = new PNM(width, height, QImage::Format_Indexed8); Transformation laplasjan(ELOG.transform()); int v_0 = 128; if (image->format() == QImage::Format_Indexed8) { for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { math::matrix<float> maska = laplasjan.getWindow(i, j, size, LChannel, RepeatEdge); int min = 255; int max = 0; for (int x = 0; x < size; x++) { for (int y = 0; y < size; y++) { if (maska(x, y) > max) max = maska(x, y); if (maska(x, y) < min) min = maska(x, y); } } if (min < (v_0 - t) && max > (v_0 + t)) { newImage->setPixel(i, j, maska(size / 2, size / 2)); } else { newImage->setPixel(i, j, 0); } } } } else if (image->format() == QImage::Format_RGB32) { for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { math::matrix<float> maskaR = laplasjan.getWindow(i, j, size, RChannel, RepeatEdge); math::matrix<float> maskaG = laplasjan.getWindow(i, j, size, GChannel, RepeatEdge); math::matrix<float> maskaB = laplasjan.getWindow(i, j, size, BChannel, RepeatEdge); int minR = 255; int minG = 255; int minB = 255; int maxR = 0; int maxG = 0; int maxB = 0; for (int x = 0; x < size; x++) { for (int y = 0; y < size; y++) { if (maskaR(x, y) > maxR) maxR = maskaR(x, y); if (maskaR(x, y) < minR) minR = maskaR(x, y); if (maskaG(x, y) > maxG) maxG = maskaG(x, y); if (maskaG(x, y) < minG) minG = maskaG(x, y); if (maskaB(x, y) > maxB) maxB = maskaB(x, y); if (maskaB(x, y) < minB) minB = maskaB(x, y); } } int r_val, g_val, b_val; if (minR < (v_0 - t) && maxR >(v_0 + t)) { r_val = maskaR(size / 2, size / 2); newImage->setPixel(i, j, maskaR(size / 2, size / 2)); } else if (minG < (v_0 - t) && maxG >(v_0 + t)) { g_val = maskaG(size / 2, size / 2); newImage->setPixel(i, j, g_val); } else if (minB < (v_0 - t) && maxB >(v_0 + t)) { b_val = maskaB(size / 2, size / 2); newImage->setPixel(i, j, b_val); } else { newImage->setPixel(i, j, 0); } } } } return newImage; }
PNM* MorphologicalOperator::transform() { int size = getParameter("size").toInt(); SE shape = (MorphologicalOperator::SE) getParameter("shape").toInt(); PNM* newImage = new PNM(image->width(), image->height(), QImage::Format_RGB32); math::matrix<bool> se(size,size); se = getSE(size,shape); int radius = size/2; if (image->format() == QImage::Format_Mono) { } else if (image->format() == QImage::Format_Indexed8) { // Iterate over image space for (int x=0; x<image->width(); x++) { for (int y=0; y<image->height(); y++) { math::matrix<double> window(size,size); int a=0; for (int i=(x-radius);i<(x+radius+1);i++) { int b=0; for (int j=(y-radius);j<(y+radius+1);j++) { QRgb pixel = getPixel (i,j, RepeatEdge); window(a,b) = qGray(pixel); b++; } a++; } int v = morph(window, se); newImage->setPixel(x,y, v); } } } else { //if (image->format() == QImage::Format_RGB32) // Iterate over image space for (int x=0; x<image->width(); x++) { for (int y=0; y<image->height(); y++){ math::matrix<double> windowR(size,size); math::matrix<double> windowG(size,size); math::matrix<double> windowB(size,size); int a=0; for (int i=(x-radius);i<(x+radius+1);i++) { int b=0; for (int j=(y-radius);j<(y+radius+1);j++) { QRgb pixel = getPixel (i,j, RepeatEdge); windowR(a,b) = qRed(pixel); windowG(a,b) = qGreen(pixel); windowB(a,b) = qBlue(pixel); b++; } a++; } int r = morph(windowR, se); int g = morph(windowG, se); int b = morph(windowB, se); QColor newPixel = QColor(r,g,b); newImage->setPixel(x,y, newPixel.rgb()); } } } return newImage; }
PNM* MapHorizon::transform() { int width = image->width(), height = image->height(); double scale = getParameter("scale").toDouble(); int sun_alpha = getParameter("alpha").toInt(); int dx, dy; switch (getParameter("direction").toInt()) { case NORTH: dx = 0; dy = -1; break; case SOUTH: dx = 0; dy = 1; break; case EAST: dx = 1; dy = 0; break; case WEST: dx = -1; dy = 0; break; default: qWarning() << "Unknown direction!"; dx = 0; dy = -1; } PNM* newImage = new PNM(width, height, QImage::Format_Indexed8); PNM* mapHeight = MapHeight(image).transform(); for(int i=0; i<width; i++) for(int j=0; j<height; j++) { double alpha = 0; int current_h = qRed(mapHeight->pixel(i, j)); for(int k = i+dx, l = j+dy; k < width && l < height && k >= 0 && l >= 0; k = k+dx, l = l+dy) { int ray_h = qRed(mapHeight->pixel(k,l)); if(current_h < ray_h) { double dist = sqrt(pow(k - i, 2) + pow(l - j, 2)) * scale; double ray_alpha = atan((ray_h - current_h) / dist); if(ray_alpha > alpha) alpha = ray_alpha; } } double delta = alpha - sun_alpha * M_PI/180; if(delta > 0) { newImage->setPixel(i, j, cos(delta) * 255); } else { newImage->setPixel(i, j, 255); } } return newImage; }
/** Does the convolution process for all pixels using the given mask. */ PNM* Convolution::convolute(math::matrix<double> mask, Mode mode = RepeatEdge) { int width = image->width(), height = image->height(); PNM* newImage = new PNM(width, height, image->format()); double weight = sum(mask); if (image->format() == QImage::Format_Indexed8) { for (int x=0; x<width; x++) for (int y=0; y<height; y++) { math::matrix<double> window = getWindow(x, y, mask.ColNo(), LChannel, mode); math::matrix<double> akumulator = join(mask, window); double sumaAkumulatora = sum(akumulator); if(weight > 0) { sumaAkumulatora /= weight; } if(sumaAkumulatora > PIXEL_VAL_MAX) { sumaAkumulatora = PIXEL_VAL_MAX; } else if(sumaAkumulatora < PIXEL_VAL_MIN) { sumaAkumulatora = PIXEL_VAL_MIN; } newImage->setPixel(x, y, sumaAkumulatora); } } else //if (image->format() == QImage::Format_RGB32) { for(int x = 0; x < width; x++) for(int y = 0; y < height; y++) { math::matrix<double> windowR = getWindow(x, y, mask.ColNo(), RChannel, mode); math::matrix<double> akumulatorR = join(mask, windowR); math::matrix<double> windowG = getWindow(x, y, mask.ColNo(), GChannel, mode); math::matrix<double> akumulatorG = join(mask, windowG); math::matrix<double> windowB = getWindow(x, y, mask.ColNo(), BChannel, mode); math::matrix<double> akumulatorB = join(mask, windowB); double sumaAkumulatoraR = sum(akumulatorR); double sumaAkumulatoraG = sum(akumulatorG); double sumaAkumulatoraB = sum(akumulatorB); if(weight > 0) { sumaAkumulatoraR /= weight; sumaAkumulatoraG /= weight; sumaAkumulatoraB /= weight; } if(sumaAkumulatoraR > PIXEL_VAL_MAX) { sumaAkumulatoraR = PIXEL_VAL_MAX; } else if(sumaAkumulatoraR < PIXEL_VAL_MIN) { sumaAkumulatoraR = PIXEL_VAL_MIN; } if(sumaAkumulatoraG > PIXEL_VAL_MAX) { sumaAkumulatoraG = PIXEL_VAL_MAX; } else if(sumaAkumulatoraG < PIXEL_VAL_MIN) { sumaAkumulatoraG = PIXEL_VAL_MIN; } if(sumaAkumulatoraB > PIXEL_VAL_MAX) { sumaAkumulatoraB = PIXEL_VAL_MAX; } else if(sumaAkumulatoraB < PIXEL_VAL_MIN) { sumaAkumulatoraB = PIXEL_VAL_MIN; } newImage->setPixel(x, y, qRgb(sumaAkumulatoraR, sumaAkumulatoraG, sumaAkumulatoraB)); } } return newImage; }
PNM* HistogramStretching::transform() { int width = image->width(); int height = image->height(); PNM* newImage = new PNM(width, height, image->format()); if (image->format() == QImage::Format_Indexed8) { QHash<int, int>* channel = image->getHistogram()->get(Histogram::LChannel); QList<int> list = channel->keys(); double min = 255; double max = 0; for(int temp: list) { if(temp > max) max = temp; if (temp < min) min = temp; } for (int x=0; x<width; x++) for (int y=0; y<height; y++) { QRgb pixel = image->pixel(x,y); double val = qGray(pixel); val = (255/(max - min)) * (val - min); newImage->setPixel(x,y,val); } } else //if (image->format() == QImage::Format_RGB32) { QHash<int,int>* channelR = image->getHistogram()->get(Histogram::RChannel); QHash<int,int>* channelG = image->getHistogram()->get(Histogram::GChannel); QHash<int,int>* channelB = image->getHistogram()->get(Histogram::BChannel); QList<int> listR = channelR->keys(); QList<int> listG = channelG->keys(); QList<int> listB = channelB->keys(); double minR = 255; double maxR = 0; double minG = 255; double maxG = 0; double minB = 255; double maxB = 0; for(int temp: listR) { if(temp > maxR) maxR = temp; if(temp < minR) minR = temp; } for(int temp : listG) { if(temp > maxG) maxG = temp; if(temp < minG) minG = temp; } for(int temp: listB) { if(temp > maxB) maxB = temp; if(temp < minB) minB = temp; } for (int x=0; x<width; x++) for (int y=0; y<height; y++) { QRgb pixel = image->pixel(x,y); double r = qRed(pixel); double g = qGreen(pixel); double b = qBlue(pixel); r = (255.0/(maxR - minR)) * (r - minR); g = (255.0/(maxG - minG)) * (g - minG); b = (255.0/(maxB - minB)) * (b - minB); newImage->setPixel(x,y, qRgb(r,g,b)); } } return newImage; }
PNM* BinarizationGradient::transform() { int width = image->width(); int height = image->height(); PNM* newImage = new PNM(width, height, QImage::Format_Mono); if (image->format() == QImage::Format_Mono) { } else if (image->format() == QImage::Format_Indexed8) { int numerator = 0; int denominator = 0; for (int x=0; x<width; x++) { for (int y=0; y<height; y++) { QRgb pixel = image->pixel(x,y); // Getting the pixel(x,y) value int l=qGray(pixel); QRgb pixel1 = getPixel(x+1,y, RepeatEdge); QRgb pixel2 = getPixel(x-1,y, RepeatEdge); int l1 = qGray(pixel1); int l2 = qGray(pixel2); int gx = l1-l2; pixel1 = getPixel(x,y+1, RepeatEdge); pixel2 = getPixel(x,y-1, RepeatEdge); l1 = qGray(pixel1); l2 = qGray(pixel2); int gy = l1-l2; int g=max(gx, gy); numerator+=(l*g); denominator+=g; } } int threshold=(numerator/denominator); for (int x=0; x<width; x++) { for (int y=0; y<height; y++) { QRgb pixel = image->pixel(x,y); // Getting the pixel(x,y) value int l = qGray(pixel); newImage->setPixel(x,y, l < threshold ? Qt::color0 : Qt::color1); } } } else { //if (image->format() == QImage::Format_RGB32) int numerator = 0; int denominator = 0; for (int x=0; x<width; x++) { for (int y=0; y<height; y++) { QRgb pixel = image->pixel(x,y); // Getting the pixel(x,y) value int ri = qRed(pixel); // Get the 0-255 value of the R channel int gi = qGreen(pixel); // Get the 0-255 value of the G channel int bi = qBlue(pixel); // Get the 0-255 value of the B channel int ti = (ri+gi+bi)/3; QRgb pixelx1 = getPixel(x+1,y, RepeatEdge); QRgb pixelx2 = getPixel(x-1,y, RepeatEdge); int rx1 = qRed(pixelx1); // Get the 0-255 value of the R channel int gx1 = qGreen(pixelx1); // Get the 0-255 value of the G channel int bx1 = qBlue(pixelx1); // Get the 0-255 value of the B channel int tx1 = (rx1+gx1+bx1)/3; int rx2 = qRed(pixelx2); // Get the 0-255 value of the R channel int gx2 = qGreen(pixelx2); // Get the 0-255 value of the G channel int bx2 = qBlue(pixelx2); // Get the 0-255 value of the B channel int tx2 = (rx2+gx2+bx2)/3; int gx = tx1-tx2; int pixely1 = getPixel(x,y+1, RepeatEdge); int pixely2 = getPixel(x,y-1, RepeatEdge); int ry1 = qRed(pixely1); // Get the 0-255 value of the R channel int gy1 = qGreen(pixely1); // Get the 0-255 value of the G channel int by1 = qBlue(pixely1); // Get the 0-255 value of the B channel int ty1 = (ry1+gy1+by1)/3; int ry2 = qRed(pixely2); // Get the 0-255 value of the R channel int gy2 = qGreen(pixely2); // Get the 0-255 value of the G channel int by2 = qBlue(pixely2); // Get the 0-255 value of the B channel int ty2 = (ry2+gy2+by2)/3; int gy = ty1-ty2; int g=max(gx, gy); numerator=numerator+(ti*g); denominator+=g; } } int threshold=(numerator/denominator); for (int x=0; x<width; x++) { for (int y=0; y<height; y++) { QRgb pixel = image->pixel(x,y); // Getting the pixel(x,y) value int r = qRed(pixel); // Get the 0-255 value of the R channel int g = qGreen(pixel); // Get the 0-255 value of the G channel int b = qBlue(pixel); // Get the 0-255 value of the B channel int t = (r+g+b)/3; newImage->setPixel(x,y, t < threshold ? Qt::color0 : Qt::color1); } } } return newImage; }
PNM* HistogramStretching::transform() { int width = image->width(); int height = image->height(); PNM* newImage = new PNM(width, height, image->format()); QHash<int, int>::const_iterator i; int minR = 255; int maxR = 0; for (i=image->getHistogram()->get(Histogram::RChannel)->constBegin(); i!=image->getHistogram()->get(Histogram::RChannel)->constEnd();++i) { if (i.key()>maxR) { maxR=i.key(); } if (i.key()<minR) { minR=i.key(); } } int minG = 255; int maxG = 0; for (i=image->getHistogram()->get(Histogram::GChannel)->constBegin(); i!=image->getHistogram()->get(Histogram::GChannel)->constEnd();++i) { if (i.value()!=0 && i.key()>maxG) { maxG=i.key(); } if (i.value()!=0 && i.key()<minG) { minG=i.key(); } } int minB = 255; int maxB = 0; for (i=image->getHistogram()->get(Histogram::BChannel)->constBegin(); i!=image->getHistogram()->get(Histogram::BChannel)->constEnd();++i) { if (i.value()!=0 && i.key()>maxB) { maxB=i.key(); } if (i.value()!=0 && i.key()<minB) { minB=i.key(); } } int minL = 255; int maxL = 0; for (i=image->getHistogram()->get(Histogram::LChannel)->constBegin(); i!=image->getHistogram()->get(Histogram::LChannel)->constEnd();++i) { if (i.value()!=0 && i.key()>maxL) { maxL=i.key(); } if (i.value()!=0 && i.key()<minL) { minL=i.key(); } } if (image->format() == QImage::Format_Indexed8) { for (int x=0; x<width; x++) { for (int y=0; y<height; y++) { QRgb pixel = image->pixel(x,y); // Getting the pixel(x,y) value int l = qGray(pixel); // Get the 0-255 value of the L channel l = (255/(maxL-minL))*(l-minL); newImage->setPixel(x,y, l); } } } else { for (int x=0; x<width; x++) { for (int y=0; y<height; y++) { QRgb pixel = image->pixel(x,y); // Getting the pixel(x,y) value int r = qRed(pixel); // Get the 0-255 value of the R channel r = (255/(maxR-minR))*(r-minR); int g = qGreen(pixel); // Get the 0-255 value of the G channel g = (255/(maxG-minG))*(g-minG); int b = qBlue(pixel); // Get the 0-255 value of the B channel b = (255/(maxB-minB))*(b-minB); QColor newPixel = QColor(r,g,b); newImage->setPixel(x,y, newPixel.rgb()); } } } return newImage; }
/** Does the convolution process for all pixels using the given mask. */ PNM* Convolution::convolute(math::matrix<double> mask, Mode mode = RepeatEdge) { int width = image->width(), height = image->height(); PNM* newImage = new PNM(width, height, image->format()); if (image->format() == QImage::Format_Indexed8) { for (int x=0; x<width; x++) { for (int y=0; y<height; y++) { double weight = sum(mask); int size = mask.RowNo(); QRgb pixel = image->pixel(x,y); // Getting the pixel(x,y) value int l = qGray(pixel); // Get the 0-255 value of the L channel math::matrix<double> window = Transformation::getWindow(x,y, size, LChannel, mode); math::matrix<double> accumulator = join(window, mask); double sumAc = sum(accumulator); if (weight!=0) { sumAc/=weight; } l = sumAc; l = (l>PIXEL_VAL_MAX ? PIXEL_VAL_MAX : l); l = (l<PIXEL_VAL_MIN ? PIXEL_VAL_MIN : l); newImage->setPixel(x,y, l); } } } else { // Iterate over image space for (int x=0; x<width; x++) { for (int y=0; y<height; y++) { double weight = sum(mask); int size = mask.RowNo(); QRgb pixel = image->pixel(x,y); // Getting the pixel(x,y) value int r; // Get the 0-255 value of the R channel int g; // Get the 0-255 value of the G channel int b; // Get the 0-255 value of the B channel math::matrix<double> window = Transformation::getWindow(x,y, size, RChannel, mode); math::matrix<double> accumulator = join(window, mask); double sumAc = sum(accumulator); if (weight!=0) { sumAc/=weight; } r = sumAc; r = (r>PIXEL_VAL_MAX ? PIXEL_VAL_MAX : r); r = (r<PIXEL_VAL_MIN ? PIXEL_VAL_MIN : r); window = Transformation::getWindow(x,y, size, GChannel, mode); accumulator = join(window, mask); sumAc = sum(accumulator); if (weight!=0) { sumAc/=weight; } g = sumAc; g = (g>PIXEL_VAL_MAX ? PIXEL_VAL_MAX : g); g = (g<PIXEL_VAL_MIN ? PIXEL_VAL_MIN : g); window = Transformation::getWindow(x,y, size, BChannel, mode); accumulator = join(window, mask); sumAc = sum(accumulator); if (weight!=0) { sumAc/=weight; } b = sumAc; b = (b>PIXEL_VAL_MAX ? PIXEL_VAL_MAX : b); b = (b<PIXEL_VAL_MIN ? PIXEL_VAL_MIN : b); QColor newPixel = QColor(r,g,b); newImage->setPixel(x,y, newPixel.rgb()); } } } return newImage; }