Hello Everyone!
I am in my master degree starting in image processing. I have a .dat 4-bytes 1024x1024 binary file, I need to read this aiming multiply it after for a image 1024x1024. I decided to convert the .dat file into a matrix 1024x1024 of floating points before the multiplication. I am getting some troubles. Someone could help me with this?
below my current code:
#include <iostream>
#include <fstream>
using namespace std;
Second why are you trying to read the file as some kind of unsigned character instead of a float?
I really doubt that file.read(reinterpret_cast<char*>(buffer), sizeof(buffer)) is really doing anything close as to what you seem to expect. Perhaps if "buffer" were a single float you may get closer. But this is only a guess since I have no idea about how your input file was written, and how that file was formatted.
When posting code, remember to put it in "code tags" to preserve indentation, like this:
[code]
your code goes here
[/code]
As long as the 4-byte values in the binary file actually represent float data and they have the same endianess (byte order) as your system and they are stored in row-major order, then something like the following.
Note that you can read the bytes directly into a float variable.
I see I forgot to change the f! I had used a separate f variable originally.
Instead of sizeof height[y][x] it may be better to say sizeof height[0][0].
Note that you don't actually need the parentheses if you are using a variable instead of a type.
To fix your current error, put this up above (outside) of main, like you had it before:
I just came back to edit my answer to suggest what salem has just said.
I would go with that.
So leave it in main, but put the keyword static in front of it.
But it's still better to say = {{0}} since the second {0} does nothing.
You should move height back inside main. Either you use the word static and have it inside main, or you don't use the word static and leave it outside (although you can also use the word static outside main, but that just makes it inaccessible from another code file which is not needed in your case). Either way it is kept off of the stack. I suggest moving it back inside main with the static keyword.