Matlab 3-layered Image Matrix - image

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.

Related

Convolutional Neural Networks - Theory

I am sorry for asking this stupid question, but after a bit thinking, I still don't get it yet:
According to Jordi Torres (see here), if we look at an image with 28x28 = 784 pixels, then one way to implement this is to let one neuron of a hidden layer learn about 5x5 = 25 pixels of the input layer:
However, as he explains it:
Analyzing a little bit the concrete case we have proposed, we note that, if we have an input of 28×28 pixels and a window of 5×5, this defines a space of 24×24 neurons in the first hidden layer because we can only move the window 23 neurons to the right and 23 neurons to the bottom before hitting the right (or bottom) border of the input image. We would like to point out to the reader that the assumption we have made is that the window moves forward 1 pixel away, both horizontally and vertically when a new row starts. Therefore, in each step, the new window overlaps the previous one except in this line of pixels that we have advanced.
I really don't get why we need a space of 24x24 neurons in the first hidden layer? Since I take 5x5 windows (which have 25 pixels out of 784 in them), I thought we would need 785/25 = 32 neurons at all. I mean, doesn't one neuron of the hidden layer learn the property of 25 pixels?
Apparently not, but I am really confused.
You're assuming non-overlapping 5x5 segments, but that's not the case. In this example, the first output is derived from rows 1-5, columns 1-5 of the input. The next one uses rows 1-5, columns 2-6, on to rows 1-5, columns 24-28, then rows 2-6, columns 1-5, etc. etc. until rows 24-28, columns 24-28. This is referred to as a "stride" of 1.

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).

"Submatrix incorrectly defined" when trying to decrease color depth of greyscale images

I'm trying to make Scilab receive a certain JPEG image then transform it into a matrix of values between 0 and 255 (normal 8bit depth image) and then rearrange those values into smaller depths. The proposal is to make all the options from 1bit to 7bits which translates into 2, 4, 8, 16, 32, 64 and 128 different levels of color for them respectively.
We're doing it with greyscale images to make things simpler, since we can simply get any of the 3 channels and work with it as a matrix of rows and columns. I know there are many better ways of doing this, but I need to do it using Scilab since it's for a image processing course at University (signals and linear systems subject from Electrical Engineering to be exact).
What I could come up with, and it worked fine for the test-matrices that I tried, is this:
function y=bits(x,p)
[rows, columns]=size(x);
y=zeros(rows,columns);
aux=round(linspace(0,255,2^p)); //define which values the output can have
for i=1:rows //varies rows
for j=1:columns //varies columns
[aux2,minpos]=min(abs(aux-x(i,j)));//calculates the closest value between the input and the possible output values
y(i,j)=aux(minpos); //get the calculated closest value and puts it at the output
end
end
endfunction
What I can't understand is why it works fine for any hand-made matrix but when I try to send it something bigger (I mean, which more rows and columns) it gives the "Submatrix incorrectly defined." error at line 8 which is the " y(i,j)=aux(minpos);" line.
Edit: Just to add, I'm importing the image using "imread", which is a function of SIVP.
Any help is appreciated, thanks in advance =)
I could fix it with the following at the beginning of the function:
x=x(:,:,1);
x=double(x);
=).

Creating Neutral network for line detection - is it possible?

So I want to detect lines on grayscale images. I have a lot of data 9x9 matrices of pixel ints 1 to 256 and 1*4 matrices of ponnts coords X ,Y, X,Y We have 1 line per 9x9 image or non lines. So what structure should have my NN?
Assuming that you're using the most common variety of neural networks, multillayer perceptrons, you'll have exactly as many input nodes as there are features.
The inputs may include transformed variables, in addition to the raw variables. The number of hidden nodes is selected by you, but you should have enough to permit the neural network to adequately make the mapping.
The number of output nodes will be determined by the number of classes and the representation you choose. Assuming two classes ("line", "not line" seems likely), you may use 1 output node, which indicates the estimated probability of one class (the probability of the remaining class being 1 minus the probability of the first class).
Detecting simple lines on a grayscale image is a well known problem. A Hough transform would be suffice for the job. See http://opencv.willowgarage.com/documentation/cpp/imgproc_feature_detection.html?highlight=hough%20line#cv-houghlines for a function that implement finding lines using Hough Transform.
Can you try the above function and see if it works?
If it doesn't, please update your question with a sample image.

How to extract a linear slice from an image in OpenCV / EMGU

I have an image and two points,
and I want to read the pixels between these two points,
and resample them into a small 1x40 array.
I'm using EMGU which is a C# wrapper for OpenCV.
thanks,
SW
What you are looking for is Bresenham's line algorithm. It will allow you to get the points in the pixel array that best approximate a straight line. The Wikipedia link also contains psuedo code to get you started.
Emgu CV includes method in the Image class for sampling color along a line called Sample.
Refer to the manual for the definition. Here's the link to Image.Sample in version 2.3.0.
You will still have to re-sample/interpolate the points in array returned from Sample to end up with a 40 element array. Since there are a number of ways to re-sample, I'll suggest you look to other questions for that.
Rotate and crop
I'd first try to do it like this:
calculate rotation matrix with (GetRotationMatrix2D)
warp the image so that this line is horisontal (WarpAffine)
calculate new positions of two of your points (you can use Transform)
get image rectangle of suitable width and 1 px high (GetRectSubPix)
Interpolation here and there may affect the results, but you have to interpolate anyway. You may consider cropping the image before rotation.
Iterate over the 8-connected pixels of the line
Otherwise you may use the line iterator to iterate over the pixels between two points. See documentation for InitLineIterator (Sorry, the link is to the Python version of OpenCV, I've never heard of EMGU). I suppose that in this case you iterate over pixels of a line which was not antialiased. But this should be much faster.
Interpolate manually
Finally, you may convert the image to an array, calculate which elements the line is passing through and subsample and interpolate manually.

Resources