void createMasks(const RGBImage& img) { cout << "creating masks ..." << endl; int w = img.width(), h = img.height(); GrayScaleImagef I(w, h); for(int i=0;i<h;i++) { for(int j=0;j<w;j++) { RGBImage::pixel_t pix = img.getPixel(j, i); if( pix.r > 0 ) { I.setPixel(j, i, 1.0); //cout << (int)pix.r << endl; } else { //cout << (int)pix.r << endl; I.setPixel(j, i, 0.0); } } } float invSamples = 1.0 / (samples * samples); float step = 1.0 / samples; // create a set of masks for(int i=0;i<256;i++) { cout << "level " << i << endl; float maskSize = i+1; float topY = 0.5 * (h - maskSize); float bottomY = topY + maskSize; float leftX = topY; float rightX = bottomY; GrayScaleImagef m(w, h); for(int i=0;i<w;i++) { int y = i; for(int j=0;j<h;j++) { int x = j; //cout << i << "," << j << endl; int cnt = 0; for(int ny=0;ny<samples;ny++) { float yy = ny * step + y; float yyy =(yy-topY) / maskSize; for(int nx=0;nx<samples;nx++) { float xx = nx * step + x; float xxx = (xx-leftX) / maskSize; if( xxx >= 0 && xxx < 1.0 && yyy >= 0 && yyy < 1.0 ) { GrayScaleImage::pixel_t pix = I.sample(xxx*(w-1), yyy*(h-1)); cnt += pix; } } } m.setPixel(j, i, cnt * invSamples); } } masks.push_back(m); } cout << "masks created!" << endl; }
RGBImageStudent::RGBImageStudent(const RGBImage &other) : RGBImage(other.getWidth(), other.getHeight()), pixelMap(nullptr) { const int SIZE = other.getSize(); if (SIZE > 0) { pixelMap = new RGB[SIZE]; for (int i = 0; i < SIZE; i++) { pixelMap[i] = other.getPixel(i); } } }
void RGBImageStudent::set(const RGBImage &other) { const int SIZE = other.getSize(); RGBImage::set(other.getWidth(), other.getHeight()); if (SIZE > 0) { delete[] pixelMap; pixelMap = new RGB[SIZE]; for (int i = 0; i < SIZE; i++) { pixelMap[i] = other.getPixel(i); } } }
void HereBeDragons::ToStandInThyAffairsFallByThySide(const RGBImage &src, cv::Mat &dst) { int w = src.getWidth(); int h = src.getHeight(); dst.create(h, w, CV_8UC3); for (int x = 0; x < dst.cols; x++) { for (int y = 0; y < dst.rows; y++) { RGB color = src.getPixel(x, y); dst.at<cv::Vec3b>(y, x) = cv::Vec3b(color.b, color.g, color.r); } } }
void LuminosityAlgorithm::doAlgorithm(const RGBImage& input, IntensityImage& output) { // Check image size if (input.getWidth() != output.getWidth() || input.getHeight() != output.getHeight()) { output.set(input.getWidth(), input.getHeight()); } // Lunimosity Algorithm defined as // Gray = (Red * 0.2126 + Green * 0.7152 + Blue * 0.0722) for (int i = 0; i < input.getWidth()*input.getHeight(); i++) { RGB pixel = input.getPixel(i); output.setPixel(i, pixel.r * 0.2126 + pixel.g * 0.7152 + pixel.b * 0.0722); } }
void saveImage(const RGBImage& img, const string& filename) { int w = img.width(); int h = img.height(); QImage I(w, h, QImage::Format_ARGB32); for(int y=0;y<h;y++) { for(int x=0;x<w;x++) { RGBImage::pixel_t pix = img.getPixel(x, y); I.setPixel(x, y, qRgb(pix.r, pix.g, pix.b)); } } I.save(filename.c_str()); }
void fillBlock(int x0, int y0, int x1, int y1, const RGBImage& img, RGBImage& I) { // compute average color float rSum = 0, gSum = 0, bSum = 0; for(int y=y0;y<y1;y++) { for(int x=x0;x<x1;x++) { RGBImage::pixel_t pix = img.getPixel(x, y); rSum += pix.r; gSum += pix.g; bSum += pix.b; } } float H = y1 - y0; float W = x1 - x0; float invA = 1.0 / (H * W); // compute average brightness float rAvg = rSum * invA, gAvg = gSum * invA, bAvg = bSum * invA; int lev = (int)Utils::clamp<float>(0.2989*rAvg + 0.5870*gAvg + 0.1140*bAvg, 128.f, 255.f); const GrayScaleImagef& m = masks[lev]; float hFactor = (m.height() - 1) / H; float wFactor = (m.width() - 1) / W; float invSamples = 1.0 / (samples * samples); float step = 1.0 / samples; // apply the mask for(int y=y0, i=0;y<y1;y++,i++) { float yy = (y - y0); for(int x=x0, j=0;x<x1;x++, j++) { float xx = (x - x0); float mVal = 0; for(int ny=0;ny<samples;ny++) { float yyy = yy + ny * step; for(int nx=0;nx<samples;nx++) { float xxx = xx + nx * step; mVal += m.sample(xxx * wFactor, yyy * hFactor); } } mVal *= invSamples; //cout << mVal << endl; // color I.setPixel(x, y, RGBPixel(rAvg, gAvg, bAvg) * mVal); } } }