我目前使用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;
}
這也許可以幫助:http://stackoverflow.com/a/7903042/3702797的 – Kaiido
可能重複[OpenCV中得到席圖像像素通道值(http://stackoverflow.com/questions/7899108/opencv-get-pixel-channel-value-from-mat-image) – Kiran
@Kiran,不知道它是否真的是一個愚蠢的問題,這裏的問題是*還*關於手動將它轉換爲YCbCr,不僅得到像素通道 – Kaiido