Extracting Pixel Data from Images in Matlab
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
Comments