Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <tbb/task_scheduler_init.h>
- #include <tbb/parallel_for.h>
- #include <tbb/blocked_range.h>
- #include <tbb/blocked_range2d.h>
- #include <tbb/parallel_reduce.h>
- #include <FreeImagePlus.h>
- #include <math.h>
- #include <tbb/tick_count.h>
- using namespace std;
- using namespace tbb;
- void Part1();
- void Part2();
- int main()
- {
- //Part 1 (Greyscale Gaussian blur): -----------DO NOT REMOVE THIS COMMENT----------------------------//
- Part1();
- //Part 2 (Colour image processing): -----------DO NOT REMOVE THIS COMMENT----------------------------//
- Part2();
- return 0;
- }
- void Part1()
- {
- fipImage inputImage;
- inputImage.load("../Images/render_1.png");
- inputImage.convertToFloat();
- float *inputBuffer = (float*)inputImage.accessPixels();
- const int width = inputImage.getWidth();
- const int height = inputImage.getHeight();
- fipImage outputImage;
- outputImage = fipImage(FIT_FLOAT, width, height, 32);
- float *outputBuffer = (float*)outputImage.accessPixels();
- const int kernel = 7;
- const int bounds = kernel/2;
- fipImage outputGrayImage;
- outputGrayImage = fipImage(FIT_FLOAT, width, height, 32);
- float *outputGrayBuffer = (float*)outputGrayImage.accessPixels();
- //sequential
- for (auto y = 0; y < height; y++){
- for (auto x = 0; x < width; x++){
- for (auto i = 0; i < kernel; i++){
- for (auto j = 0; j < kernel; j++){
- outputGrayBuffer[y * width + x] = inputBuffer[y * width + x];
- }
- }
- }
- }
- float sigma = 10.0f;
- cout << "saving" << endl;
- for (auto y = bounds; y < height; y++){
- for (auto x = bounds; x < width; x++){
- for (auto i = bounds; i < kernel; i++){
- for (auto j = bounds; j < kernel; j++){
- float u = float(-(width) + x);
- float v = float(-(height) + y);
- outputBuffer[y * width + x] = 1.0f / (2.0f * float(M_PI) * pow(sigma, 2) * exp(-((pow(u, 2) + (pow(v, 2))))) / (2.0f * (pow(sigma, 2))));
- }
- }
- }
- }
- //parallel for
- parallel_for(blocked_range2d<int, int>(0, height, 0, width), [=](const blocked_range2d<int, int>& r) {
- for (auto y = r.rows().begin(); y < r.rows().end(); ++y) {
- for (auto x = r.cols().begin(); x < r.cols().end(); ++x) {
- float u = float(-(width >> 1) + x);
- float v = float(-(height >> 1) + y);
- outputBuffer[y * width + x] = 1.0f / (2.0f * float(M_PI) * sigma * sigma) * exp(-((u * u + v * v) / (2.0f * sigma * sigma)));
- }
- }
- });
- kernel void blur(read_only image2d_t)
- //parallel for
- /*
- parallel_for(blocked_range2d<int, int>(0, height, 0, width), [=](const blocked_range2d<int, int>& r) {
- for (auto y = r.rows().begin(); y < r.rows().end(); ++y) {
- for (auto x = r.cols().begin(); x < r.cols().end(); ++x) {
- float u = float(-(width >> 1) + x);
- float v = float(-(height >> 1) + y);
- outputBuffer[y * width + x] = 1.0f / (2.0f * float(M_PI) * sigma * sigma) * exp(-((u * u + v * v) / (2.0f * sigma * sigma)));
- }
- }
- });
- */
- std::cout << "Saving image...\n";
- outputImage.convertToType(FREE_IMAGE_TYPE::FIT_BITMAP);
- outputImage.convertTo24Bits();
- outputImage.save("grey_blurred.png");
- std::cout << "...done\n\n";
- }
- void Part2()
- {
- fipImage inputImage[2];
- inputImage[0].load("../Images/render_1.png");
- inputImage[1].load("../Images/render_2.png");
- unsigned int width = inputImage[0].getWidth();
- unsigned int height = inputImage[0].getHeight();
- // Setup Output image array
- fipImage outputImage;
- outputImage = fipImage(FIT_BITMAP, width, height, 24);
- vector<vector<RGBQUAD>> rgbValues;
- rgbValues.resize(height, vector<RGBQUAD>(width));
- float pixels = height * width;
- int value;
- cout << "binary threshold: ";
- cin >> value;
- //FreeImage structure to hold RGB values of a single pixel
- parallel_for(blocked_range2d<int, int>(0, height, 0, width), [&] (blocked_range2d<int, int> &range){
- //2D Vector to hold the RGB colour data of an image
- for (int x = range.cols().begin(); x < range.cols().end(); ++x){
- for (int y = range.rows().begin(); y < range.rows().end(); ++y){
- RGBQUAD rgb[2];
- inputImage[0].getPixelColor(x, y, &rgb[0]); //Extract pixel(x,y) colour data and place it in rgb
- inputImage[1].getPixelColor(x, y, &rgb[1]);
- //Extract colour data from image and store it as individual RGBQUAD elements for every pixel
- if (abs(rgb[0].rgbRed - rgb[1].rgbRed) >= value ||
- abs(rgb[0].rgbBlue - rgb[1].rgbBlue) >= value ||
- abs(rgb[0].rgbGreen - rgb[1].rgbGreen) >= value){
- rgbValues[y][x].rgbRed = 255;
- rgbValues[y][x].rgbGreen = 255;
- rgbValues[y][x].rgbBlue = 255;
- }
- //Place the pixel colour values into output image
- outputImage.setPixelColor(x, y, &rgbValues[y][x]);
- }
- }
- });
- int white = parallel_reduce(blocked_range2d<int, int>(0, height, 0, width), 0, [&](blocked_range2d< int, int> & range, int count) ->int {
- for (int x = range.cols().begin(); x < range.cols().end(); x++){
- for (int y = range.rows().begin(); y <range.rows().end(); y++){
- if (rgbValues[y][x].rgbRed != 0 && rgbValues[y][x].rgbGreen != 0 && rgbValues[y][x].rgbBlue != 0){
- count++;
- }\
- }
- }
- return count;
- }, [&](int x, int y) -> int {return x + y;}
- );
- float percentage = white / pixels * 100;
- cout << "Percentage of white pixels: "<< percentage << "%" << endl;
- //Save the processed image
- outputImage.save("RGB_processed.png");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement