我试图使用OpenCV2.2中的imread
函数。imread不工作在Opencv
我的代码很简单。
cv::Mat host= imread("1.bmp", CV_LOAD_IMAGE_GRAYSCALE);
之后,宿主矩阵由零指针填充,即图像未加载。
如果我使用cvLoadImage
那么它一切正常。
该文件存在,我不混合发布和调试库。为什么imread
不起作用?
我试图使用OpenCV2.2中的imread
函数。imread不工作在Opencv
我的代码很简单。
cv::Mat host= imread("1.bmp", CV_LOAD_IMAGE_GRAYSCALE);
之后,宿主矩阵由零指针填充,即图像未加载。
如果我使用cvLoadImage
那么它一切正常。
该文件存在,我不混合发布和调试库。为什么imread
不起作用?
如果您认为这是一个OpenCV错误,那么请发布您的图像和指令以复制到OpenCV bugtracker。
我可以证实,在OpenCV 2.2中存在一些imread问题。但是,这些问题只发生在Windows 32位系统上。在Linux和Mac上它工作。我不知道为什么它不起作用,但我们有一个小的解决方法。
我们用下面的宏修复了这个问题,也许你可以试试这个,然后使用“ourImread”。
#ifdef WIN32
#define ourImread(filename, isColor) cvLoadImage(filename.c_str(), isColor)
#else
#define ourImread(filename, isColor) imread(filename, isColor)
#endif
实际上,这个错误在OpenCV 2.4.2 win32上仍然存在。 – vinjn
我也遇到了OpenCV 2.4.7,win32在Visual Studio 2012中运行触发了这个bug,在控制台上运行在release模式下有时会起作用 – franckysnow
混合发布和调试模式是造成这个问题的原因,请参阅http://stackoverflow.com/questions/9125817/ opencv-imreadfilename -in-debug-mode-when-release-libraries – Aerospace
看到相关的问题here
请确保您的路径是正确的,
根据OpenCV的的API,我想尝试这一呼吁:
arrayMat[i]=imread("1.jpg" , 1);
为imread参数:
Mat imread(const string& filename, int flags=1)
Loads an image from a file.
Parameters:
filename – Name of file to be loaded.
flags – Specifies color type of the loaded image:
>0 the loaded image is forced to be a 3-channel color image
=0 the loaded image is forced to be grayscale
<0 the loaded image will be loaded as-is (note that in the current implementation the alpha channel, if any, is stripped from the output image, e.g. 4-channel RGBA image will be loaded as RGB if ).
好运 小号
我知道你想用 “imread” & “CV_LOAD_IMAGE_GRAYSCALE” 并自动转换。 但对于负载一个图片这另一种方式,并转换为灰度:
define CV_NO_BACKWARD_COMPATIBILITY
#include <cv.h>
#include <highgui.h>
#include <math.h>
int main(){
/* load the image */
IplImage* img = cvLoadImage("yourPicture.bmp"); //jpg - bmp
/* retrieve properties */
int width = img->width;
int height = img->height;
int nchannels = img->nChannels;
int step = img->widthStep;
IplImage* img2 = cvCreateImage(cvSize(img->height, img->width),IPL_DEPTH_8U,1);
/* setup the pointer to access image data */
uchar *data = (uchar*)img->imageData;
uchar *data2= (uchar*)img2->imageData;
/* convert to grayscale manually */
int i, j, r, g, b, byte;
for(i = 0 ; i < height ; i++) {
for(j = 0 ; j < width ; j++) {
r = data[i*step + j*nchannels + 0];
g = data[i*step + j*nchannels + 1];
b = data[i*step + j*nchannels + 2];
byte = (r + g + b)/3;
int v0=0, v1=0, v2=0;
data2[i*(img2->widthStep)+j*(img2->nChannels)+0] = byte;
data2[i*(img2->widthStep)+j*(img2->nChannels)+1] = byte;
data2[i*(img2->widthStep)+j*(img2->nChannels)+2] = byte;
}
}
cvNamedWindow("ImagenColor", 1);
cvShowImage("ImagenColor", img);
cvNamedWindow("Gray", 1);
cvShowImage("Gray", img2);
}
我有同样的问题。我解决了它。关键是文件名是否有jpg。 如果文件名是p1
,那么应该使用类似imread("p1.jpg")
的东西。但我们经常将文件名设置为p1.jpg
,这里我们应该使用类似imread("p1.jpg.jpg")
的东西。
我有同样的问题
cv::Mat image= cv::imread("immagine12.jpg"); // Read the file
if(! image.data) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
cv::waitKey(5000);
return -1;
}
cv::namedWindow("Display window", CV_WINDOW_AUTOSIZE);// Create a window for display.
imshow("Display window", image);
//non so perchè ma senza il waitKey e lasciando solo il system pause non carica l'immagine...
cv::waitKey(5000);
system("pause");
,但我固定它,当我插入cv::waitKey(5000);
我不知道为什么,但与system pause
不能加载图像,并继续暂停在它加载图像后!
混合发布和调试模式是问题的原因。请参阅http://stackoverflow.com/questions/9125817/opencv-imreadfilename-fails -in-debug-mode-when-using-release-libraries – Aerospace
我也有同样的问题,imread没有工作,cvLoadImage没有工作。
我决定从头开始创建一个新的Visual Studio,现在它可以工作。
在win32下OpenCV 2.4.3中的imread没有一般问题。
我在2.4.6面临同样的问题。原因是在选择库时,我选择了调试版本和发行版本。当我选择只库的调试版本一切正常
转载与opencv 2.4.8。
如果您在Debug中运行,请检查您是否也在使用调试库,它解决了我们的问题。 :OpenCV imread(filename) fails in debug mode when using release libraries。
这也发生在我身上,我的简单的解决办法是使用C API,然后转换为Mat
:
IplImage* img = cvLoadImage("c://frame_201.bmp");
Mat mat = Mat(img);
这工作适用于我! –
我有同样的问题
cvLoadImage
是旧的空调风格
imread
是C++风格,但是当您使用调试库时工作正常。但速度慢,使用发布库时它不会工作,因为opencv C++接口有许多错误。所以你必须使用调试库,然后imread工作正常
这不是一个答案 – Lrrr
反过来也是如此:如果你正在构建发布,你有调试库,然后imread()安静地失败(在imread()之后errno为0,但图像对象未被填充)。
另一种可能性:
如果你在OS X和静态链接的OpenCV一定要使用它捆绑了OpenCV的,不系统的一个的libjpeg。
我和OpenCV 3.0有类似的问题,只是cvLoadImage
工作不正常。所以,这可能不会真正回答你的问题,但也许它会帮助其他人。
我知道这是晚了,但有人可能会发现这有帮助。我在使用OpenCV-3.0使用imread
时遇到同样的问题。我尝试了所有的解决方案,但实际上我并没有添加库opencv2/imgcodecs.hpp
。虽然imshow
没有它的工作,但添加后,我能够读取图像。
我只在读取32位tiff图像时,在Linux上有类似的问题。
Mat mat= imread(filename, CV_LOAD_IMAGE_ANYDEPTH);
问题是因为OpenCV由于某种原因没有使用Tiff支持构建。
@asandwhich yes there is“为什么imread不起作用?”。这不是一个问题吗?可能不是寻求帮助的最佳方式,但仍然是一个问题。人们不仅仅是讽刺而且真的试图帮助人们!我知道我们很多人都没有正确描述和询问,但如果你不打算帮忙,请不要打扰他们讽刺。 – BRabbit27
对原始问题做出回应表示抱歉。 “为什么imread不起作用?“这个问题最初并不是问题,我指出这个问题,但是,嘿,我想我需要停止它。 –