// Ps_algorithm.h
# Ifndef ps_algorithm_h_included
# Define ps_algorithm_h_included
# Include <iostream>
# Include <string>
# Include "cv. H"
# Include "highgui. H"
# Include "cxmat. HPP"
# Include "cxcore. HPP"
Using namespace STD;
Using namespace CV;
# Endif // ps_algorithm_h_marshded
/*
The program will transfor the color
Image to the gray image.
The image must be color image.
*/
# Include "ps_algorithm.h"
Int main ()
{
String image_name ("2.jpg ");
Mat image = imread (image_name.c_str ());
Mat image_test (image. Size (), cv_32fc3 );
Image. convertize (image_test, cv_32fc3 );
Mat gray_img (image_test.size (), cv_32fc1 );
Mat R, G, B;
Gray_img.copyto (R );
G = R;
B = R;
Mat BGR [] = {B, G, r };
Split (image_test, BGR );
B = BGR [0];
G = BGR [1];
R = BGR [2];
Mat I1, I2, I3;
I1 = R;
I2 = R;
I3 = R;
// I = 0.299 * r + 0.587 * g + 0.144 * B solution 1
Gray_img = (0.299 * r + 0.587 * g + 0.144 * B );
I1 = gray_img/255;
Imshow ("I1", i1 );
// I = (R + G + B)/3 solution 2
Gray_img = (0.333 * r + 0.333 * g + 0.333 * B );
I2 = gray_img/255;
For (INT I = 100; I <105; I ++)
For (Int J = 100; j <105; j ++)
Cout <i2.at <float> (I, j) <Endl;
Imshow ("I2", I2 );
// I = max (R, G, B) solution 3
Float * P1, * P2, * P3, * P;
P1 = R. PTR <float> (0 );
P2 = G. PTR <float> (0 );
P3 = B. PTR <float> (0 );
P = i3.ptr <float> (0 );
Int Nums;
Nums = gray_img.rows * gray_img.cols;
For (INT I = 0; I <Nums; I ++)
P [I] = max (P1 [I], max (P2 [I], P3 [I])/255;
Imshow ("I3", I3 );
Cout <"All is well." <Endl;
Waitkey ();
}