Creating an Image from a Textfile Data using Matlab - image

With the below lines of code, I am breaking the 64x64 size colored image and storing it in a text file after normalization.
input_image_filename = './Images/Image_64x64.jpg'; % A 64x64 size colored image
input_image_3D = imread(input_image_filename); % Breaking image into pixel
value_data= double(reshape(input_image_3D,[],1));
%norm_image_3D= value_data/norm(value_data);
fid= fopen('filename_test1.txt','wt');
fprintf(fid,'%0.16f\n',value_data); % Written the image to file
fclose(fid);
filename = 'filename_test1.txt';
testing_image_filename= './Testing_64x64_output.jpg';
testingReadFile= importdata(filename);
data_in_matrix_form= double(reshape(testingReadFile,[64 64 3]));
imwrite(data_in_matrix_form,testing_image_filename);
imshow(data_in_matrix_form);
After this, suppose I want to generate the image back from the textfile data, how should I do that?
The output image looks different from the input image.
The Input image is
The output image I am getting is .

Related

Convert image to array and show again it in image

I'm using the following code to show an image from array which previously converted to array. But the image not show correctly:
I = imread('ut.jpg');
image=mat2gray(I);
imshow(image);
FID = fopen('FileName.txt', 'w');
if FID == -1, error('Cannot create file.'); end
fprintf(FID, '%g %g %g ... %g \n', image);
fclose(FID);
x = 100*rand(512,1500);
fileID = fopen('FileName.txt','w');
fprintf(fileID,'%f',x);
fclose(fileID);
imshow(x);
Bitmaps consist of two things. First: 3 matrices with colour intensities, graduated as uint, numbers from 0 to 255. Second: A header containing information regarding size, colordepth, filelength, etc.
Your program does not create correct images because it misses a header.
Regarding the matlab procedure:
By using imread on a RGB image, you are automatically creating a matrix. If you convert it to grayscale with rgb2gray you will have a single matrix of uint (without any additional layers).
If you want to safe your image after processing it simply use:
I = imread('ut.jpg');
% Convert, do smth e.g. I_new = rgb2gray(I);
filename = 'myNewImage.jpg';
imwrite(I_new,filename);
By using imwrite you automatically create a correct header.

How to read image from pixel data?

I am trying to read image of cifar10 dataset in MATLAB. The data is given in 10000x3072 format in which one row contains corresponding RGB value. I used:
img= reshape(data(1, 1:1024), [32,32]);
image(img)
to convert the image into meaningful because it is showing garbage image. How can I read the image from this .mat file? from this dataset https://www.cs.toronto.edu/~kriz/cifar-10-matlab.tar.gz
According to this page, the format of data is:
data -- a 10000x3072 numpy array of uint8s. Each row of the array stores a 32x32 colour image. The first 1024 entries contain the red channel values, the next 1024 the green, and the final 1024 the blue. The image is stored in row-major order, so that the first 32 entries of the array are the red channel values of the first row of the image.
Using your code:
img= reshape(data(1, 1:1024), [32,32]);
you should get the red channel of the first image in column-major order (i.e. transposed). To get a full RGB image with the correct orientation, you'll want to use:
img = reshape(data(1, 1:3072), [32,32,3]); % get 3-channel RGB image
img = permute(img, [2 1 3]); % exchange rows and columns

Reading & Writing Grayscale TIF files in Matlab

I'm trying to extract a (very) large number of subimages from a large grayscale TIF file and save each image as a GIF, PNG, or even another TIF using MATLAB. I'm able to display the individual images using imshow(sub(:,:,1),cmap) but when I try to write the data to an image file, the generated files are just white squares 101x101 px. Using the cmap argument in imwrite produces the same result, as does changing the image format (I've tried with PNG, TIF, GIF, and JPG with no luck). The file a.tif is 16 bit according to the property menu in Windows Explorer. Any help is appreciated. I'm really at wit's end with this.
% Import coordinates array and correct for multiplication by 10
datafile = 'data.xlsx';
coords = xlsread(datafile,1,'G2:H13057');
x = coords(:,1) ./ 10;
y = coords(:,2) ./ 10;
r = 50;
[img, cmap] = imread('a.tif'); % import the image
s = 2*r+1; % scalar of size of each submatrix in the array (sise of image)
sub = zeros(s,s,num); % create 3D matrix/array of matrices. Each submatrix corresponds to 50 px box around each point
i = 1:4;
subrgb = zeros(s,s,num);
for i=1:4
sub(:,:,i) = img((y(i)-r):(y(i)+r),(x(i)-r):(x(i)+r));
filename = 'dot_%d.png';
filename = sprintf(filename,i);
imwrite(sub(:,:,i),filename,'png');
end
Try changing the line:
sub = zeros(s,s,num);
to:
sub = zeros(s,s,num,class(img));
I assume that the problem is that sub is of type double.
Good luck

Writing a greyscale video using Videowriter/avifile

I am writing a function that generates a movie mimicking a particle in a fluid. The movie is coloured and I would like to generate a grayscaled movie for the start. Right now I am using avifile instead of videowriter. Any help on changing this code to get grayscale movie? Thanks in advance.
close all;
clear variables;
colormap('gray');
vidObj=avifile('movie.avi');
for i=1:N
[nx,ny]=coordinates(Lx,Ly,Nx,Ny,[x(i),-y(i)]);
[xf,yf]=ndgrid(nx,ny);
zf=zeros(size(xf))+z(i);
% generate a frame here
[E,H]=nfmie(an,bn,xf,yf,zf,rad,ns,nm,lambda,tf_flag,cc_flag);
Ecc=sqrt(real(E(:,:,1)).^2+real(E(:,:,2)).^2+real(E(:,:,3)).^2+imag(E(:,:,1)).^2+imag(E(:,:,2)).^2+imag(E(:,:,3)).^2);
clf
imagesc(nx/rad,ny/rad,Ecc);
writetif(Ecc,i);
if i==1
cl=caxis;
else
caxis(cl)
end
axis image;
axis off;
frame=getframe(gca);
cdata_size = size(frame.cdata);
data = uint8(zeros(ceil(cdata_size(1)/4)*4,ceil(cdata_size(2)/4)*4,3));
data(1:cdata_size(1),1:cdata_size(2),1:cdata_size(3)) = [frame.cdata];
frame.cdata = data;
vidObj = addframe(vidObj,frame);
end
vidObj = close(vidObj);
For your frame data, use rgb2gray to convert a colour frame into its grayscale counterpart. As such, change this line:
data(1:cdata_size(1),1:cdata_size(2),1:cdata_size(3)) = [frame.cdata];
To these two lines:
frameGray = rgb2gray(frame.cdata);
data(1:cdata_size(1),1:cdata_size(2),1:cdata_size(3)) = ...
cat(3,frameGray,frameGray,frameGray);
The first line of the new code will convert your colour frame into a single channel grayscale image. In colour, grayscale images have all of the same values for all of the channels, which is why for the second line, cat(3,frameGray,frameGray,frameGray); is being called. This stacks three copies of the grayscale image on top of each other as a 3D matrix and you can then write this frame to your file.
You need to do this stacking because when writing a frame to file using VideoWriter, the frame must be colour (a.k.a. a 3D matrix). As such, the only workaround you have if you want to write a grayscale frame to the file is to replicate the grayscale image into each of the red, green and blue channels to create its colour equivalent.
BTW, cdata_size(3) will always be 3, as getframe's cdata structure always returns a 3D matrix.
Good luck!

convert hex(png) file to rgb values

I converted a PNG image to hex and am wondering if it is possible to decompress the hex into this type of format for each pixel of the image:
Opacity(0-255)-Red(0-255)-Green(0-255)-Blue(0-255)
I'm using a site/program that has heavy restrictions on images you can upload(quality, size, amount, etc,) but I can create images pixel by pixel. I was hoping to decompress the hex that I converted from the original PNG file to the format above so that I can create a simple function to build it on the screen. Come to think of it, is there a way to pull the RGB and transparency from the hex PNG file without any need for reformatting?
It is very easy you have to cut your hexvalue,
and applicate an HEX to DECIMAL CONVERSION.
Ex : FFAACC00
FF = R = 255
AA = G = 170
CC = B = 204
00 = Transparency Full (FF = Transparency OFF)
If you need a program to do that you can use PNG2HEX.exe :
Download
(just drop your PNG File on it and it will create 2 files :
1- fileHex.txt with the value of each pixel in Hexa
2- FileRGB.txt with the value of each pixel in R G B
You can then modify values in these files and rebuild the png with the modification using
HEX2PNG.exe

Resources