Step 1: Loading image
% First we will be loading imageimg =imread(‘[Link]’)
figure('Name','Original Image'); imshow(img);
I am using Penguins Image.
Step 2: Converting it into a grayscale image
% Now let's convert into grayscale image
gray_img=rgb2gray(img);
figure('Name','Gray Scale Image'); imshow(gray_img);
Step 3: Get Fourier Coefficients
%Get Fourier Transform of an image
F = fft2(gray_img);
figure('Name','Fourier transform of an image'); imshow(abs(F), []);
Step 4: Shift Zero Frequency Component
%Get the centered spectrum
Fsh = fftshift(F);
figure('Name','Centered fourier transform of Image');
imshow(abs(Fsh), []);
STEPS TO PERFORM UP SAMPLING:
1. Consider an array A of size 1xM
2. Obtain the upsample Ratio N
3. Pre-allocate an array B of size 1x(M*N)
4. If the index is divisible by N then update the array B with value of A else zero
MATLAB Code:
% First we will be loading image
img=imread('[Link]')
figure('Name','Original Image'); imshow(img)
%Get Fourier Transform of an image
fourier=fft2(img)
figure('Name','Fourier transform of an image');imshow(fourier,[])
%Fourier transform image is not giving any information or detail about
%the original image so that’s why \ zero-frequency component/very low
frequency
%components are shifted towards center of the spectrum.
%Get the centered spectrum
Fsh = fftshift(fourier);
figure('Name','Centered fourier transform of Image'); imshow(Fsh, []);
%reconstruct the Image
ifourier=ifft2(fourier)
figure('Name','Reconstructed Image');imshow(ifourier,[])
img=imread('[Link]')
img(1:2:end)=0
figure,imshow(img)
imshow(I,[]) displays the grayscale image I scaling the display based
on the range of pixel values in I. imshow uses [min(I(:)) max(I(:))] as
the display range, that is, the minimum value in I is displayed as
black, and the maximum value is displayed as white.
Print Command
print("Hello, World!")
Hello, World!
Variable Intalization
y = int(3)
print(y)# y will be 3"
3
z = float(3)
print(z)# z will be 3.0"
3.0
x = y = z = "Orange"
print(x)
print(y)
print(z)
Orange
Orange
Orange
Data Type Conversion
x = 1 # int
y = 2.8 # float\n",
z = 1j # complex\n",
#convert from int to float:
a = float(x)
#convert from float to int:
b = int(y)
#convert from int to complex:
c = complex(x)
print(a)
print(b)
print(c)
print(type(a))
print(type(b))
1.0
2
(1+0j)
<class 'float'>
<class 'int'>
Variable Casting
x = int(1) # x will be 1\n",
y = int(2.8) # y will be 2\n",
print(x)
print(y)
1
2
Strings
a = "Hello, World!"
print(a)
print(a[0])
print(a[1])
Hello, World!
H
e
Condition Statement
if 5 > 2:
print("Five is greater than two!")
Five is greater than two!
a = 33
b = 33
if b > a:
print("b is greater than a"),
elif a == b:
print("a and b are equal")
a and b are equal
Operators
x = "Python is "
y = "awesome"
z= x+y
print(z)
Python is awesome
print(10*5)
50
x = 15
y=4
print('x / y =',x/y)
x / y = 3.75
Comparsion Operators in Pythons
x = 10
y = 12
print('x > y is',x>y)
print('x < y is',x<y)
# Output: x == y is False
print('x == y is',x==y)
# Output: x != y is True
print('x != y is',x!=y)
# Output: x >= y is False
print('x >= y is',x>=y)
# Output: x <= y is True
print('x <= y is',x<=y)
x > y is False
x < y is True
x == y is False
x != y is True
x >= y is False
x <= y is True
Logical Operations
x = True
y = False
print('x and y is',x and y)
print('x or y is',x or y)
print('not x is',not x)
x and y is False
x or y is True
not x is False
While loop
i=1
while i < 6:
print(i)
i += 1
1
2
3
4
5
For Loop
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
apple
banana
cherry
Range function
The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1
(by default), and ends at a specified number.
for x in range(6):
print(x)
0
1
2
3
4
5
Functions
def my_function():
print("Hello from a function")
my_function()
Hello from a function
Global variable
variables that are created outside of a function (as in all of the examples above) are known as global
variables
x = "abc"
def myfunc():
print("Python is " + x)
myfunc()
Python is abc
Lists
Lists are used to store multiple items in a single variable
thislist = ["apple", "banana", "cherry"]
print(thislist)
['apple', 'banana', 'cherry']
thislist = ["apple", "banana", "cherry"]
print(len(thislist))
3
list1 = ["abc", 34, True, 40, "male"]
print(list1[0])
abc
last item in the list
thislist = ["apple", "banana", "cherry"]
#print(thislist[1])
print(thislist[-2])
banana
Arrays
cars = ["Ford", "Volvo", "BMW"]
x=cars[0]
cars[0] = "Toyota"# modify the ist value item
print(cars)
['Toyota', 'Volvo', 'BMW']
import pandas as pd
df=pd.read_csv('play_tennis.csv')
type(df)
[Link]()
[Link]
[Link][0]
Access 1st three rows
[Link][0:2]
Access 1st two rows
[Link][0:2]
One way to access unique elements is the 'iloc' method, where you can access the 1st row and first column as
follows
[Link][0,0:2]
You can access columns with rows as well
[Link][0:2,0:2]