how to create an image in matlab (if possible) from matrix? [duplicate] - image

This question already has an answer here:
How to create an array according to row and column number [duplicate]
(1 answer)
Closed 5 years ago.
The problem is the following: I have a .txt file containing 3 columns of numbers. The first 2 columns are the coordinate x,y of the points. The third columnn (z vector) is made of numbers that express the luminosity of each point. (The .txt files have been generated by a software that is used to study the pictures of a combustion process). Each vector (x,y,z) is made of 316920 elements (all integer numbers). Now: is there a way to create from these 3 vectors an image in matlab relating the luminosity value to the coordiantes of the point?
Thanks for your time!

consider a file image.txt contains y x and intensity values separated line. like this.
1 1 0
1 2 12
1 3 10
....
....
255 255 0
open the text file using fopen function
fid = fopen(image.txt,'r');
im=[];
and read a string-line of characters by fgetl function, convert string-line into vector using sscanf and put intensity value into y and x coordinates of a image matrix, im.
tline=fgetl(fid) ;
rd=sscanf(tline,'%d');
im(rd(1),rd(2))=rd(3);
The same process is iterated up-to end of file.
at last close file-handle fid

I am going to assume that the three columns in your text file are comma separated( The code will need to be a bit different if they are not comma separated) . Since you said all numbers are integers, I am going to assume that you have all the data needed to fill a 2D grid using your x and y coloumns is present. I am not assuming that it is in a ordered form. With these assumptions the code will look like
data = csvread(filename)
for i=1:length(data)
matrix(data(i,2)+1,data(i,1)+1)=data(i,3) // +1 is added since there maybe a index starting from 0 and matlab needs them to start from 1
end
image(matrix)
For other delimiters use
data = dlmread(filename,delimiter)

Related

Gnuplot: Making a gif of a map generated with matrix?

I generated a .dat file with 100 matrix 15x15, now I want to create a gif which shows the evolution from the first to the last matrix. They are all matrix with 1 or -1, so if I want to represent the inicial matrix I can copy and paste it in another file and I put this in gnuplot:
plot 'firstmatrix.dat' matrix with image
It represents the 1, -1 matrix with yellow and black.
To create the gif I'm trying to do this in gnuplot:
set terminal gif animate delay 20
set output 'evolution.gif'
set xrange [0:15]
set yrange [0:15]
N=15
nframes=5
do for [i=1:int(nframes)] {
plot 'evolution.dat' every ::(i-1)*N+1::i*N matrix with image
}
I intend to read from the first line of the file to the 15th line, then from the 16th to the 30th and so on.
I put only 5 frames to see better the result, and I obtain that the gif shows the first matrix in the first frame and nothing more, only white frames.
The error message is four times this one:
warning: Skipping data file with no valid points
So the data for the first frame, the first matrix, is well processed but not the rest. So here is my problem, I don't know why it process good the first one and no more.
Thanks in advance.
It shows only the first matrix in the first frame
You've been pretty close. But it took me also some iterations and testing...
Apparently, slicing a block of rows from a matrix requires every :::rowFirst::rowLast (mind the 3 colons at the beginning). And then gnuplot apparently takes the row index of the whole matrix as y-coordinate. Since you want to have it "on top of each other" you need the modulo operator % (check help operators binary). It might have been a bit easier if your matrices were separated by one or two empty lines.
Code:
### animated matrix data
reset session
### create some random data
set print $Data
do for [n=1:20] {
do for [y=1:15] {
Line = ''
do for [x=1:15] {
Line=Line.sprintf("% 3g",int(rand(0)*2)*2-1)
}
print Line
}
}
set print
set terminal gif animate delay 30
set output "tbMatrixAnimated.gif"
unset key
N=15
do for [i=1:20] {
plot $Data u 1:(int($2)%N):3 matrix every :::N*(i-1)::N*i-1 with image
}
set output
### end of code
Result: (only 20 matrices)

How these matrices are working?

I am reading a article about pattern reconigation. I am not understanding how this 8 column coming from. and how its output is generating.
I tried to get the concept but i am not getting how first matrix have 8 column ? and how its calculating output ?
The network of figure 1 is trained to recognise the patterns T and H.
The associated patterns are all black and all white respectively as
shown below.
If we represent black squares with 0 and white squares with 1 then the
truth tables for the 3 neurones after generalisation are;
enter image description here
Each table represents one line of you image.
Each column(Xij) of your table represents possible combinations of those pixels in one line(of your input image) and OUT represents if those combinations evaluate to true or false.
There are 8 columns because there are 8 possibilities of combining 3 values of 1 and 0 (2 at the power of 3).
I think it's easier if you look at those tables on vertical(transpose).

Syntax of animating in gnuplot using 'blocks' in a single file

I have a data file of two column, ten row ‘blocks’, with two lines of whitespace between each block. Each frame of the animation I want the ten points in the successive block to be plotted, until the end of the data file.
I've searched for how to do this for ages but can't appropriate any of the examples I've found to my case as I don't understand the syntax and can't find an explanation of it anywhere.
How would the example here or here be extended to blocks of x rows?
E.g., in the second example, pasted below for easy reference
n=10 # n present here the number of blocks in your file
plot "output.dat" using 1:2 every :::i::i
i=i+1
if i<n reread
What do the number of colons in every :::i::i mean? Is that three data lines, then two whitespace lines? (Appropriating assuming that doesn't work.)
(If this question seems too obvious, I assure you it is due to my lack of knowledge, not my lack of effort in researching. I would very gladly accept being pointed towards the place in the documentation where this is covered.)
This is not an answer regarding the syntax of every, but a way to achieve this animated plot that is scalable for future users in my position.
A datablock, or block, is x consecutive lines of data, separated by exactly two lines of whitespace.
The plot command option index can be used to access each of these blocks.
For example, plot "datafile.dat" using 1:2 index 1 would plot only the points in the first dataset (block of data).
A loop can be used to animate your data. The stats command can be used to find the number of datasets/blocks in your file, to use in the loop.
set terminal x11
stats 'bdata.txt' nooutput
set xrange [0:10]
set yrange [0:10]
do for [a = 1: int(STATS_blocks - 1)] {
plot "bdata.txt" using 1:2 index a
pause 0.1
}

Matlab 3-layered Image Matrix

I'm currently learning Matlab and having troubles understand the image section. I have this powerpoint slide about matlab image:
****Image Matrices*
3-layered image matrices -- Read ‘rainbow.jpg’ into im
Subset im to im2 – e.g.( 155:184 , 145:164, : )
*1 layer of an image – Get the red layer in im2****
I would like to ask what does(155:184, 145:164, :) represent? What does each value in parenthesis represent? Also, what does the semi-colon represent?
Thank you!
Say you have a 3 dimensional matrix A and you are indexing your matrix. I will use the example above:
A(155:184,145:164,:)
155:184 in the first entry means take rows 155 to 184
145:164 in the second entry means take columns 145 to 164
and the semi-colon in the last entry means take EVERY element along the 3rd dimension. So if you have 200x200x3, the semi-colon will take the 3 arrays along that 3rd dimension.

How can you load a matrix in from a file in octave?

I loaded an 8bit grayscale image into octave with imread, then I saved it in ascii format and got a giant list of all of it's values. Then I dithered it with a 2x2 matrix in Java and printed out a list of each dithered matrix all on one line.
If the matrix for a pixel in my program turns out to be this:
0 2
3 1
Then the output that my program generates looks like:
0 2 3 1
Then I have all of the matrices for each pixel in that format all on one line. How can I load this into octave to see the final dithered image?
I was messing around with octave and created a simple matrix like the first one I showed and saved that to a file, then I was able to put it all on one line and load it up again just fine. I tried to then replace the matrix in that file with the matrix my program generated, but octave doesn't seem to be loading that in. The matrix it tried to load it to doesn't get changed at all.
I dont think I fully understood your question, but if you are having trouble interacting with the file system, I suggest using the functions dlmread and dlmwrite.
The follow code should provide an example to get you started:
%Random 4 by 4 matrix
M = rand(4,4)
%Write matrix to file system
dlmwrite("filename.txt",M);
%Read it back and store in an other variable
M2 = dlmread("filename.txt")

Resources