split r g b matrix from rgb image

asked 2015-09-22 19:34:02 -0600

dmngu9 gravatar image

updated 2015-09-23 00:07:31 -0600

berak gravatar image
 Mat image; 
  Mat grayFrame;
  Mat blue_matrix ;
  Mat diffFrame;
  image = cv_ptr->image;
  float max=0;
//obtain luminosity image and blue matrix and diffframe of blue and luminance 
  for (int row = 0; row<=479; row++)
  {
    for (int col =0; col<=639; col++)
    {
        Vec3f color_value = image.at<Vec3f>(row, col);
        int blue = color_value.val[0];
        int green = color_value.val[1];
        int red = color_value.val[2];
        grayFrame.at<int>(row,col) = round (0.21*red + 0.72*green + 0.07 * blue); 
        blue_matrix.at<int>(row,col) = blue;
        cout << blue<< "       "<<endl;
}}

Hi, I have a bgr image and want to split into r g b matrice and convert original matrix to grayscale. However , this code gives me core dump error because of these lines

grayFrame.at<int>(row,col) = round (0.21*red + 0.72*green + 0.07 * blue); 
        blue_matrix.at<int>(row,col) = blue;

Also, i notice the blue value is weird. This is the value i obtained -2147483648 -2147483648.

Anyone help me with this problem please

edit retag flag offensive close merge delete

Comments

2
  • i guess, none of your at<something>() calls is using the correct type (Vec3b instead of Vec3f, uchar instead of int).
  • if you shuffle data into new Mat's manually, you have to allocate data before ( Mat blue_matrix(image.size(), CV_8U); Mat grayFrame(image.size(), CV_8U); ).

opencv is a high level library, please avoid writing per-pixel loops like above but use existing functions properly. you could just do:

Mat gray;
cvtColor(image,gray,COLOR_BGR2GRAY);

Mat blue;
extractChannel(image,blue,0);

and be done.

berak gravatar imageberak ( 2015-09-23 00:20:46 -0600 )edit