Extracting Pixel Data from Images in Matlab
- Elisabeth Cawley
- Mar 19, 2019
- 1 min read
This week we ran through some more Matlab examples and also code from the Matlab forums to learn how to export the value of a jpg image to a .txt file. Below is code which both extracts the pixel values in rgb triads from a jpg image and places it in a .txt file, and then re-imports the data.[1]
%https://www.mathworks.com/matlabcentral/answers/52567-how-to-export-the-pixel-value-of-an-jpg-image-to-a-txt-file img = imread('ShorelineDJI_0040.JPG'); fid = fopen('deleteMeShoreline.txt', 'w'); if fid == -1, error('Cannot open file'); end fprintf(fid, '%d %d %d '); % Assuming it is an RGB image fprintf(fid, '%g ', img(:)); fclose(fid); fid = fopen('deleteMeShoreline.txt', 'r'); if fid == -1, error('Cannot open file'); end ImgSize = fscanf(fid, '%d %d %d ', 3); ImgData = fscanf(fid, '%g ', Inf); Img = reshape(ImgData, ImgSize); fclose(fid);
[1]https://www.mathworks.com/matlabcentral/answers/52567-how-to-export-the-pixel-value-of-an-jpg-image-to-a-txt-file
Recent Posts
See AllThis past week I explored the statistics and machine learning toolbox in Matlab. I applied basic statistics concepts I am learning at...
Last week I helped Fred with the prototype 6 Degree of Freedom arm. We Figured out how to connect the transmitter and power to the arm....
Comments