久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

將數(shù)據(jù)從 glReadPixels() 轉換為 OpenCV::Mat

Converting data from glReadPixels() to OpenCV::Mat(將數(shù)據(jù)從 glReadPixels() 轉換為 OpenCV::Mat)
本文介紹了將數(shù)據(jù)從 glReadPixels() 轉換為 OpenCV::Mat的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我想使用 glReadPixels() 從動畫中獲取每個 OpenGL 幀,并將數(shù)據(jù)轉換為 OpenCV::Mat.我知道 glReadPixels() 從下到上,從左到右按行獲取數(shù)據(jù).另一方面,OpenCV 以不同的方式存儲數(shù)據(jù).

I want to get every OpenGL frame from an animation with glReadPixels() and convert the data to OpenCV::Mat. I know that glReadPixels() gets the data by rows from the lower one to upper one, from left to right. On the other hand, OpenCV stores the data differently.

有沒有人知道任何可以幫助我將數(shù)據(jù)從 glReadPixels 轉換為 C++ 中的 OpenCV:Mat 的庫或任何教程/示例?

Does anybody know any library or any tutorial/example that helps me to convert data from glReadPixels to a OpenCV:Mat in C++?

總結

 OpenGL frame      ----------------------->        CV::Mat

Data from left to right,                    Data from left to right,
bottom to top.                              top to bottom.

推薦答案

首先我們創(chuàng)建一個空的(或單元化的)cv::Mat 以便我們的數(shù)據(jù)可以直接讀入.這可以在啟動時完成一次,但另一方面 cv::Mat::create 當圖像已經(jīng)具有匹配的大小和類型時并不會真正花費太多.類型取決于您的需求,通常類似于 CV_8UC3 用于 24 位彩色圖像.

First we create an empty (or unititialized) cv::Mat for our data to be read into directly. This can be done once at startup, but on the other hand cv::Mat::create doesn't really cost much when the image already has matching size and type. The type depends on your needs, usually it's something like CV_8UC3 for a 24-bit color image.

cv::Mat img(height, width, CV_8UC3);

img.create(height, width, CV_8UC3);

然后您必須考慮 cv::Mat 不必連續(xù)存儲圖像行.每行末尾可能有一個小的填充值,以使行 4 字節(jié)對齊(或 8 字節(jié)?).所以你需要弄亂像素存儲模式:

Then you have to account for cv::Mat not neccessarily storing image rows contiguously. There might be a small padding value at the end of each row to make rows 4-byte aligned (or 8?). So you need to mess with the pixel storage modes:

//use fast 4-byte alignment (default anyway) if possible
glPixelStorei(GL_PACK_ALIGNMENT, (img.step & 3) ? 1 : 4);

//set length of one complete row in destination data (doesn't need to equal img.cols)
glPixelStorei(GL_PACK_ROW_LENGTH, img.step/img.elemSize());

接下來,矩陣的類型會影響glReadPixels 的格式和類型參數(shù).如果你想要彩色圖像,你必須記住 OpenCV 通常以 BGR 順序存儲顏色值,所以你需要使用 GL_BGR(A)(這是在 OpenGL 1.2 中添加的)而不是 GL_RGB(A).對于一個分量圖像,使用 GL_LUMINANCE(對各個顏色分量求和)或 GL_REDGL_GREEN、...(獲取單個分量).因此,對于我們的 CV_8UC3 圖像,將其直接讀入 cv::Mat 的最終調(diào)用將是:

Next, the type of the matrix influences the format and type parameters of glReadPixels. If you want color images you have to keep in mind that OpenCV usually stores color values in BGR order, so you need to use GL_BGR(A) (which were added with OpenGL 1.2) instead of GL_RGB(A). For one component images use either GL_LUMINANCE (which sums the individual color components) or GL_RED, GL_GREEN, ... (to get an individual component). So for our CV_8UC3 image the final call to read it directly into the cv::Mat would be:

glReadPixels(0, 0, img.cols, img.rows, GL_BGR, GL_UNSIGNED_BYTE, img.data);

最后,OpenCV 從上到下存儲圖像.因此,您可能需要在獲取它們后翻轉它們,或者首先在 OpenGL 中渲染它們(這可以通過調(diào)整投影矩陣來完成,但在這種情況下要注意三角形方向).要垂直翻轉 cv::Mat,您可以使用 cv::flip:

Finally, OpenCV stores images from top to bottom. So you may need to either flip them after getting them or render them flipped in OpenGL in the first place (this can be done by adjusting the projection matrix, but keep an eye on triangle orientation in this case). To flip a cv::Mat vertically, you can use cv::flip:

cv::flip(img, flipped, 0);

所以要記住 OpenCV:

So to keep in mind OpenCV:

  • 從上到下,從左到右存儲圖像
  • 按 BGR 順序存儲彩色圖像
  • 可能不會將圖像行存儲得緊湊

這篇關于將數(shù)據(jù)從 glReadPixels() 轉換為 OpenCV::Mat的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!

相關文檔推薦

Assertion failed (size.widthgt;0 amp;amp; size.heightgt;0)(斷言失敗(size.width0 amp;amp; size.height0))
Rotate an image in C++ without using OpenCV functions(在 C++ 中旋轉圖像而不使用 OpenCV 函數(shù))
OpenCV: process every frame(OpenCV:處理每一幀)
Why can#39;t I open avi video in openCV?(為什么我不能在 openCV 中打開 avi 視頻?)
OpenCV unable to set up SVM Parameters(OpenCV 無法設置 SVM 參數(shù))
Convert a single color with cvtColor(使用 cvtColor 轉換單一顏色)
主站蜘蛛池模板: 99久久婷婷国产综合精品电影 | 国产精品免费一区二区三区四区 | 欧美区在线观看 | 亚洲综合一区二区三区 | 日韩中文不卡 | 日日日日操 | 亚洲精品视频在线看 | 婷婷福利 | 天堂免费 | 日韩欧美精品 | 国产免费一区二区三区网站免费 | 久久久成人一区二区免费影院 | 99在线免费观看视频 | 黄一区二区三区 | 亚洲伦理自拍 | 武道仙尊动漫在线观看 | 精品国产鲁一鲁一区二区张丽 | 羞羞视频网站免费看 | 91网站在线观看视频 | 毛片韩国 | 午夜寂寞网站 | 日本三级电影在线观看视频 | 久久99精品久久久久久琪琪 | 91久久国产综合久久 | chengrenzaixian| 久草网站| av国产精品 | 中文字幕精品视频在线观看 | 中文字幕第十五页 | 国产日韩精品一区 | 看片91| 亚洲精品在线观 | 波多野吉衣久久 | 四虎精品在线 | 午夜成人免费视频 | 亚洲视频一区在线观看 | 91观看 | 国产精品亚洲综合 | 日韩久久久久久久 | 成人网在线观看 | 男女视频在线免费观看 |