void YellowColorFilter::filterImage(ImageRGB & image) { // Image size; int width = image.width(); int height = image.height(); // For every pixel in the image for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { // Get the RGB values Rgb<unsigned char &> pixelRGB = image.at(x, y); // To save the Hue, Saturation and Value float hue, saturation, value; // Convert RGB to HSV. RGB2HSV(pixelRGB.red, pixelRGB.green, pixelRGB.blue, hue, saturation, value); // If the color is yellow. if (hue >= 25 && hue <= 60 && saturation >= 0.60) { // If the color is within our yellow range, make the output pixel white. pixelRGB.red = 255; pixelRGB.green = 255; pixelRGB.blue = 255; } else { // Else make the pixel black. pixelRGB.red = 0; pixelRGB.green = 0; pixelRGB.blue = 0; } } } }
ImageGray GrayscaleImage::convertToGrayscale(ImageRGB & image) { // Image size. int width = image.width(); int height = image.height(); // The destination image. ImageGray grayImage(width, height); // For every pixel in the image. for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { // Get the pixel at position x,y. Rgb<unsigned char &> pixelRGB = image.at(x, y); unsigned char& pixelGray = grayImage.at(x, y); // Convert RGB to grayscale. pixelGray = pixelRGB.red * 0.114 + pixelRGB.green * 0.587 + pixelRGB.blue * 0.299; } } // Return the gray image. return grayImage; }
void saveImg(const ImageRGB & img, const std::string filename) { CImg<unsigned char> cimg(img.data(0, 0, Channel::Red), img.width(), img.height(), 1, 3); cimg.save(filename.c_str()); }
std::unique_ptr<ImageGray> thresholdDetermination::convert(const ImageRGB& img){ std::unique_ptr<ImageGray> returnImage = std::make_unique<ImageGray>(img.width(), img.height()); meanCorners = (getIntensity(convertToHex(img.at(0, 0).red, img.at(0, 0).green, img.at(0, 0).blue)) + getIntensity(convertToHex(img.at(img.width() - 1, 0).red, img.at(img.width() - 1, 0).green, img.at(img.width() - 1, 0).blue)) + getIntensity(convertToHex(img.at(0, img.height() - 1).red, img.at(0, img.height() - 1).green, img.at(0, img.height() - 1).blue))) + getIntensity(convertToHex(img.at(img.width() - 1, img.height() - 1).red, img.at(img.width() - 1, img.height() - 1).green, img.at(img.width() - 1, img.height() - 1).blue)) / 4; meanAllOthers = 0; for (int h = 0; h < img.height(); ++h) { for (int w = 0; w < img.width(); ++w) { if (!((w == 0 && h == 0) || (w == img.width() - 1 && h == 0) || (w == 0 && h == img.height() - 1) || (w == img.width() - 1 && h == img.height() - 1))) { meanAllOthers += getIntensity(convertToHex(img.at(w, h).red, img.at(w, h).green, img.at(w, h).blue)); } } } meanAllOthers /= ((img.width()*img.height()) - 4); tOld = 0; tNew = (meanCorners + meanAllOthers) / 2; unsigned int u1count = 0, u2count = 0; while (tNew != tOld) { meanAllOthers = 0; meanCorners = 0; for (int h = 0; h < img.height(); ++h) { for (int w = 0; w < img.width(); ++w) { if (getIntensity(convertToHex(img.at(w, h).red, img.at(w, h).green, img.at(w, h).blue)) < tNew){ meanCorners += getIntensity(convertToHex(img.at(w, h).red, img.at(w, h).green, img.at(w, h).blue)); u1count++; } else if (getIntensity(convertToHex(img.at(w, h).red, img.at(w, h).green, img.at(w, h).blue)) >= tNew){ meanAllOthers += getIntensity(convertToHex(img.at(w, h).red, img.at(w, h).green, img.at(w, h).blue)); u2count++; } } } if (u1count != 0){ meanCorners /= u1count; // HOTFIX - lars u1count kan nul zijn.... } if (u2count != 0){ meanAllOthers /= u2count; // HOTFIX - lars u2count kan nul zijn.... } u1count = 0; u2count = 0; tOld = tNew; tNew = (meanCorners + meanAllOthers) / 2; } for (int h = 0; h < returnImage->height(); ++h) { for (int w = 0; w < returnImage->width(); ++w) { if (getIntensity(convertToHex(img.at(w, h).red, img.at(w, h).green, img.at(w, h).blue)) < tNew){ returnImage->at(w, h) = 0; } else if (getIntensity(convertToHex(img.at(w, h).red, img.at(w, h).green, img.at(w, h).blue)) >= tNew){ returnImage->at(w, h) = 255; } } } return returnImage; }