How to Convert NV12 to BGR(AVFrame to cv::Mat)
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:
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
add a comment |
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:
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
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
add a comment |
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:
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
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:
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
opencv ffmpeg mat yuv
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
add a comment |
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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