将Opencv imread()转换为Mat_< float> [英] Opencv imread() to Mat_<float>
问题描述
我正在程序中读取许多图像,并使用以下代码读取每个图像.
I was reading many images in my program and used following code to read each image.
cv::Mat_<float> LoadedImage = imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
而且,它适用于某些图像,但不适用于所有图像. (图像均为JPG文件)
And, it works for some images but not for all images. (Images are all JPG files)
我使用VS 2010调试模式检查了结构,并且LoadedImage
具有以下结构.
I checked structure with VS 2010 debug mode and LoadedImage
has following structure.
结构http://imageshack.com/a/img838/451/5n17. png
以某种方式,行为零(但列是正确的).有人能向我解释原因吗? :)
Somehow rows are zero (But cols are correct). Is anyone able to explain to me why? :)
谢谢.
我正在放置当前正在使用的代码来避免此问题.但是我不确定为什么必须那样做.
I am putting code that I am currently using to avoid this issue. But I am not sure why that has to be done that way.
// Read image with imread() to Mat
cv::Mat LoadedImage_CharFormat = imread(fullfilename);
// Convert to Gray image
if(LoadedImage_CharFormat.type() != CV_8UC1)
cvtColor(LoadedImage_CharFormat, LoadedImage_CharFormat, CV_RGB2GRAY);
// Convert to Mat_<float>
cv::Mat_<float> LoadedImage;
LoadedImage_CharFormat.convertTo(LoadedImage, CV_32F);
这是您可以尝试的图像. http://imagizer.imageshack.us/a/img845/4624/y8h0. jpg
Here is image that you can try. http://imagizer.imageshack.us/a/img845/4624/y8h0.jpg
推荐答案
您无法将灰度(uchar)图像直接读取到浮动垫中.
you can't read a grayscale(uchar) image directly into a float Mat. instead,
Mat m = imread("image.jpg",0);
Mat_<float> fm;
m.convertTo(fm,CV_32F);
这篇关于将Opencv imread()转换为Mat_< float>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!