-
Notifications
You must be signed in to change notification settings - Fork 0
/
Project_1.py
145 lines (108 loc) · 4.44 KB
/
Project_1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# -*- coding: utf-8 -*-
"""
@author: mehme
"""
import cv2
import numpy as np
from matplotlib import pyplot as plt # for showing the histogram
def vertical_flip(img):
height,width=img.shape
same_image=[] # copy image
for j in range(height): # take all rows
same_image.append(img[j,:])
new_image=[]
for a in range(0,len(same_image),1): # reverse all rows and write to a new list
new_image.append(same_image[a][::-1])
vertical = np.reshape(np.array(new_image), (375, 1242)) # reshaping
return vertical
def horizontal_flip(img):
height,width=img.shape
same_image=[] # copy image
for j in range(width): # take all columns
same_image.append(img[:,j])
new_image=[]
for a in range(0,len(same_image)): # reverse all columns and write to a new list
new_image.append(same_image[a][::-1])
List_flatten = []
for j in range(height): # flating the array according to pixel order
for i in range (width):
List_flatten.append(new_image[i][j])
horizontal = np.reshape(np.array(List_flatten), (375, 1242)) # reshaping the flatten array
return horizontal
#rotates the image 90 deg counterclockwise
def rotate_90_counter(height, width, i, j):
# reassign pixels
i_new = -j + width - 1
j_new = i
return i_new, j_new
def rotate_90(height, width, i, j):
# reassign pixels
i_new = j
j_new = -i + height - 1
return i_new, j_new
def negative(im):
height=len(im)
width = len(im[0])
for row in range(height):
for col in range(width):
im[row][col] = 255 - im[row][col] # process of making negative
return im
def gamma_transformation(img):
print("Please write gamma value \n")
gamma = float(input()) # take gamme value
gamma_corrected = np.array(255*(img / 255) ** gamma, dtype = 'uint8')# gamma process
return gamma_corrected
def histogram_comp(img):
image_height = img.shape[0]
image_width = img.shape[1]
histogram = np.zeros([256], np.int32) # pixel values are between 0-255 so there are 256 values.
for x in range(0, image_height):
for y in range(0, image_width):
histogram[img[x,y]] +=1 # counter the histogram value which has "img[x,y]" value
return histogram
def plot_histogram(Histogram):
plt.figure() # creating empty figure
plt.title("Histogram Of The Image")
plt.xlabel("Intensity Level")
plt.ylabel("Intensity Frequency")
plt.xlim([0, 256]) # x label limit
plt.plot(Histogram) # plotting figure
def main():
# read image
print("Please write absolute path of the image e.g. F:/yedek/Ders PDF/4.S/II/Image Processing/Homework1/image2.png \n")
path = input()
image_raw = cv2.imread(path) #read image
gray = cv2.cvtColor(image_raw, cv2.COLOR_BGR2GRAY) # converts all images to GRAYSCALE
image = np.copy(gray)
cv2.imshow("Source image",image) # shows real image
cv2.waitKey()
vertical = vertical_flip(image)
cv2.imshow("vertical",vertical)
cv2.waitKey()
horizontal = horizontal_flip(image)
cv2.imshow("horizontal",horizontal)
cv2.waitKey()
rotate_90_counter_img = np.zeros([image.shape[1], image.shape[0]], dtype=image.dtype) # empty array that has dimensions of source image
for i in range(image.shape[0]-1):
for j in range(image.shape[1]-1):
newPoint = rotate_90_counter(image.shape[0], image.shape[1], i, j)
rotate_90_counter_img[newPoint[0], newPoint[1]]=image[i,j] # assign values to empty array
cv2.imshow("90 deg counterclockwise",rotate_90_counter_img)
cv2.waitKey()
rotate_90_img = np.zeros([image.shape[1], image.shape[0]], dtype=image.dtype) # empty array that has dimensions of source image
for i in range(image.shape[0]-1):
for j in range(image.shape[1]-1):
newPoint = rotate_90(image.shape[0], image.shape[1], i, j)
rotate_90_img[newPoint[0], newPoint[1]]=image[i,j] # assign values to empty array
cv2.imshow("90 deg clockwise",rotate_90_img)
cv2.waitKey()
negative_img = negative(image)
cv2.imshow("negative",negative_img)
cv2.waitKey()
gamma_trans = gamma_transformation(image)
cv2.imshow("gamma transformation",gamma_trans)
cv2.waitKey()
histogram = histogram_comp(image)
plot_histogram(histogram)
if __name__ == "__main__":
main()