Ask Your Question

Read grayscale image into array

asked 2015-01-22 16:30:57 -0600

titan1432 gravatar image

updated 2015-01-22 16:35:02 -0600

Hi all, I am absolutely new to openCV, and I need to read tif grayscale image into array of floats in order process it with my GPU kernel. Here is the code:

#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main()
Mat image,image1;
string a = "C:\\programs\\misha\\pic-1.tif";
image = imread(a, CV_32F); // Read the file
float *b;
b =;

return 0;

The error is: cannot convert from *uchar to *float. I tried to to do b = (float*), but there was a trash in array b after that.

I tried also to convert image to CV_32F. As I understand, must return me pointer to the data. I use 0-based row-major order in my GPU code. The speed of reading is also very important for me, so I need the fastest way to do it.


edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2015-01-22 16:38:38 -0600

berak gravatar image

updated 2015-01-22 17:16:44 -0600

1:) you got the imread flags wrong

2.) no, it can't read float images from a .tif (.exr would be another thing) you will have to do it in 2 steps:

// 1. Read the file, keep it 16 bits, if it was so originally:
image = imread(a, CV_LOAD_IMAGE_ANYDEPTH); 
// 2. convert the img to float:
Mat img_float;
image.convertTo(img_float, CV_32F);
// .. use img_float ..
edit flag offensive delete link more


That doesn't work: 1>------ Build started: Project: test_openCV, Configuration: Debug x64 ------ 1> Source.cpp 1>Source.cpp(14): error C2065: 'CV_LOAD_IMAGE_ANYDEPTH' : undeclared identifier 1>Source.cpp(19): error C2440: '=' : cannot convert from 'uchar *' to 'float *' 1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== So, for some reason CV_LOAD_IMAGE_ANYDEPTH doesn't work.

titan1432 gravatar imagetitan1432 ( 2015-01-22 16:51:13 -0600 )edit

if you're using opencv3.0 (sorry, lost trace), this would be: cv::IMREAD_ANYDEPTH

berak gravatar imageberak ( 2015-01-22 16:55:26 -0600 )edit

Thanks, but still float *b; b =; doesn't work: cannot convert from uchar* to float*

titan1432 gravatar imagetitan1432 ( 2015-01-22 17:01:13 -0600 )edit

opencv's Mat's keep anything in a plain, flat uchar array, so you will have to use:

float *b = img_float.ptr<float>(0); // ptr to 1st element in row 0

(aww, if you're bad with pointers and the stuff they're pointing to, might delay the gpu coding a bit, and catch up on basic c/c++ ?)

berak gravatar imageberak ( 2015-01-22 17:03:33 -0600 )edit

Thanks, it works! Last question - can I somehow avoid conversion (in order to improve performance)?

titan1432 gravatar imagetitan1432 ( 2015-01-22 17:13:47 -0600 )edit

you could try to save/read your img in a format, that natively supports float/HDR images (like .exr, but that might not even be supported in the 'plain-vanilla' opencv version you downnloaded)

again, i doubt, that you'll get any speedup from that.

berak gravatar imageberak ( 2015-01-22 17:20:29 -0600 )edit

Question Tools

1 follower


Asked: 2015-01-22 16:30:57 -0600

Seen: 4,776 times

Last updated: Jan 22 '15