Show histogram & Equalize histogram & Lomography effect & Cartoonize effect

The Original

Show histogram

  1. void showHistoCallback(int state, void* userData)
  2. {
  3.     // Separate image in BRG
  4.     vector<Mat> bgr;
  5.     split( img, bgr );
  6.  
  7.     // Create the histogram for 256 bins
  8.     // The number of possibles values
  9.     int numbins= 256;
  10.  
  11.     /// Set the ranges ( for B,G,R) )
  12.     float range[] = { 0, 256 } ;
  13.     const float* histRange = { range };
  14.  
  15.     Mat b_hist, g_hist, r_hist;
  16.  
  17.     calcHist( &bgr[0], 1, 0, Mat(), b_hist, 1, &numbins, &histRange );
  18.     calcHist( &bgr[1], 1, 0, Mat(), g_hist, 1, &numbins, &histRange );
  19.     calcHist( &bgr[2], 1, 0, Mat(), r_hist, 1, &numbins, &histRange );
  20.  
  21.     // Draw the histogram
  22.     // We go to draw lines for each channel
  23.     int width= 512;
  24.     int height= 300;
  25.     // Create image with gray base
  26.     Mat histImage( height, width, CV_8UC3, Scalar(20,20,20) );
  27.  
  28.     // Normalize the histograms to height of image
  29.     normalize(b_hist, b_hist, 0, height, NORM_MINMAX );
  30.     normalize(g_hist, g_hist, 0, height, NORM_MINMAX );
  31.     normalize(r_hist, r_hist, 0, height, NORM_MINMAX );
  32.  
  33.     int binStep= cvRound((float)width/(float)numbins);
  34.     for( int i=1; i< numbins; i++)
  35.     {
  36.         line( histImage, 
  37.                 Point( binStep*(i-1), height-cvRound(b_hist.at<float>(i-1) ) ),
  38.                 Point( binStep*(i), height-cvRound(b_hist.at<float>(i) ) ),
  39.                 Scalar(255,0,0)
  40.             );
  41.         line( histImage, 
  42.                 Point( binStep*(i-1), height-cvRound(g_hist.at<float>(i-1) ) ),
  43.                 Point( binStep*(i), height-cvRound(g_hist.at<float>(i) ) ),
  44.                 Scalar(0,255,0)
  45.             );
  46.         line( histImage, 
  47.                 Point( binStep*(i-1), height-cvRound(r_hist.at<float>(i-1) ) ),
  48.                 Point( binStep*(i), height-cvRound(r_hist.at<float>(i) ) ),
  49.                 Scalar(0,0,255)
  50.             );
  51.     }
  52.  
  53.     imshow("Histogram", histImage);
  54.  
  55. }

Equalize histogram

  1. void equalizeCallback(int state, void* userData)
  2. {
  3.     Mat result;
  4.     // Convert BGR image to YCbCr
  5.     Mat ycrcb;
  6.     cvtColor( img, ycrcb, COLOR_BGR2YCrCb);
  7.  
  8.     // Split image into channels
  9.     vector<Mat> channels;
  10.     split( ycrcb, channels );
  11.  
  12.     // Equalize the Y channel only
  13.     equalizeHist( channels[0], channels[0] );
  14.  
  15.     // Merge the result channels
  16.     merge( channels, ycrcb );
  17.  
  18.     // Convert color ycrcb to BGR
  19.     cvtColor( ycrcb, result, COLOR_YCrCb2BGR );
  20.  
  21.     // Show image
  22.     imshow("Equalized", result);
  23. }

Lomography effect

  1. void lomoCallback(int state, void* userData)
  2. {
  3.     Mat result;
  4.  
  5.     const double E = std::exp(1.0);
  6.     // Create Lookup table for color curve effect
  7.     Mat lut(1, 256, CV_8UC1);
  8.     for (int i=0; i<256; i++)
  9.     {
  10.         float x= (float)i/256.0; 
  11.         lut.at<uchar>(i)= cvRound( 256 * (1/(1 + pow(E, -((x-0.5)/0.1)) )) );
  12.     }
  13.  
  14.     // Split the image channels and apply curve transform only to red channel
  15.     vector<Mat> bgr;
  16.     split(img, bgr);
  17.     LUT(bgr[2], lut, bgr[2]);
  18.     // merge result
  19.     merge(bgr, result);
  20.  
  21.     // Create image for halo dark
  22.     Mat halo( img.rows, img.cols, CV_32FC3, Scalar(0.3,0.3,0.3) );
  23.     // Create circle 
  24.     circle(halo, Point(img.cols/2, img.rows/2), img.cols/3, Scalar(1,1,1), -1); 
  25.     blur(halo, halo, Size(img.cols/3, img.cols/3));
  26.  
  27.     // Convert the result to float to allow multiply by 1 factor
  28.     Mat resultf;
  29.     result.convertTo(resultf, CV_32FC3);
  30.  
  31.     // Multiply our result with halo
  32.     multiply(resultf, halo, resultf);
  33.  
  34.     // convert to 8 bits
  35.     resultf.convertTo(result, CV_8UC3);
  36.  
  37.     // show result
  38.     imshow("Lomograpy", result);
  39.  
  40.     // Release mat memory
  41.     halo.release();
  42.     resultf.release();
  43.     lut.release();
  44.     bgr[0].release();
  45.     bgr[1].release();
  46.     bgr[2].release();
  47. }

Cartoonize effect

  1. void cartoonCallback(int state, void* userData)
  2. {
  3.     /** EDGES **/
  4.     // Apply median filter to remove possible noise
  5.     Mat imgMedian;
  6.     medianBlur(img, imgMedian, 7);
  7.  
  8.     // Detect edges with canny
  9.     Mat imgCanny;
  10.     Canny(imgMedian, imgCanny, 50, 150);
  11.  
  12.     // Dilate the edges
  13.     Mat kernel= getStructuringElement(MORPH_RECT, Size(2,2));
  14.     dilate(imgCanny, imgCanny, kernel);
  15.  
  16.     // Scale edges values to 1 and invert values
  17.     imgCanny= imgCanny/255;
  18.     imgCanny= 1-imgCanny;
  19.  
  20.     // Use float values to allow multiply between 0 and 1
  21.     Mat imgCannyf;
  22.     imgCanny.convertTo(imgCannyf, CV_32FC3);
  23.  
  24.     // Blur the edgest to do smooth effect
  25.     blur(imgCannyf, imgCannyf, Size(5,5));
  26.  
  27.     /** COLOR **/
  28.     // Apply bilateral filter to homogenizes color
  29.     Mat imgBF;
  30.     bilateralFilter(img, imgBF, 9, 150.0, 150.0);
  31.  
  32.     // truncate colors
  33.     Mat result= imgBF/25;
  34.     result= result*25;
  35.  
  36.     /** MERGES COLOR + EDGES **/
  37.     // Create a 3 channles for edges
  38.     Mat imgCanny3c;
  39.     Mat cannyChannels[]={ imgCannyf, imgCannyf, imgCannyf};
  40.     merge(cannyChannels, 3, imgCanny3c);
  41.  
  42.     // Convert color result to float 
  43.     Mat resultf;
  44.     result.convertTo(resultf, CV_32FC3);
  45.  
  46.     // Multiply color and edges matrices
  47.     multiply(resultf, imgCanny3c, resultf);
  48.  
  49.     // convert to 8 bits color
  50.     resultf.convertTo(result, CV_8UC3);
  51.  
  52.     // Show image
  53.     imshow("Result", result);
  54.  
  55. }

Leave a Reply

Your email address will not be published. Required fields are marked *