Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <tbb/task_scheduler_init.h>
  4. #include <tbb/parallel_for.h>
  5. #include <tbb/blocked_range.h>
  6. #include <tbb/blocked_range2d.h>
  7. #include <tbb/parallel_reduce.h>
  8. #include <FreeImagePlus.h>
  9. #include <math.h>
  10. #include <tbb/tick_count.h>
  11.  
  12. using namespace std;
  13. using namespace tbb;
  14. void Part1();
  15. void Part2();
  16.  
  17. int main()
  18. {
  19.     //Part 1 (Greyscale Gaussian blur): -----------DO NOT REMOVE THIS COMMENT----------------------------//
  20.     Part1();
  21.     //Part 2 (Colour image processing): -----------DO NOT REMOVE THIS COMMENT----------------------------//
  22.     Part2();
  23.     return 0;
  24. }
  25. void Part1()
  26. {
  27.     fipImage inputImage;
  28.     inputImage.load("../Images/render_1.png");
  29.     inputImage.convertToFloat();
  30.     float *inputBuffer = (float*)inputImage.accessPixels();
  31.  
  32.     const int width = inputImage.getWidth();
  33.     const int height = inputImage.getHeight();
  34.  
  35.     fipImage outputImage;
  36.     outputImage = fipImage(FIT_FLOAT, width, height, 32);
  37.     float *outputBuffer = (float*)outputImage.accessPixels();
  38.  
  39.     const int kernel = 7;
  40.     const int bounds = kernel/2;
  41.  
  42.     fipImage outputGrayImage;
  43.     outputGrayImage = fipImage(FIT_FLOAT, width, height, 32);
  44.     float *outputGrayBuffer = (float*)outputGrayImage.accessPixels();
  45.     //sequential
  46.     for (auto y = 0; y < height; y++){
  47.         for (auto x = 0; x < width; x++){
  48.             for (auto i = 0; i < kernel; i++){
  49.                 for (auto j = 0; j < kernel; j++){
  50.                     outputGrayBuffer[y * width + x] = inputBuffer[y * width + x];
  51.                 }
  52.             }
  53.         }
  54.     }
  55.     float sigma = 10.0f;
  56.     cout << "saving" << endl;
  57.     for (auto y = bounds; y < height; y++){
  58.         for (auto x = bounds; x < width; x++){
  59.             for (auto i = bounds; i < kernel; i++){
  60.                 for (auto j = bounds; j < kernel; j++){
  61.                     float u = float(-(width) + x);
  62.                     float v = float(-(height) + y);
  63.  
  64.                     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))));
  65.                 }
  66.             }
  67.         }
  68.     }
  69.     //parallel for
  70.     parallel_for(blocked_range2d<int, int>(0, height, 0, width), [=](const blocked_range2d<int, int>& r) {
  71.  
  72.         for (auto y = r.rows().begin(); y < r.rows().end(); ++y) {
  73.  
  74.             for (auto x = r.cols().begin(); x < r.cols().end(); ++x) {
  75.  
  76.                 float u = float(-(width >> 1) + x);
  77.                 float v = float(-(height >> 1) + y);
  78.  
  79.                 outputBuffer[y * width + x] = 1.0f / (2.0f * float(M_PI) * sigma * sigma) * exp(-((u * u + v * v) / (2.0f * sigma * sigma)));
  80.             }
  81.         }
  82.     });
  83.  
  84.     kernel void blur(read_only image2d_t)
  85.     //parallel for
  86.     /*
  87.     parallel_for(blocked_range2d<int, int>(0, height, 0, width), [=](const blocked_range2d<int, int>& r) {
  88.  
  89.         for (auto y = r.rows().begin(); y < r.rows().end(); ++y) {
  90.  
  91.             for (auto x = r.cols().begin(); x < r.cols().end(); ++x) {
  92.  
  93.                 float u = float(-(width >> 1) + x);
  94.                 float v = float(-(height >> 1) + y);
  95.  
  96.                 outputBuffer[y * width + x] = 1.0f / (2.0f * float(M_PI) * sigma * sigma) * exp(-((u * u + v * v) / (2.0f * sigma * sigma)));
  97.             }
  98.         }
  99.     });
  100. */
  101.  
  102.     std::cout << "Saving image...\n";
  103.     outputImage.convertToType(FREE_IMAGE_TYPE::FIT_BITMAP);
  104.     outputImage.convertTo24Bits();
  105.     outputImage.save("grey_blurred.png");
  106.  
  107.     std::cout << "...done\n\n";
  108.  
  109.  
  110.  
  111. }
  112. void Part2()
  113. {
  114.     fipImage inputImage[2];
  115.     inputImage[0].load("../Images/render_1.png");
  116.     inputImage[1].load("../Images/render_2.png");
  117.  
  118.     unsigned int width = inputImage[0].getWidth();
  119.     unsigned int height = inputImage[0].getHeight();
  120.  
  121.     // Setup Output image array
  122.     fipImage outputImage;
  123.     outputImage = fipImage(FIT_BITMAP, width, height, 24);
  124.  
  125.     vector<vector<RGBQUAD>> rgbValues;
  126.     rgbValues.resize(height, vector<RGBQUAD>(width));
  127.  
  128.     float pixels = height * width;
  129.     int value;
  130.     cout << "binary threshold: ";
  131.     cin >> value;
  132.     //FreeImage structure to hold RGB values of a single pixel
  133.  
  134.     parallel_for(blocked_range2d<int, int>(0, height, 0, width), [&] (blocked_range2d<int, int> &range){
  135.         //2D Vector to hold the RGB colour data of an image
  136.         for (int x = range.cols().begin(); x < range.cols().end(); ++x){
  137.             for (int y = range.rows().begin(); y < range.rows().end(); ++y){
  138.                 RGBQUAD rgb[2];
  139.  
  140.                 inputImage[0].getPixelColor(x, y, &rgb[0]); //Extract pixel(x,y) colour data and place it in rgb
  141.                 inputImage[1].getPixelColor(x, y, &rgb[1]);
  142.  
  143.                 //Extract colour data from image and store it as individual RGBQUAD elements for every pixel
  144.                 if (abs(rgb[0].rgbRed - rgb[1].rgbRed) >= value ||
  145.                     abs(rgb[0].rgbBlue - rgb[1].rgbBlue) >= value ||
  146.                     abs(rgb[0].rgbGreen - rgb[1].rgbGreen) >= value){
  147.                     rgbValues[y][x].rgbRed = 255;
  148.                     rgbValues[y][x].rgbGreen = 255;
  149.                     rgbValues[y][x].rgbBlue = 255;
  150.                 }
  151.                 //Place the pixel colour values into output image
  152.                 outputImage.setPixelColor(x, y, &rgbValues[y][x]);
  153.             }
  154.         }
  155.     });
  156.     int white = parallel_reduce(blocked_range2d<int, int>(0, height, 0, width), 0, [&](blocked_range2d< int, int> & range, int count) ->int {
  157.                                     for (int x = range.cols().begin(); x < range.cols().end(); x++){
  158.                                         for (int y = range.rows().begin(); y <range.rows().end(); y++){
  159.                                             if (rgbValues[y][x].rgbRed != 0 && rgbValues[y][x].rgbGreen != 0 && rgbValues[y][x].rgbBlue != 0){
  160.                                                 count++;
  161.                                             }\
  162.                                         }
  163.                                     }
  164.                                     return count;
  165.                                 }, [&](int x, int y) -> int {return x + y;}
  166.     );
  167.  
  168.  
  169.     float percentage = white / pixels * 100;
  170.     cout << "Percentage of white pixels: "<< percentage << "%" << endl;
  171.     //Save the processed image
  172.     outputImage.save("RGB_processed.png");
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement