2015-04-17 180 views
0

我目前使用OpenCV函數 - cvtColor將圖像從RGB轉換爲YCrCb格式。我想用我自己的方程進行類似於在OpenCV中將RGB轉換爲YCbCr

//equations for RGB to YUV conversion 
Y' = 0.299 R + 0.587 G + 0.114 B 
U = -0.147 R - 0.289 G + 0.436 B 
V = 0.615 R - 0.515 G - 0.100 B.  

我不能理解OpenCV圖像矩陣運算。我想從圖像Mat訪問RGB像素值,以便我可以自己執行轉換。我如何從圖像中獲取R,G,B值,然後如何應用轉換?我目前的代碼如下。

int main (int argc, char *argv[]) 
{ 
    // Load in image 
    cv::Mat src = cv ::imread("C:\\openv2410\\frames\\frame_0.png",1); 

    // Create a vector for the channels and split the original image into B G R colour channels. 
    // Keep in mind that OpenCV uses BGR and not RGB images 
    vector<cv::Mat> spl; 
    split(src,spl); 

    // Create an zero pixel image for filling purposes - will become clear later 
    // Also create container images for B G R channels as colour images 
    cv::Mat empty_image = cv::Mat::zeros(src.rows, src.cols, CV_8UC1); 
    cv::Mat empty_channel = cv::Mat::zeros(src.rows, src.cols, CV_8UC1); 

    cv::Mat result_blue(src.rows, src.cols, CV_8UC3); // notice the 3 channels here! 
    cv::Mat result_green(src.rows, src.cols, CV_8UC3); // notice the 3 channels here! 
    cv::Mat result_red(src.rows, src.cols, CV_8UC3); // notice the 3 channels here! 

    // Create blue channel 
    cv::Mat in1[] = { spl[0], empty_image, empty_image }; 
    int from_to1[] = { 0,0, 1,1, 2,2 }; 
    mixChannels(in1, 3, &result_blue, 1, from_to1, 3); 

    // Create green channel 
    cv::Mat in2[] = { empty_channel, spl[1], empty_image }; 
    int from_to2[] = { 0,0, 1,1, 2,2 }; 
    mixChannels(in2, 3, &result_green, 1, from_to2, 3); 

    // Create red channel 
    cv::Mat in3[] = { empty_channel, empty_channel, spl[2]}; 
    int from_to3[] = { 0,0, 1,1, 2,2 }; 
    mixChannels(in3, 3, &result_red, 1, from_to3, 3);  

    imshow("blue channel",result_blue); 
    imshow("green channel",result_green); 
    imshow("red channel",result_red); 

    cv::waitKey(0); 
    return 0;  
}  
+0

這也許可以幫助:http://stackoverflow.com/a/7903042/3702797的 – Kaiido

+0

可能重複[OpenCV中得到席圖像像素通道值(http://stackoverflow.com/questions/7899108/opencv-get-pixel-channel-value-from-mat-image) – Kiran

+0

@Kiran,不知道它是否真的是一個愚蠢的問題,這裏的問題是*還*關於手動將它轉換爲YCbCr,不僅得到像素通道 – Kaiido

回答

1

從BGR轉換爲YCrCb的示例代碼。來源(1)。

//sample input and output 
float data[3][1] = { 98,76,88 }; 
Mat input(1, 1, CV_32FC3, data) ; 
Mat output(1, 1, CV_32FC3); 

//iterate over all pixels 
for(int i = 0; i < input.rows; i++) { 
    for(int j = 0; j < input.cols; j++) { 
     //get bgr pixel 
     Vec3f bgrPixel = input.at<Vec3f>(i, j); 

     float B = bgrPixel[0]; 
     float G = bgrPixel[1]; 
     float R = bgrPixel[2];   

     //actual conversion from BGR to YCrCb 
     float delta = 0.5f; 
     float Y = 0.299f * R + 0.587f * G + 0.114f * B; 
     float Cb = (B - Y) * 0.564f + delta; 
     float Cr = (R - Y) * 0.713f + delta; 

     //store into result image 
     Vec3f yCrCbPixel(Y, Cr, Cb); 
     output.at<Vec3f>(i, j) = yCrCbPixel; 
    } 
} 
+0

感謝您的幫助。 但是當我試圖替換虛擬數據浮點數據[3] [1] = {98,76,88}; 與 Mat input = imread(argv [1],0);它沒有顯示我添加的結果 std :: cout <<「Y Channel =」<< Y << std :: endl; 顯示Y的值,但不顯示任何內容。 – user3805574

+0

@ user3805574'imread(argv [1],0)'給你一個灰度圖像而不是RGB圖像。我假設這個問題是關於將RGB轉換爲YCrCb。 – Kiran

+0

是的問題是關於將RGB轉換爲灰度,但是,當我試圖替換Mat輸入(1,1,CV_32FC3,數據);與墊img = imread; – user3805574