Creating a grayscale image from ascii file

Hi all,
Could anyone please help me how to create a grayscale image (map) from an ascii file (the file contains three columns: X coord, Y coord and intensity value). So the result of grayscale image will have a georeference coord and the grayscale represents the intensity value. Thank you in advance.
Henz.
I attached the small piece of the file as well from the total 300mb. The grid size is 5 cm and I was thinking to re-gridding with 10 cm or 15 cm of grid size, so the processing time would be not too long. But I don't know how to do that? I really appreciate your help. Thank you.

6 comentarios

Walter Roberson
Walter Roberson el 16 de Sept. de 2018
Do the X and Y form a rectangular grid?
From your mention of georeference it would not surprise me if X and Y are a lat/long grid that might perhaps have equal lat and long divisions but not equal linear divisions because of curvature of the Earth. If that is the case, do you need the map to be in lat/long coordinates (so, before map projection, not something that can be directly displayed as an image) or in linear coordinates (that is, after map projection)
Hi Walter,
The X and Y coord are in utm and the area coverage is just around 500m, so I think the earth curvature can be ignored?
Walter Roberson
Walter Roberson el 16 de Sept. de 2018
Okay, but are they scattered or do they form a rectangular grid?
If they form a rectangular grid, then you can read the intensity values and reshape() them, and possibly a transpose is needed too, depending on the order they are input. The edge vectors would then be uniquetol(X) and uniquetol(Y)
Thanks Walter,
Could you please give me some codes examples or maybe using combination with ArcGIS, I am still new in matlab so need to learn much more.
Stephen23
Stephen23 el 16 de Sept. de 2018
@hendra kurnia febriawan: are the data scattered or gridded?:
It would probably help if you uploaded the data file by clicking the paperclip button.
hendra kurnia febriawan
hendra kurnia febriawan el 16 de Sept. de 2018
Editada: Walter Roberson el 16 de Sept. de 2018
Hi Stephen,
Thanks for the links. The ascii file has spatial spacing 5 cm, so it is a rectangular grid. The total file is very big (217 mb). I tried to assign the 1st row as x, 2nd row as y and 3rd row as z.
M = dlmread('file.txt',',');
x = M(:,1);
y = M(:,2);
z = M(:,3);
[X,Y,Z] = meshgrid(x,y,z);
But it showed following warning.
Error using repmat
Requested 493606x493606x493606 (896048273.5GB) array exceeds
maximum array size preference. Creation of arrays greater
than this limit may take a long time and cause MATLAB to
become unresponsive. See array size limit or preference panel
for more information.
Error in meshgrid (line 77)
xx = repmat(xx, ny, 1, nz);
Any suggestion for that?
Thanks.

Iniciar sesión para comentar.

Respuestas (1)

Walter Roberson
Walter Roberson el 16 de Sept. de 2018
M = dlmread('file.txt',',');
x = M(:,1);
y = M(:,2);
z = M(:,3);
ux = uniquetol(x);
uy = uniquetol(y);
Z = reshape(z, length(ux), length(uy));
You might need
Z = reshape(z, length(uy), length(ux)) .';

3 comentarios

Hi Walter,
Thanks for the codes. I have tried it but when running the reshape it comes with error. x = 493606x1 double y = 493606x1 double z = 493606x1 double ux = 429x1 double uy = 1253x1 double Z = reshape(z, length(uy), length(ux));
Error using reshape To RESHAPE the number of elements must not change.
And let say it works then I need to meshgrid the ux, uy and Z? Thanks.
Stephen23
Stephen23 el 16 de Sept. de 2018
Editada: Stephen23 el 16 de Sept. de 2018
@hendra kurnia febriawan: some of the values are missing. You can check the sizes yourself:
  • ux has size 429x1
  • uy has size 1253x1
Now lets calculate how many Z values that would give:
>> 429*1253
ans = 537537
Does M have 537537 rows? No. Actually you have 493606 data values for each of X, Y, and Z. There is no way to reshape a 493606 element vector into a 537537 element matrix. I suspect that you will have to resample/interpolate your data, to get the gridded data that you require. I would love to help you, but your sample file simply repeats the same X and Y values like this (I copied the data verbatim):
4.063e+05,6.474e+06,75.828
4.063e+05,6.474e+06,75.982
4.063e+05,6.474e+06,75.443
4.063e+05,6.474e+06,75.029
4.063e+05,6.474e+06,74.431
4.063e+05,6.474e+06,74.651
4.063e+05,6.474e+06,74.873
4.063e+05,6.474e+06,76.198
...
and I doubt that that represents the real data values (are they really only stored to four/five significant figures?). Please:
  • create a file suitable for uploading,
  • check the file has useful values (not just 4 sigfig),
  • edit your question,
  • upload the file by clicking the paperclip button.
Hi Stephen,
Thanks for the comments. I apologize for the previous data since I only simply extracted and took a piece of row from the original file without checking first. I have re-uploaded the data. Actually, the grid resolution is 5cm so you can notice the values are only different after the comma since it shows in decimal meter.
Thank you.

Iniciar sesión para comentar.

Categorías

Más información sobre Images en Centro de ayuda y File Exchange.

Productos

Versión

R2018a

Etiquetas

Preguntada:

el 16 de Sept. de 2018

Comentada:

el 16 de Sept. de 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by