Output of convolution - neural-network

Suppose we have an 5x5 size image and a 3x3 size kernel with
Stride 2 and Padding On. What is the size of the output image after passing through a convolution layer in neural networks.

The other answer is correct, but here is a drawing which visualizes why this formula holds:
I: Image size, K: Kernel size, P: Padding, S: Stride
I will explain the formula for a single direction only (shifting the filter to the right), since its the same principle for the other direction.
Imagine, you place the kernel (the filter) in the upper left corner of the padded image.
Then there are I-K+2P pixels left over on the right hand side. If your stride is S, you will be able to place the kernel on this remaining part at floor( (I-K+2*P)/S ) positions. You can verify that you need "floor" for an image which has 4x4 pixels. You have to add one for the initial position of the kernel, to get the total number of kernel-positions.
Thus there are floor( (I-K+2*P)/S ) + 1 positions in total - which is the formula for your output size.
Hope that helps.

Let's consider a more general case:
Input is an image with size I*I. The input is padded with P*P pixels. The kernel has K*K size, and the strides are S*S. Then, the output has a O*O size which can be computed using a simple formula:
O = [(I+2*P-K)/S]+1; where [] shows the floor function.
So, you're answer is 3*3 since O=[(5+2*1-3)/2]+1=3.

Related

How do i effectively compare an image with another image whose resolution is double of the original image?

I have two sets of 3D images (they come in form of 2D stacks). Image A is 10 micron, with size: 1000 x 1024 x 1017, while image B is 5 micron, with size: 2004 x 2048 x 2036. I like to make some computations on randomly chosen set of the 2D slices of A, and then compare this to the same set of slices for B. However, since B has twice the number of slices for each slice of A, will it be sensible to compare two slices of B to each of A? If so, how do i determine exactly which of the two slices of B make up a slice of A?
While contemplating on this, i also thought of blowing up A by 2 using imresize function for each 2D slice that i chose for the computation. Will it be okay to compare this new B with the A, considering that i have completely ignored what happens with the z-coordinate?
Thank you.
As you mentioned this is microCT, I am assuming that both images are different size resolution of the same object. This means that pixels have specific spatial location, not only value, therefore for this case, there are no pixels (assuming a pixel is a infinitesimally small dot in the center of the cube) that match in both images.
So, lets assume that in image A, the locations of the pixel centers are their indices (1,1,1), (1,1,2) etc. This means that the image starts (pixel boundaries) at "world value" 0.5, and ends at size(imgA)+0.5
Now, first lets transform the desired pixel coordinates for interpolation to this range. imgB pixel centers are then in locations (ind-0.5)*size(imgA)/size(imgB)+0.5.
Example: Assume
size(imgA,1)=3; size(imgB,1)=4;
therefore the pixels in imgA are at x location 1:3. The pixels on imgB are, using the above formula, in [0.8750 1.6250 2.3750 3.1250].
Note how the first pixel is 0.375 from 0.5 (our image border) and the next pixel is at 0.75 (double 0.375*2).
We scaled a higher resolution image to the same "real world" coordinates.
Now to the real stuff.
We need to create the desired coordinates in the reference (A) image. For that, we do:
[y, x, z]=...
ndgrid((1:size(imgB,1)-0.5)*size(imgA,1)/size(imgB,1)+0.5),...
(1:size(imgB,2)-0.5)*size(imgA,2)/size(imgB,2)+0.5),...
(1:size(imgB,3)-0.5)*size(imgA,3)/size(imgB,3)+0.5);
Now these 3 have the coordinates we want. Caution! each of these are size(imgB) !!! You need to have the RAM 5*size(imgB) in total to work with this.
Now we can interpolate
imAinB=interp3(imgA,x,y,z,'linear'); % Or nearest
It seems to be that your function is imresize3. You can change one volume to the other's dimentions with:
B = imresize3(V,[numrows numcols numplanes])
You can also explore interpolation methods.

How to determine Boundary Cut Utilization MATLAB?

Working on 2D Rectangular Nesting. Need to find the utilization percentage of the material. Assuming i have the length, breadth, left-bottom position of each rectangle. What is the best way to determine the Boundary-Cut Utilization?
Objective:- To find the AREA under the RED Line.
Sample images attached to depict what i have done and what i need.
What i have done
what i need
Another Example image of rectangles packed with allowance
If you're interested in determining the total "area" underneath the red line, one suggestion I have is if you have access to the Image Processing Toolbox, simply create a binary image where we draw all of the rectangles on the image at once, fill all of the holes, then to determine the area, just determine the total sum of all of the binary "pixels" in the image. You said you have the (x,y) positions of the bottom-left corner of each rectangle, as well as the width and height of each rectangle. To make this compatible in an image context, the y axis is usually flipped so that the top-left corner of the space is the origin instead of the bottom-left. However, this shouldn't affect our analysis as we are simply reflecting the whole 2D space downwards.
Therefore, I would start with a blank image that is the same size as the grid you are dealing with, then writing a loop that simply sets a rectangular grid of coordinates to true for each rectangle you have. After, use imfill to fill in any of the holes in the image, then calculate the total sum of the pixels to get the area. The definition of a hole in an image processing context is any black pixels that are completely surrounded by white pixels. Therefore, should we have gaps that are surrounded by white pixels, these will get filled in with white.
Therefore, assuming that we have four separate variables of x, y, width and height that are N elements long, where N is the number of rectangles you have, do something like this:
N = numel(x); %// Determine total number of rectangles
rows = 100; cols = 200; %// Define dimensions of grid here
im = false(rows, cols); %// Declare blank image
%// For each rectangle we have...
for idx = 1 : N
%// Set interior of rectangle at location all to true
im(y(idx)+1:y(idx)+height(idx), x(idx)+1:x(idx)+width(idx)) = true;
end
%// Fill in the holes
im_filled = imfill(im, 'holes');
%// Determine total area
ar = sum(im_filled(:));
The indexing in the for loop:
im(y(idx)+1:y(idx)+height(idx), x(idx)+1:x(idx)+width(idx)) = true;
Is a bit tricky to deal with. Bear in mind that I'm assuming that y accesses the rows of the image and x accesses the columns. I'm also assuming that x and y are 0-based, so the origin is at (0,0). Because we access arrays and matrices in MATLAB starting at 1, we need to offset the coordinates by 1. Now, the beginning index for the row starts from y(idx)+1. We end at y(idx) + height(idx) because we technically start at y(idx)+1 but then we need to go up to height(idx) but then we also subtract by 1 as your coordinates begin at 0. Take for example a line with the width of 20, from x = 0 to x = 19. This width is 20, but we draw from 0, up to 20-1 which is 19. Because of the indexing starting at 1 for MATLAB, and the subtraction of 1 due to the 0 indexing, the +1 and -1 cancel, which is why we are just left with y(idx) + height(idx). The same can be said with the x coordinate and the width.
Once we draw all of the rectangles in the image, we use imfill to fill up the holes, then we can sum up the total area by just unrolling the whole image into a single vector and invoking sum. This should (hopefully) get what you need.
Now, if you want to find the area without the filled in holes (I suspect this is what you actually need), then you can skip the imfill step. Simply apply the sum on the im, instead of im_filled, and so:
ar = sum(im(:));
This will sum up all of the "white" pixels in the image, which is effectively the area. I'm not sure what you're actually after, so use one or the other depending on your needs.
Boundary-Cut Area without using Image Processing Toolbox.
The Detailed question description and answer could be found here
This solution is applicable only to Rectangular parts.

Finding size of reconstructed image in iradon in MATLAB

Consider the following code:
P = Phantom(256);
theta = 0:1:179;
R = radon(P, theta);
I = iradon(R, theta);
iradon.m calculates the size of the reconstructed image using
N = 2*floor(size(R,1)/(2*sqrt(2)))
But why this formula? It gives N as approximately equal to number of projections divided by square root of 2. But how will this give size of image? Is there any better way to find the size of image given R and theta?
size(R,1) does not give you the number of projections, but the projection size. Number of projections would be size(R,2).
The output of radon is of size n x m where n = the size of individual projections and m is the number of projections. The projection size is larger than the image size - imagine taking a projection at 45 degrees, you need the projection to be about sqrt(2) times as large as the image in order not to lose any information. iradon is just doing the reverse calculation to get the original image size back.
In practice, possibly because of the way MATLAB has implemented radon, the size of your reconstructed image will be slightly larger than the original image.
I think it has to do with the maximum size of a square that can fit within a circle. Diameter of circle is the width of 2D images fed into iRadon.

How to make "well" a ridge-shape from a given 2d line? (gaussian, matlab)

My goal is to make a ridge(mountain)-like shape from the given line. For that purpose, I applied the gaussian filter to the given line. In this example below, one line is vertical and one has some slope. (here, background values are 0, line pixel values are 1.)
Given line:
Ridge shape:
When I applied gaussian filter, the peak heights are different. I guess this results from the rasterization problem. The image matrix itself is discrete integer space. The gaussian filter is actually not exactly circular (s by s matrix). Two lines also suffer from rasterization.
How can I get two same-peak-height nice-looking ridges(mountains)?
Is there more appropriate way to apply the filter?
Should I make a larger canvas(image matrix) and then reduce the canvas by interpolation? Is it a good way?
Moreover, I appreciate if you can suggest a way to make ridges with a certain peak height. When using gaussian filter, what we can do is deciding the size and sigma of the filter. Based on those parameters, the peak height varies.
For information, image matrix size is 250x250 here.
You can give a try to distance transform. Your image is a binary image (having only two type of values, 0 and 1). Therefore, you can generate similar effects with distance transform.
%Create an image similar to yours
img=false(250,250);
img(sub2ind(size(img),180:220,linspace(20,100,41)))=1;
img(1:200,150)=1;
%Distance transform
distImg=bwdist(img);
distImg(distImg>5)=0; %5 is set manually to achieve similar results to yours
distImg=5-distImg; %Get high values for the pixels inside the tube as shown
%in your figure
distImg(distImg==5)=0; %Making background pixels zero
%Plotting
surf(1:size(img,2),1:size(img,1),double(distImg));
To get images with certain peak height, you can change the threshold of 5 to a different value. If you set it to 10, you can get peaks with height equal to the next largest value present in the distance transform matrix. In case of 5 and 10, I found it to be around 3.5 and 8.
Again, if you want to be exact 5 and 10, then you may multiply the distance transform matrix with the normalization factor as follows.
normalizationFactor=(newValue-minValue)/(maxValue-minValue) %self-explanatory
Only disadvantage I see is, I don't get a smooth graph as you have. I tried with Gaussian filter too, but did not get a smooth graph.
My result:

Variance and Mean of Image

I am calculating mean and variance of my original and stego image to compare them
I am using grayscale BMP image for comaprison
image=imread("image name")
M = mean(image(:))
V = var((image(:)))
Is this is correct way fo calculating mean/var in MATLAB? My Variance is getting more than mean..
Any help appreciated..
These are indeed the correct way to calculate the mean and variance over all the pixels of your image.
It is not impossible that your variance is larger than the mean as both are defined in the following way:
mean = sum(x)/length(x)
variance = sum((x - mean(x)).^2)/(length(x) - 1);
For example, if you generate noise from a standard normal distribution with randn(N,1), you will get N samples, and if you calculate the mean and variance, you will get approximately 0 and 1. So there as well, your variance may well be larger than the mean.
Both have a totally different meaning: the mean gives you an idea where your pixels are (i.e. are they white, black, 50% gray, ...). The mean will give you an idea of what pixel color to choose to summarize the color of the complete image. The variance gives you an idea how the pixel values are spread: e.g. if your mean pixel value is 50% gray, are most of the other pixels also 50% gray (small variance) or do you have 50 black pixels and 50 white pixels (large variance)? So you could also view it as a way to get an idea how well the mean summarizes the image (i.e. with zero variance, most of the information is captured by the mean).
edit: For the RMS value (Root Mean Square) of a signal, just do what the definition says. In most cases you want to remove the DC component (i.e. the mean) before calculating the RMS value.
edit 2: What I forgot to mention was: it also makes little sense to compare the numerical value of the variance with the mean from a physical point of view. The mean has the same dimension as your data (in case of pixels, think of intensity), while the variance has the dimension of your data squared (so intensity^2). The standard deviation (std in MATLAB), which is the square root of the variance on the other hand has the same dimension as the data, so there you could make some comparisons (it is another question whether you should do such comparison).
If you are workign with RGB image (H x W x 3), you have to calculate mean and variance separately for each channel. In this case the mean pixel will also be 3-values vector.
for ch = 1:3
M(ch) = mean(reshape(img(:,:,ch),[],1));
V(ch) = var(reshape(img(:,:,ch),[],1));
end
MATLAB has function image. Avoid using it as a variable.