Ask Your Question

[Rotate image 90 Degrees] Strange color after using cvTranspose

asked 2013-07-12 00:02:19 -0500

zhaozhixin gravatar image


I'm testing the Camera record by using FFmpegFrameRecorder, but the video always in landscape. When I use cvTranspose to rotate image to portrait, the result image have strange color.

Could anybody tell my how to correct this?

Many thanks,

  • Init recorder code:

    if (yuvIplimage == null) {
        yuvIplimage = IplImage.create(imageWidth, imageHeight, IPL_DEPTH_8U, 2);
        yuvIplimage2 = IplImage.create(imageHeight, imageWidth, IPL_DEPTH_8U, 2);
    Log.i(LOG_TAG, "create yuvIplimage");
    } Log.i(LOG_TAG, "ffmpeg_url: " + ffmpeg_link); recorder = new FFmpegFrameRecorder(ffmpeg_link, imageWidth, imageHeight, 1); // recorder.setSampleRate(sampleAudioRateInHz); recorder.setAudioCodec(avcodec.AV_CODEC_ID_AAC); recorder.setAudioChannels(1); recorder.setAudioBitrate(audioBitrate); recorder.setVideoCodec(avcodec.AV_CODEC_ID_FLV1); // Set in the surface changed method recorder.setFrameRate(frameRate); recorder.setPixelFormat(avutil.AV_PIX_FMT_YUV420P); recorder.setFormat("flv");
  • OnPreviewFrame function:

public void onPreviewFrame(byte[] data, Camera camera) { /* get video data */ if (yuvIplimage != null && recording) {


            opencv_core.cvTranspose(yuvIplimage, yuvIplimage2);
            opencv_core.cvFlip(yuvIplimage2, null, 1);

            Log.v(LOG_TAG,"Writing Frame");
            try {
                recorder.setTimestamp(1000 * (System.currentTimeMillis() - startTime));

            } catch (FFmpegFrameRecorder.Exception e) {
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2013-07-12 16:00:19 -0500

Kirill Kornyakov gravatar image

updated 2013-07-12 16:01:41 -0500

It would help if you could attach your images with strange colors... It is also interesting which YUV format are you talking about.

My guess is that you should process Y, U and V planes independently. OpenCV doesn't know about YUV format, so cvTranspose may corrupt image. For example, if your format is YUV420p - planar format, where all planes are separated and U and V are twice smaller than Y (in each direction), then you should call cvTranspose three times. If you're working with YUV420sp format, U and V are interleaved, and you should call cvTranspose two times, if I'm not mistaken.

So, OpenCV functions usually work properly with 8UC1 (grayscale), 8UC3 (RGB or BGR, etc) formats. But YUV is not natively supported, so you should call OpenCV functions on individual planes sometimes.

edit flag offensive delete link more


@Kirill: can you elaborate on how to "call OpenCV functions on individual planes"? The problem here is that the default preview format from an Android camera is in YUV420sp format (NV21), and as the OP noted, calling cvTranspose() on that IplImage causes strange green/purple color corruption (for the reasons you point out). But there is no clear example anywhere on how to achieve this with a YUV pixel format and OpenCV. Even attempting to convert to RGB with cvCvtColor() fails in all attempts I've made. cvFlip() works correctly on the YUV image, but not cvTranspose(). You also mention calling cvTranspose "two times" and "three times" -- what does that mean? Each call to cvTranspose simply corrupts the image even further.

joeh gravatar imagejoeh ( 2013-11-06 14:14:32 -0500 )edit

Question Tools


Asked: 2013-07-12 00:02:19 -0500

Seen: 2,556 times

Last updated: Jul 12 '13