Obrazy kolorowe

RGB 

Rozkład na poszczegulne kanały RGB:

#include <cstdlib>
#include <iostream>
#include <string>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;

int main(){     
  Mat img = imread("./LENA_512.jpg", 1 );
  Mat img_R = img.clone();
  Mat img_G = img.clone();
  Mat img_B = img.clone();
  for (int row=0; row < img.rows; row++){
    for (int col=0; col < img.cols; col++){
      Vec3b intensity = img.at(row, col);
      uchar blue = intensity.val[0];
      uchar green = intensity.val[1];
      uchar red = intensity.val[2];
      img_B.at(row,col)[0] = blue;
      img_B.at(row,col)[1] = 0;
      img_B.at(row,col)[2] = 0;

      img_G.at(row,col)[0] = 0;
      img_G.at(row,col)[1] = green;
      img_G.at(row,col)[2] = 0;

      img_R.at(row,col)[0] = 0;
      img_R.at(row,col)[1] = 0;
      img_R.at(row,col)[2] = red;
    }
  }
  namedWindow( "Blue" , WINDOW_AUTOSIZE );
  imshow( "Blue" , img_B );             
  namedWindow( "Green" , WINDOW_AUTOSIZE );
  imshow( "Green" , img_G );
  namedWindow( "Red" , WINDOW_AUTOSIZE );
  imshow( "Red" , img_R );
  waitKey(); 
  return 0;   
}

W OpecCV możemy wyodrębnić kanały Red, Green, Blue za pomocą funkcji  split()

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;

int main(){  
  Mat bgr_img = imread("./LENA_512.jpg", 1 );
  Mat grey_img;
  
  vector<Mat> bgr_vec(3);
  split(bgr_img, bgr_vec);
  Mat& blue_img = bgr_vec[0];
  Mat& green_img = bgr_vec[1];
  Mat& red_img = bgr_vec[2];

  namedWindow( "Blue", WINDOW_AUTOSIZE );// Create a window for display.
  imshow( "Blue", blue_img );
  namedWindow( "Green", WINDOW_AUTOSIZE );// Create a window for display.
  imshow( "Green", green_img ); 
  namedWindow( "Red", WINDOW_AUTOSIZE );// Create a window for display.
  imshow( "Red", red_img );   
  waitKey(); 
  return 0;   
}  

Konwersja do obrazu w odcieniach szarość 

Można użyć wbudowanej funkcji OpenCV cvtColor(img, grey_img, CV_BGR2GRAY);

#include <cstdlib>
#include <iostream>
#include <string>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(){  
    Mat img = imread("./LENA_512.jpg", 1 );
    Mat grey_img;
    cvtColor(img, grey_img, CV_BGR2GRAY);
    namedWindow( "Grey", WINDOW_AUTOSIZE );
    imshow( "Grey", grey_img );   
    waitKey(); 
    return 0;   
}

Można też napisać tą operację ręcznie. 

#include <cstdlib>
#include <iostream>
#include <string>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(){     
  Mat img = imread("/home/przemek/Pulpit/openCVExamples/LENA_512.jpg", 1 );
  Mat grey_img = img.clone();
  for (int row=0; row < img.rows; row++){
    for (int col=0; col < img.cols; col++){
      Vec3b intensity = img.at(row, col);
      uchar blue = intensity.val[0];
      uchar green = intensity.val[1];
      uchar red = intensity.val[2];
      grey_img.at(row,col)[0] = 0.299*red+0.587*green + 0.114*blue;
      grey_img.at(row,col)[1] = 0.299*red+0.587*green + 0.114*blue;
      grey_img.at(row,col)[2] = 0.299*red+0.587*green + 0.114*blue;
    }
  }
  imshow( "Grey" , grey_img );
  waitKey(); 
  return 0;   
}

YUV

W OpenCV możemy łatwo przekontrastować obraz na format YUV: 

#include <cstdlib>
#include <iostream>
#include <string>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(){
  Mat img = imread("./LENA_512.jpg", 1 );
  Mat yuv_img;
  cvtColor(img, yuv_img, CV_BGR2YUV);
  namedWindow( "YUV", WINDOW_AUTOSIZE );
  imshow( "YUV", yuv_img );   
  waitKey(); 
  return 0;
}

Poszczególne kanały możemy wyodrębnić za pomocą poniższego programu: 

#include <cstdlib>
#include <iostream>
#include <string>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(){  
  Mat img = imread("./LENA_512.jpg", 1 );
  Mat yuv_img;
  cvtColor(img, yuv_img, CV_BGR2YUV);
  
  vector<Mat> yuv_vec(3);
  split(yuv_img, yuv_vec);
  Mat& Y_img = yuv_vec[0];
  Mat& U_img = yuv_vec[1];
  Mat& V_img = yuv_vec[2];

  namedWindow( "Y", WINDOW_AUTOSIZE );
  imshow( "Y", Y_img );
  namedWindow( "U", WINDOW_AUTOSIZE );
  imshow( "U", U_img ); 
  namedWindow( "V", WINDOW_AUTOSIZE );
  imshow( "V", V_img );   
  waitKey(); 
  return 0;   
}

YCbCr 

Poszczególne kanały formatu YCbCr możemy wyodrębnić za pomocą poniższego programu:

#include <cstdlib>
#include <iostream>
#include <string>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(){  
  Mat img = imread("./LENA_512.jpg", 1 );
  Mat ycrcb_img;
  cvtColor(img, ycrcb_img, CV_BGR2YCrCb);

  vector<Mat> ycrcb_vec(3);
  split(ycrcb_img, ycrcb_vec);
  Mat& Y_img = ycrcb_vec[0];
  Mat& Cb_img = ycrcb_vec[1];
  Mat& Cr_img = ycrcb_vec[2];
 
  namedWindow( "Y", WINDOW_AUTOSIZE );
  imshow( "Y", Y_img );
  namedWindow( "Cb", WINDOW_AUTOSIZE );
  imshow( "Cb", Cb_img ); 
  namedWindow( "Cr", WINDOW_AUTOSIZE );
  imshow( "Cr", Cr_img );   
  waitKey(); 
  return 0;   
}

HLS 

Aby przekonwertować obraz z formatu RGB na HLS w OpenCV można użyć następującego kodu: 

#include <cstdlib>
#include <iostream>
#include <string>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(){
  Mat img = imread("./LENA_512.jpg", 1 );
  Mat hls_img;
  cvtColor(img, hls_img, CV_BGR2HLS);
  namedWindow( "HLS", WINDOW_AUTOSIZE );
  imshow( "HLS", hls_img );   
  waitKey(); 
  return 0;
}

Poszczególne kanały w formacie HLS można wyodrębnić za pomocą poniższego kodu:  

#include <cstdlib>
#include <iostream>
#include <string>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(){  
    Mat img = imread("./LENA_512.jpg", 1 );
    Mat hls_img;
    cvtColor(img, hls_img, CV_BGR2HLS);
  
    vector<Mat> hls_vec(3);
    split(hls_img, hls_vec);
    Mat& H_img = hls_vec[0];
    Mat& L_img = hls_vec[1];
    Mat& S_img = hls_vec[2];

    namedWindow( "H", WINDOW_AUTOSIZE );
    imshow( "H", H_img );
    namedWindow( "L", WINDOW_AUTOSIZE );
    imshow( "L", L_img ); 
    namedWindow( "S", WINDOW_AUTOSIZE );
    imshow( "S", S_img );   
    waitKey(); 
    return 0;   
}

Detekcja skóry

Cod umożliwiający detekcję skóry:

#include <cstdlib>
#include <iostream>
#include <string>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(){
  Mat img = imread("./face1.jpg", 1 );
  Mat hls_img;
  cvtColor(img, hls_img, CV_BGR2HLS);
  for (int row=0; row < img.rows; row++){
    for (int col=0; col < img.cols; col++){
      uchar H = hls_img.at(row,col)[0];
      uchar L = hls_img.at(row,col)[1];
      uchar S = hls_img.at(row,col)[2];
      double LS_ratio = ((double) L) / ((double) S);
      bool skin_pixel = (S >= 50) && (LS_ratio > 0.5) && (LS_ratio < 3.0) && ((H <= 14) || (H >= 165));
      if(!skin_pixel){
        img.at(row,col)[0] = 0;
        img.at(row,col)[1] = 0;
        img.at(row,col)[2] = 0;					
      }
    }
  }		
  namedWindow( "HLS", WINDOW_AUTOSIZE );
  imshow( "HLS", img );   
  waitKey(); 
  return 0;
}

Detekcja zjawiska czerwonych oczu 

asd

#include <cstdlib>
#include <iostream>
#include <string>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(){
  Mat img = imread("./face2.jpg", 1 );
  Mat hls_img;
  cvtColor(img, hls_img, CV_BGR2HLS);
  for (int row=0; row < img.rows; row++){
    for (int col=0; col < img.cols; col++){
      uchar H = hls_img.at(row,col)[0];
      uchar L = hls_img.at(row,col)[1];
      uchar S = hls_img.at(row,col)[2];
      double LS_ratio = ((double) L) / ((double) S);
      bool red_eye_pixel = (L >= 64) && (S >= 100) && (LS_ratio > 0.5) && (LS_ratio < 1.5) && ((H <= 7) || (H >= 162));
      if(!red_eye_pixel){
        Vec3b intensity = img.at(row, col);
        uchar blue = intensity.val[0];
	    uchar green = intensity.val[1];
	    uchar red = intensity.val[2];
        img.at(row,col)[0] = 0.299*red+0.587*green + 0.114*blue;;
        img.at(row,col)[1] = 0.299*red+0.587*green + 0.114*blue;;
        img.at(row,col)[2] = 0.299*red+0.587*green + 0.114*blue;;					
      }
    }
  }		
  namedWindow( "HLS", WINDOW_AUTOSIZE );
  imshow( "HLS", img );   
  waitKey(); 
  return 0;
}