def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1],
padding='SAME')
Can someone please kindly help me to understand, what is the functionality of strides in this place?. If possible, please explain each argument
The meaning of stride is how you jump forward in a dimension, your dimension are [batch, height, width, color].
If you set strides=[1, 1, 1, 1], the filter window will move 1 batch, 1 height pixel, 1 width pixel and 1 color pixel
If you set strides=[1, 2, 2, 1], the filter window will move 1 batch, 2 height pixel, 2 width pixel and 1 color pixel (Imagine that you have an image and a window, after processing, you move the window 2 pixel to the left and 2 pixel down)
The effect will result in the output with smaller size output (~1/2 height and 1/2 width)
The padding='SAME' will pad the border of image with zeros so that you can do convolution on the top-left pixel.
Other argument explain can be found here.
Related
I am trying to set 0 values on my c3.js graph to white instead of the default grey value.
I am using
colors: d3.interpolateHslLong(d3.hsl(250, 1, 0.5), d3.hsl(0, 1, 0.5)) currently.
Does anyone know how to define the 0 value color? It seems no matter what I set the min and max values to, 0 remains grey.
Any help much appreciated.
White has 'lightness' equal to 1 (or 100%)
So, the first value will need have: d3.hsl(250, 1, 1), eg
d3.interpolateHslLong(d3.hsl(250, 1, 1), d3.hsl(0, 1, 0.5))
The hue and saturation (the first two values in the HSL constructor) can be anything, but they will affect the colour gradient to the second colour.
The following is the code for sample covariance matrix for the single pixel. I have taken 10 neighboring pixels for the (1,1) including the first pixel of the stacked image. y_1, y_2, y_3 and y_4 are my four images. Kindly do let me know if the question is not clear.
y_cal=cat(3, y_1, y_2, y_3, y_4);
Y_new=reshape(y_cal, [5586, 4]);
Y_new_cov=Y_new(1:10,:);
Y_new_cell = arrayfun(#(ri) Y_new_cov(ri, :)', 1:10, 'UniformOutput', 0);
Y_new_cell_tr= cellfun(#ctranspose, Y_new_cell , 'UniformOutput', 0);
Y_covariance_initial = cellfun(#mtimes, Y_new_cell,Y_new_cell_tr, 'UniformOutput', 0);
Y_covariance_final = Y_covariance_initial{1,1}+Y_covariance_initial{1,2}+Y_covariance_initial{1,3}+Y_covariance_initial{1,4}+Y_covariance_initial{1,5}+Y_covariance_initial{1,6}+Y_covariance_initial{1,7}+Y_covariance_initial{1,8}+Y_covariance_initial{1,9}+Y_covariance_initial{1,10};
Here 10 pixels were taken manually where covariance is implemented. I have the image dimension as 114 X 49. So the final covariance matrix generated is 114 X 49 x 4 X 4. How should I apply a square window to select the neighboring pixels for a target pixel and continue for other pixels also?
Kindly provide necessary assistance as it took me two months to write this code being from a non coding background. Your help will be highly appreciated.
Regards
The standard way would be to use nlfilter. For this function, you supply your function (the one to compute covariance), and it will apply it to a sliding window of your size. For example:
octave> img = rand (64, 64);
octave> img_cov = nlfilter (img, [10 10], #(x) cov (x(:)));
Will call cov (x(:)) for each sliding block of size [10 10] (after padding the original image with zeros), and return an array of size [64 64] (same as the input image) with those results. Since you are using Octave, your window and image may have any number of dimensions. So you can do this:
octave> img = rand (64, 64, 3, 4);
octave> img_cov = nlfilter (img, [10 10 3 4], #(x) cov (x(:)));
An alternative is to get all the sliding windows from your n dimensional image into a column (using im2col), use a function that will work along each column, and then build an image back with col2im. This may, or may not, be faster but does give you a bit more flexibility if you can warp your head around it:
octave> img = rand (64, 64);
octave> im_cols = im2col (img, [10 10], "sliding");
octave> im_cov = you_nd_cov_function (im_cols);
octave> img_cov = col2im (cov (im_cols), [1 1], [55 55], "sliding");
My problem is as follows:
I have the picture of a half cylinder taken from a horizontal perspective and it has square grid lines on it, so I was wondering how can I implement in MATLAB to unwrap this half cylinder so all my grid cells become the same size? I know I will loose lots of resolution in the edge cells and a simple linear interpolation should do the trick, but I do not know how to tell MATLAB to do this. Also I know the geometrical properties of the cylinder, radius and height. Any help is greatly appreciated.
This is the approach I am using, but I am trying to find the transformation that will make the edges be same size as inner cells.
im=imread('Capture.png');
imshow(im);
impixelinfo
r = #(x) sqrt(x(:,1).^2 + x(:,2).^2);
w = #(x) atan2(x(:,2), x(:,1));
f = #(x) [sqrt(r(x)) .* cos(w(x)), sqrt(r(x)) .* sin(w(x))];
g = #(x, unused) f(x);
tform2 = maketform('custom', 2, 2, [], g, []);
im3 = imtransform(im, tform2, 'UData', [-1 1], 'VData', [-1 1], ...
'XData', [-1 1], 'YData', [-1 1]);
figure,
imshow(im3)
I think the transformation is much simpler than what you're trying to do. Take a look at the (forward) transformation to take a flat grid and wrap it around a cylinder. The coordinates along the axis of the cylinder (the y coordinates, in this case) are unchanged. If we take the range of the grid coordinates in the x direction to be [-1,1], the coordinates on the cylinder will be:
sin(x × π/2)
Since this is the forward transformation going from a grid to the cylinder, it is also the inverse transformation going from the cylinder to the grid.
f = #(x, unused) [sin(x (:, 1) * pi / 2), x(:, 2)]
tform2 = maketform('custom', 2, 2, [], f, []);
im3=imtransform(img, tform2, 'UData', [-1 1], 'VData', [-1 1], ...
'XData', [-1 1], 'YData', [-1 1]);
Result:
This still isn't perfect, primarily because the original image has borders around it that we're transforming along with the rest of the image. This could be improved by cropping the image to contain only the cylinder portion.
I am developing some routines in Octave and need to display an image, then plot a curve on top which will hopefully overlay some image features.
However, I cannot work out how to match the origin/scale of the image and the plot. For example, given a 1024x1024 pixel image I can do:
a=imread('image.png');
x=linspace(1,1024,100);
y=x;
imshow(a);
hold on;
plot(x,y);
But the line is not scaled to the image and does not start at a corner. (I know that the image and plot should have origins in different corners). When I examine the graphic coordinates from the cursor position, the image is clearly not at the origin, so I guess this is the basis of the problem.
Use image() instead of imshow() in this case
a = imread ('image.png');
x = linspace (1, 1024, 100);
y = x;
image (a);
hold on
plot (x, y);
axis square
You can plot functions over images this way:
Create an image called stuff.jpg like this, any size is possible but I made mine roughly 6x6 pixels so I could test:
You can plot functions over other functions this way:
octave> x = 0:1:5;
octave> plot(x, (3/2).^x, "linewidth", 2, "color", "blue");
octave> hold on
octave> plot(x, 2.^x, "linewidth", 2, "color", "red");
octave> plot(x, factorial(x), "linewidth", 2, "color", "green");
octave> plot(x, x.^3, "linewidth", 2, "color", "black");
octave>
For me it shows this:
Found that here, it has a walkthrough:
http://ericleschinski.com/c/algorithm_complexity_big_o_notation/
Which plots my power level given my age. It's already over nine thousand.
The problem with image is that it puts (0,0) (rather (min_x,min_y)) at upper-left while we usually expect (0,0) to be at bottom-left.
Also it only uses max and min values of the x and y vectors so doing y(end:-1:1) doesn't work.
im = imread('file.png'); %read the file
image([xmin xmax],[ymin ymax],im(end:-1:1,:,:)); %put the image on the screen upside down
axis('xy'); % flip the image by putting (0,0) at bottom left. Image now right side up
axis('square'); if you want to aspect ratio of the image to be 1:1
hold on;
plot([xmin xmax],[ymin ymax]) % this should draw a diagonal from bottom left to upper right.
% plot whatever you want to overlay
If I make a 4 pixel by 4 pixel image in Matlab using the image() command, it centers the tick marks in the middle of the pixels. I want the tick marks to be centered on the lower left corner of the pixel. Is there some way to do this?
You can specify x and y coordinates of the pixels and shift them by 0.5:
image([0.5,3.5],[0.5,3.5],magic(4))
I think this code will do what you want. It places tick marks only at the edges of the pixels:
A = ...; %# Your 4-by-4 matrix
image([0.5 3.5],[0.5 3.5],A); %# Pixel edges are at 0, 1, 2, 3, and 4
set(gca,'XTick',0:4,'YTick',0:4); %# Place tick marks at 0, 1, 2, 3, and 4
Try the following:
a = randi([0 255], [4 4]);
figure, imagesc(a), caxis([0 255])
b = zeros( size(a)+1 );
b(1:end-1,1:end-1) = a;
figure, pcolor(b), caxis([0 255]), axis ij
Note that I extended the matrix a because pcolor drops the last row/column.