I have a 2-D 512x512 image and I want to display the middle section i.e. A(256-50:256+50,256-50:256+50) but cannot get to work using imshow.
You can use imcrop function
pkg load image
x = 256-50; // starting position (x cordinate)
y = 256-50; // starting position (y cordinate)
a = 100;
b = 100;
B = imcrop(A, [x y a b]);
imshow(B);
This will crop the image starting from x,y by 100 pixels
Related
All, I am trying to take the laplacian of the following function:
g(x,y) = 1/2cx^2+1/2dy2
The laplacian is c + d, which is a constant. Using FFT I should get the same ( in my FFT example I am padding the function to avoid edge effects).
Here is my code:
Define a 2D function
n = 30 # number of points
Lx = 30 # extension in x
Ly = 30 # extension in x
dx = n/Lx # Step in x
dy = n/Ly # Step in x
c=4
d=4
x=np.arange(-Lx/2,Lx/2)
y=np.arange(-Ly/2,Ly/2)
g = np.zeros((Lx,Ly))
lapg = np.zeros((Lx,Ly))
for j in range(Ly):
for i in range(Lx):
g[i,j] = (1/2)*c*x[i]**2 + (1/2)*d*y[j]**2
lapg[i,j] = c + d
kxpad = 2*np.pi*np.fft.fftfreq(2*Lx,d=dx)
#kxpad = (2*np.pi/(2*Lx))*np.arange(-2*Lx/2,2*Lx/2)
#kxpad = np.fft.fftshift(kxpad)
#kypad = (2*np.pi/(2*Ly))*np.arange(-2*Ly/2,2*Ly/2)
#kypad = np.fft.fftshift(kypad)
kypad = 2*np.pi*np.fft.fftfreq(2*Ly,d=dy)
kpad = np.zeros((2*Lx,2*Ly))
for j in range(2*Ly):
for i in range(2*Lx):
kpad[i,j] = math.sqrt(kxpad[i]**2+kypad[j]**2)
kpad = np.fft.fftshift(kpad)
gpad = np.zeros((2*Lx,2*Ly))
gpad[:Lx,:Ly] = g # Filling main part of g in gpad
gpad[:Lx,Ly:] = g[:,-1::-1] # Filling the last 3 columns of gpad with g flipped
gpad[Lx:,:Ly] = g[-1::-1,:]# Filling the last 3 lines of gpad with g flipped
gpad[Lx:,Ly:] = g[-1::-1, -1::-1]# Filling the last 3 lines and last 3 columns of gpad with g flipped in line and column
rdFFT2D = np.zeros((Lx,Ly))
gpadhat = np.fft.fft2(gpad)
dgpadhat = -(kpad**2)*gpadhat #taking the derivative iwFFT(f)
rdpadFFT2D = np.real(np.fft.ifft2(dgpadhat))
rdFFT2D = rdpadFFT2D[:Lx,:Ly]
[
First image is the plot of the original function g(x,y), 2nd image is the analytical laplacian of g and 3rd image is the sugar loaf in Rio de Janeiro( lol ), actually it is the laplacian using FFT. What Am I doing wrong here?
Edit : Commenting on ripple effect.
Cris you mean the ripple effect due to the set_zlimit in the image below?Just to remember you that the result should be 8.
Edit 2 : Using non-symmetrical x and y values, produce the two images.
The padding will not change the boundary condition: You are padding by replicating the function, mirrored, four times. The function is symmetric, so the mirroring doesn't change it. Thus, your padding simply repeats the function four times. The convolution through the DFT (which you're attempting to implement) uses a periodic boundary condition, and thus already sees the input function as periodic. Replicating the function will not improve the convolution results at the edges.
To improve the result at the edges, you would need to implement a different boundary condition, the most effective one (since the input is analytical anyway) is to simply extend your domain and then crop it after applying the convolution. This introduces a boundary extension where the image is padded by seeing more data outside the original domain. It is an ideal boundary extension suitable for an ideal case where we don't have to deal with real-world data.
This implements the Laplace though the DFT with greatly simplified code, where we ignore any boundary extension, as well as the sample spacing (basically setting dx=1 and dy=1):
import numpy as np
import matplotlib.pyplot as pp
n = 30 # number of points
c = 4
d = 4
x = np.arange(-n//2,n//2)
y = np.arange(-n//2,n//2)
g = (1/2)*c*x[None,:]**2 + (1/2)*d*y[:,None]**2
kx = 2 * np.pi * np.fft.fftfreq(n)
ky = 2 * np.pi * np.fft.fftfreq(n)
lapg = np.real(np.fft.ifft2(np.fft.fft2(g) * (-kx[None, :]**2 - ky[:, None]**2)))
fig = pp.figure()
ax = fig.add_subplot(121, projection='3d')
ax.plot_surface(x[None,:], y[:,None], g)
ax = fig.add_subplot(122, projection='3d')
ax.plot_surface(x[None,:], y[:,None], lapg)
pp.show()
Edit: Boundary extension would work as follows:
import numpy as np
import matplotlib.pyplot as pp
n_true = 30 # number of pixels we want to compute
n_boundary = 15 # number of pixels to extend the image in all directions
c = 4
d = 4
# First compute g and lapg including boundary extenstion
n = n_true + n_boundary * 2
x = np.arange(-n//2,n//2)
y = np.arange(-n//2,n//2)
g = (1/2)*c*x[None,:]**2 + (1/2)*d*y[:,None]**2
kx = 2 * np.pi * np.fft.fftfreq(n)
ky = 2 * np.pi * np.fft.fftfreq(n)
lapg = np.real(np.fft.ifft2(np.fft.fft2(g) * (-kx[None, :]**2 - ky[:, None]**2)))
# Now crop the two images to our desired size
x = x[n_boundary:-n_boundary]
y = y[n_boundary:-n_boundary]
g = g[n_boundary:-n_boundary, n_boundary:-n_boundary]
lapg = lapg[n_boundary:-n_boundary, n_boundary:-n_boundary]
# Display
fig = pp.figure()
ax = fig.add_subplot(121, projection='3d')
ax.plot_surface(x[None,:], y[:,None], g)
ax.set_zlim(0, 800)
ax = fig.add_subplot(122, projection='3d')
ax.plot_surface(x[None,:], y[:,None], lapg)
ax.set_zlim(0, 800)
pp.show()
Note that I'm scaling the z-axes of the two plots in the same way to not enhance the effects of the boundary too much. Fourier-domain filtering like this is typically much more sensitive to edge effects than spatial-domain (or temporal-domain) filtering because the filter has an infinitely-long impulse response. If you leave out the set_zlim command, you'll see a ripple effect in the otherwise flat lapg image. The ripples are very small, but no matter how small, they'll look huge on a completely flat function because they'll stretch from the bottom to the top of the plot. The equal set_zlim in the two plots just puts this noise in proportion.
This is a pretty simple question, but I want to code something so that when you are in a certain range, and you click over something, it changes the background. Everything works fine so far, but I only know how to code it so that when you are the exact X and Y coordinates you're in range to click. How do I make it so that if you are in 100-300 X (for example) and 500 - 600 Y that you are in range, as oppose to the exact coordinates of 100 X, and 600 Y?
(here's the snippet of code I'm working with by the way, I can supply full code if you want.)
if 120+75 > mouse[0] > 120 and 50 + 125 > mouse[1] > 125 and x == 110 and y == 60:
print('Click to change')
You have to define the top left position of the rectangular area (x, y) and the size of the area (width, height):
x = 120
y = 125
width = 75
height = 50
The evaluate if the mouse is in the area:
if x < mouse[0] x + width and y < mouse[1] y + height:
print('Click to change')
I recommend to use pygame.Rect and .collidepoint(). Define a rectangle object by (x, y, width, height) and evaluate if the mouse poisition is in the rectangular area:
rect = pygame.Rect(120, 125, 75, 50)
if rect.collidepoint(mouse):
print('Click to change')
It sounds like your trying to make a button, in your code you have mouse and x and y, not sure what x and y are if mouse is mouse position, but here is how to do it with the button x position,y position, width and height.
mouse_pos = pygame.mouse.get_pos()
if mouse_pos[0] > x_pos and mouse_pos[0] < x_pos + width:
if mouse_pos[1] > y_pos and mouse_pos[1] < y_pos + height:
if click:
background = (255,255,255) #change background
Question
Trying to follow Gradients, Gradient Plots and Tangent Planes.
The gradient vectors of (X^2 + Y^2) do not show up correctly in Octave 4.2.0 on Windows. With the code, expected the gradients of a circle diverge from center outwards. However the actual is diagonal.
Please assist to understand what is wrong.
syms x y
f1 = x^2 + y^2;
gradf1 = jacobian(f1,[x,y]);
f1fun = function_handle(f1);
f1xfun = function_handle(gradf1(1));
f1yfun = function_handle(gradf1(2));
[xx, yy] = meshgrid(-1:.1:1,-1:.1:1);
hold on
contour(xx, yy, f1fun(xx, yy), 10)
quiver(xx, yy, f1xfun(xx, yy), f1yfun(xx, yy), 0.5)
axis equal tight
hold off
Expected
Actual
When you perform:
f1xfun = function_handle(gradf1(1));
f1yfun = function_handle(gradf1(2));
The output is:
f1xfun =
#(x) 2 * x % note: single-argument function
f1yfun =
#(y) 2 * y % note: single-argument function
that is AS OPPOSED TO
f1xfun =
#(x,y) 2 * x % two-argument function
f1yfun =
#(x,y) 2 * y % two-argument function
which is what you seem to think was happening. (i.e. the resulting functions actually only take a single input, not both x and y).
Therefore later on when you call f1yfun with two inputs, the second input (i.e. y) is simply silently discarded, and you are essentially calculating 2*x in both axes, hence the diagonal arrows.
tl;dr Your call to quiver should be:
quiver(xx, yy, f1xfun(xx), f1yfun(yy), 0.5);
I think you have a bug in your code and the call to quiver should be
quiver(xx, yy, f1xfun(xx), f1yfun(yy), 0.5)
which then gives (with colormap("jet"))
I'm new to flash and I'm trying to create a board game with actionscript 3.0
I have already created the background (checker squares) for the board and now I have to partition the background by each box. What are the ways I can achieve that? I want to logically put numbers for each square as seen in the picture.
I realized its possible to do it using lasso tool and convert each to symbols. But is there any "lazy" way of doing that? There are lots of cuts I'd have to make in order to do that.
We can use some simple calculations to map some (x, y) value to a number. Lets say:
widht = width of the image
height = height of the image
gridCount = 8
gridWidth = width / gridCount
gridHeight = height / gridCount
Now first we would like to map user click point (x, y) to some integer index i, j to the logical 8 x 8 matrix where top left is index 0, 0.
i = x / gridWidth
j = y / gridHeight
For example, if gridWidth = 60, gridHeight = 50 and user clicks on (10, 15) then i = 0, j = 0.
Now we have to map this i, j to the specified numbers. As bottom line contains 11, 21, 31, ... and every column is increasing, the final number will be:
num = (11 + i * 10) + (gridCount - j - 1)
Converting these equations to AS3 code is straight forward, so I'm not adding them.
Need to reflect a group containing objects as shown in picture.
I have a sample image for what i have done in my current progress.
Objective: Reflection of objects should be done along x and y axis as per the below image
Very easy approach: for reflection you should have copy of the object that will be reflected.
Reflection could be made by myReflectedObject.scaleY = -1
myReflectedObject.scaleY = -1;
myReflectedObject.alpha = 0.4;
//manage Y position accordingly
The simple code looks like this:
copiedDisplayObject.scaleY = -1;
copiedDisplayObject.alpha = 0.4;
That is really a specific example of reflection over a line, where the line happens to be the x axis. (ie y = 0x + 0) If you want to reflect over another line, you can use a matrix. The code below preserves previous transformations on the display object, and reflects it over a line passing through the origin.
var m:Number = 0.25; //example slope -- from line: y = mx + 0
var tmpMatrix = copiedDisplayObject.transform.matrix;
var relectMatrix:Matrix = new Matrix(1-m*m, 2*m, 2*m, m*m -1);
tmpMatrix.concat(relectMatrix);
copiedDisplayObject.transform.matrix = tmpMatrix;
I found the matrix formula here: math.stackexchange question.