web-dev-qa-db-ja.com

RGBからYUVへの変換とY、U、Vチャネルへのアクセス

私はしばらくの間、この変換を探していました。 RGB画像をYUV画像に変換し、LinuxでPythonを使用してY、U、Vチャネルにアクセスする方法は何ですか?(opencv、skimageなどを使用して...)

更新:opencvを使用しました

img_yuv = cv2.cvtColor(image, cv2.COLOR_BGR2YUV)
y, u, v = cv2.split(img_yuv)

cv2.imshow('y', y)
cv2.imshow('u', u)
cv2.imshow('v', v)
cv2.waitKey(0)

この結果が得られましたが、すべて灰色のようです。 ウィキペディアページ のように表される結果を取得できませんでした

私は何か間違ったことをしていますか?

enter image description here

4
enterbutton

説明されているようにLUT値を使用することは、ウィキペディアの記事の画像が作成された方法とまったく同じかもしれませんが、説明はそれが任意であり、おそらく単純であるために使用されることを意味します。それは恣意的ではありません。結果は基本的にRGB <-> YUV変換の仕組みと一致します。 OpenCVを使用している場合、メソッドBGR2YUVおよびYUV2BGRは、同じWikipediaYUVの記事にある変換式を使用して結果を返します。 (Javaを使用して生成された私の画像は、それ以外は同じです。)

// most of the Java program which should work in other languages with OpenCV:
// everything duplicated to do both the U and V at the same time
Mat src = new Mat();
Mat dstA = new Mat();
Mat dstB = new Mat();
src = Imgcodecs.imread("shed.jpg", Imgcodecs.IMREAD_COLOR);

List<Mat> channelsYUVa = new ArrayList<Mat>();
List<Mat> channelsYUVb = new ArrayList<Mat>();

Imgproc.cvtColor(src, dstA, Imgproc.COLOR_BGR2YUV); // convert bgr image to yuv
Imgproc.cvtColor(src, dstB, Imgproc.COLOR_BGR2YUV);

Core.split(dstA, channelsYUVa); // isolate the channels y u v
Core.split(dstB, channelsYUVb);

// zero the 2 channels we do not want to see isolating the 1 channel we want to see
channelsYUVa.set(0, Mat.zeros(channelsYUVa.get(0).rows(),channelsYUVa.get(0).cols(),channelsYUVa.get(0).type()));
channelsYUVa.set(1, Mat.zeros(channelsYUVa.get(0).rows(),channelsYUVa.get(0).cols(),channelsYUVa.get(0).type()));

channelsYUVb.set(0, Mat.zeros(channelsYUVb.get(0).rows(),channelsYUVb.get(0).cols(),channelsYUVb.get(0).type()));
channelsYUVb.set(2, Mat.zeros(channelsYUVb.get(0).rows(),channelsYUVb.get(0).cols(),channelsYUVb.get(0).type()));

Core.merge(channelsYUVa, dstA); // combine channels (two of which are zero)
Core.merge(channelsYUVb, dstB);

Imgproc.cvtColor(dstA, dstA, Imgproc.COLOR_YUV2BGR); // convert to bgr so it can be displayed
Imgproc.cvtColor(dstB, dstB, Imgproc.COLOR_YUV2BGR);

HighGui.imshow("V channel", dstA); // display the image
HighGui.imshow("U channel", dstB);

HighGui.waitKey(0);
0
Tommy131313