How to Convert NV12 to BGR(AVFrame to cv::Mat)












1















I decoded videoframe by using FFMPEG Library from IP Camera.
This videoframe format is AV_PIX_FMT_NV12. AV_PIX_FMT_NV12 is defined on FFMPEG Library.
I want to convert NV12 to BGR (AVFrame to cv::Mat), so I try to do this
but I got wrong image:



enter image description here



Please help me. I want to convert properly. Can anyone guess this problem?
Please refer to following code:



void CALLBACK MyCallbackFunc(AVFrame *avFrame )
{
if (avFrame != NULL)
{
cv::Mat imageRGB;
cv::Mat picI420 = cv::Mat(avFrame->height * 3 / 2, avFrame->width, CV_8UC1, avFrame->data[0]);
cv::cvtColor(picI420, imageRGB, CV_YUV2BGR_NV12);
imshow("imageRGB", imageRGB);

cvWaitKey(1);
}
}


Sorry for my bad english. If you want more information please comment below.










share|improve this question




















  • 1





    Adding a sample frame of data with its dimensions might help you to get an answer.

    – Mark Setchell
    Jan 1 at 12:19











  • Maybe it is not the cause of your issue but I see two let say 'mistake'. 1) You send and mono channel image to cvtColor. 2) If I well understand avFrame->data store the data columnwise. I deduct that from the fact that you divide the height by the number of channels. It is to know that Mat container expect data ordered rowwise and interleaved. e.g. let suppose a matrix of 3 rows and 4 columns BGR using unsigned char data a Mat object store the informations as: [row0_column0_blue, row0_column0_green, row0_column0_red, row0_column1_blue,..., row3_column4_red]. Hope it helps.

    – John_Sharp1318
    Jan 1 at 20:24
















1















I decoded videoframe by using FFMPEG Library from IP Camera.
This videoframe format is AV_PIX_FMT_NV12. AV_PIX_FMT_NV12 is defined on FFMPEG Library.
I want to convert NV12 to BGR (AVFrame to cv::Mat), so I try to do this
but I got wrong image:



enter image description here



Please help me. I want to convert properly. Can anyone guess this problem?
Please refer to following code:



void CALLBACK MyCallbackFunc(AVFrame *avFrame )
{
if (avFrame != NULL)
{
cv::Mat imageRGB;
cv::Mat picI420 = cv::Mat(avFrame->height * 3 / 2, avFrame->width, CV_8UC1, avFrame->data[0]);
cv::cvtColor(picI420, imageRGB, CV_YUV2BGR_NV12);
imshow("imageRGB", imageRGB);

cvWaitKey(1);
}
}


Sorry for my bad english. If you want more information please comment below.










share|improve this question




















  • 1





    Adding a sample frame of data with its dimensions might help you to get an answer.

    – Mark Setchell
    Jan 1 at 12:19











  • Maybe it is not the cause of your issue but I see two let say 'mistake'. 1) You send and mono channel image to cvtColor. 2) If I well understand avFrame->data store the data columnwise. I deduct that from the fact that you divide the height by the number of channels. It is to know that Mat container expect data ordered rowwise and interleaved. e.g. let suppose a matrix of 3 rows and 4 columns BGR using unsigned char data a Mat object store the informations as: [row0_column0_blue, row0_column0_green, row0_column0_red, row0_column1_blue,..., row3_column4_red]. Hope it helps.

    – John_Sharp1318
    Jan 1 at 20:24














1












1








1








I decoded videoframe by using FFMPEG Library from IP Camera.
This videoframe format is AV_PIX_FMT_NV12. AV_PIX_FMT_NV12 is defined on FFMPEG Library.
I want to convert NV12 to BGR (AVFrame to cv::Mat), so I try to do this
but I got wrong image:



enter image description here



Please help me. I want to convert properly. Can anyone guess this problem?
Please refer to following code:



void CALLBACK MyCallbackFunc(AVFrame *avFrame )
{
if (avFrame != NULL)
{
cv::Mat imageRGB;
cv::Mat picI420 = cv::Mat(avFrame->height * 3 / 2, avFrame->width, CV_8UC1, avFrame->data[0]);
cv::cvtColor(picI420, imageRGB, CV_YUV2BGR_NV12);
imshow("imageRGB", imageRGB);

cvWaitKey(1);
}
}


Sorry for my bad english. If you want more information please comment below.










share|improve this question
















I decoded videoframe by using FFMPEG Library from IP Camera.
This videoframe format is AV_PIX_FMT_NV12. AV_PIX_FMT_NV12 is defined on FFMPEG Library.
I want to convert NV12 to BGR (AVFrame to cv::Mat), so I try to do this
but I got wrong image:



enter image description here



Please help me. I want to convert properly. Can anyone guess this problem?
Please refer to following code:



void CALLBACK MyCallbackFunc(AVFrame *avFrame )
{
if (avFrame != NULL)
{
cv::Mat imageRGB;
cv::Mat picI420 = cv::Mat(avFrame->height * 3 / 2, avFrame->width, CV_8UC1, avFrame->data[0]);
cv::cvtColor(picI420, imageRGB, CV_YUV2BGR_NV12);
imshow("imageRGB", imageRGB);

cvWaitKey(1);
}
}


Sorry for my bad english. If you want more information please comment below.







opencv ffmpeg mat yuv






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 10:49









Brian Tompsett - 汤莱恩

4,2321338102




4,2321338102










asked Jan 1 at 10:30









Steven.YeunSteven.Yeun

62




62








  • 1





    Adding a sample frame of data with its dimensions might help you to get an answer.

    – Mark Setchell
    Jan 1 at 12:19











  • Maybe it is not the cause of your issue but I see two let say 'mistake'. 1) You send and mono channel image to cvtColor. 2) If I well understand avFrame->data store the data columnwise. I deduct that from the fact that you divide the height by the number of channels. It is to know that Mat container expect data ordered rowwise and interleaved. e.g. let suppose a matrix of 3 rows and 4 columns BGR using unsigned char data a Mat object store the informations as: [row0_column0_blue, row0_column0_green, row0_column0_red, row0_column1_blue,..., row3_column4_red]. Hope it helps.

    – John_Sharp1318
    Jan 1 at 20:24














  • 1





    Adding a sample frame of data with its dimensions might help you to get an answer.

    – Mark Setchell
    Jan 1 at 12:19











  • Maybe it is not the cause of your issue but I see two let say 'mistake'. 1) You send and mono channel image to cvtColor. 2) If I well understand avFrame->data store the data columnwise. I deduct that from the fact that you divide the height by the number of channels. It is to know that Mat container expect data ordered rowwise and interleaved. e.g. let suppose a matrix of 3 rows and 4 columns BGR using unsigned char data a Mat object store the informations as: [row0_column0_blue, row0_column0_green, row0_column0_red, row0_column1_blue,..., row3_column4_red]. Hope it helps.

    – John_Sharp1318
    Jan 1 at 20:24








1




1





Adding a sample frame of data with its dimensions might help you to get an answer.

– Mark Setchell
Jan 1 at 12:19





Adding a sample frame of data with its dimensions might help you to get an answer.

– Mark Setchell
Jan 1 at 12:19













Maybe it is not the cause of your issue but I see two let say 'mistake'. 1) You send and mono channel image to cvtColor. 2) If I well understand avFrame->data store the data columnwise. I deduct that from the fact that you divide the height by the number of channels. It is to know that Mat container expect data ordered rowwise and interleaved. e.g. let suppose a matrix of 3 rows and 4 columns BGR using unsigned char data a Mat object store the informations as: [row0_column0_blue, row0_column0_green, row0_column0_red, row0_column1_blue,..., row3_column4_red]. Hope it helps.

– John_Sharp1318
Jan 1 at 20:24





Maybe it is not the cause of your issue but I see two let say 'mistake'. 1) You send and mono channel image to cvtColor. 2) If I well understand avFrame->data store the data columnwise. I deduct that from the fact that you divide the height by the number of channels. It is to know that Mat container expect data ordered rowwise and interleaved. e.g. let suppose a matrix of 3 rows and 4 columns BGR using unsigned char data a Mat object store the informations as: [row0_column0_blue, row0_column0_green, row0_column0_red, row0_column1_blue,..., row3_column4_red]. Hope it helps.

– John_Sharp1318
Jan 1 at 20:24












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994735%2fhow-to-convert-nv12-to-bgravframe-to-cvmat%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994735%2fhow-to-convert-nv12-to-bgravframe-to-cvmat%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith