我是OGRE图书馆的新手。我在OGRE中有一个人体模型,在'orginalImage'变量中得到了模型的投影。我想使用openCV执行一些图像处理。所以我正在尝试实现OGRE :: Image到cv :: Mat的转换。
Ogre::Image orginalImage = get2dProjection();
//This is an attempt to convert the image
cv::Mat destinationImage(orginalImage.getHeight(), orginalImage.getWidth(), CV_8UC3, orginalImage.getData());
imwrite("out.png", destinationImage);
我收到以下错误:
realloc(): invalid pointer: 0x00007f9e2ca13840 ***
同样,我尝试了第二次尝试
cv::Mat cvtImgOGRE2MAT(Ogre::Image imgIn) {
//Result image intialisation:
int imgHeight = imgIn.getHeight();
int imgWidth = imgIn.getWidth();
cv::Mat imgOut(imgHeight, imgWidth, CV_32FC1);
Ogre::ColourValue color;
float gray;
cout << "Converting " << endl;
for(int r = 0; r < imgHeight; r++){
for(int c = 0; c < imgWidth; c++){
color = imgIn.getColourAt(r,c,0);
gray = 0.2126 * color.r + 0.7152 * color.g + 0.0722 * color.b;
imgOut.at<float>(r,c) = gray;
}
}
return imgOut;
}
当我执行以下操作之一时,我收到相同的错误:
imshow("asdfasd", imgOut);
imwrite("asdfasd.png", imgOut);
不幸的是,我没有使用OGRE的经验,所以我只能谈谈OpenCV以及我在Ogre文档和发布者的评论中看到的内容。
首先要提到的是“ Ogre图像” PixelFormat is PF_BYTE_RGBA
(根据评论)(根据OGRE文档)是4字节像素格式,因此cv :: Mat类型应该是CV_8UC4
图像数据应该由指针给出的情况。另外,openCV最好地支持BGR图像,因此最好保存/显示颜色转换。
请试试:
Ogre::Image orginalImage = get2dProjection();
//This is an attempt to convert the image
cv::Mat destinationImage(orginalImage.getHeight(), orginalImage.getWidth(), CV_8UC4, orginalImage.getData());
cv::Mat resultBGR;
cv::cvtColor(destinationImage, resultBGR, CV_RGBA2BGR);
imwrite("out.png", resultBGR);
在您的第二个示例中,我想知道那里出了什么问题,直到我看到color = imgIn.getColourAt(r,c,0);
这可能是错的,因为大多数图像API都在使用,.getPixel(x,y)
因此我确认对于OGRE也是一样。请尝试以下方法:
cv::Mat cvtImgOGRE2MAT(Ogre::Image imgIn)
{
//Result image intialisation:
int imgHeight = imgIn.getHeight();
int imgWidth = imgIn.getWidth();
cv::Mat imgOut(imgHeight, imgWidth, CV_32FC1);
Ogre::ColourValue color;
float gray;
cout << "Converting " << endl;
for(int r = 0; r < imgHeight; r++)
{
for(int c = 0; c < imgWidth; c++)
{
// next line was changed
color = imgIn.getColourAt(c,r,0);
gray = 0.2126 * color.r + 0.7152 * color.g + 0.0722 * color.b;
// this access is right
imgOut.at<float>(r,c) = gray;
}
}
return imgOut;
// depending of what you want to do with the image, "float" Mat type assumes what image intensity values are within 0..1 (displaying) or 0..255 (imwrite)
}
如果仍然遇到重新分配错误,是否可以尝试找到发生该错误的确切代码行?
我尚未考虑的一件事是OGRE图像的实际内存布局。他们可能使用某种对齐的内存,其中每个像素行对齐后的内存大小为4或16或sth的倍数。(这可能是更有效的,例如,使用SSE指令或某事)。如果是这样的话,则不能使用第一种方法,但你必须将其更改为cv::Mat destinationImage(orginalImage.getHeight(), orginalImage.getWidth(), CV_8UC4, orginalImage.getData(), STEPSIZE);
这里STEPSIZE
是一个字节的每个像素行的数量!但是第二个版本应该可以工作了!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句