Untitled
unknown
python
3 years ago
5.6 kB
2
Indexable
def integral_image(img):
# Your implementation of integral image
pass
def sum_image(image):
# Your implementation for summing up pixels
pass
def task1():
# Your implementation of Task1
pass
# ************************************************
# ********************TASK2***********************
def equalize_hist_image(img):
# Your implementation of histogram equalization
pass
def task2():
# Your implementation of Task2
pass
# ************************************************
# ********************TASK4***********************
def get_kernel(sigma):
# Your implementation of getGaussianKernel
pass
def task4():
pass
# ************************************************
# ********************TASK5***********************
def task5():
img = cv.imread("bonn.png")
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv2_imshow(gray)
blur1 = cv.GaussianBlur(gray,(5,5),2)
blur2 = cv.GaussianBlur(gray,(5,5),2*math.sqrt(2))
cv2_imshow(blur1)
cv2_imshow(blur2)
# ************************************************
# ********************TASK7***********************
def add_salt_n_pepper_noise(img):
numbers = [0, 1, 255]
distributions = [0.15, 0.7, 0.15]
cells = np.shape(img)[0]*np.shape(img)[1]
random_number = random.choices(numbers, distributions,k=cells)
counter = 0
res = np.empty(np.shape(img))
for row in range(0,np.shape(img)[0]):
for pixels in range(0,np.shape(img)[1]):
if random_number[counter] == 0:
img[row][pixels] = 0
elif random_number[counter] == 255:
img[row][pixels] = 255
else :
res[row][pixels] = img[row][pixels]
counter+=1
return img
def calculateDistance(i1, i2):
#print(np.sum((i1-i2)**2)/(np.shape(i1)[0]*np.shape(i1)[1]))
return math.sqrt(np.sum((i1-i2)**2)/(np.shape(i1)[0]*np.shape(i1)[1]))
def task7():
img_salt_pepper = add_salt_n_pepper_noise(cv.cvtColor(cv.imread("bonn.png"), cv.COLOR_BGR2GRAY))
img = cv.imread("bonn.png")
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
filter_range = [1,3,5,7,9]
best_gauss_range = 0
best_median_range = 0
best_bilateral_range = 0
gauss_dis = 10000
median_dis = 10000
bilateral_dis = 10000
for i in filter_range:
if calculateDistance(gray,cv.GaussianBlur(img_salt_pepper,(i,i),3)) < gauss_dis:
gauss_dis = calculateDistance(gray,cv.GaussianBlur(img_salt_pepper,(i,i),3))
#print(str(i) + " "+str(calculateDistance(gray,cv.GaussianBlur(img_salt_pepper,(i,i),2))))
best_gauss_range = i
if calculateDistance(gray,cv.medianBlur(img_salt_pepper, i)) < median_dis:
median_dis = calculateDistance(gray,cv.medianBlur(img_salt_pepper, i))
best_median_range = i
if calculateDistance(gray,cv.bilateralFilter(img_salt_pepper,i,150,150)) < bilateral_dis:
bilateral_dis = calculateDistance(gray,cv.bilateralFilter(img_salt_pepper,i,150,150))
best_bilateral_range = i
cv2_imshow(gray)
cv2_imshow(img_salt_pepper)
print("best gauss range is " + str(best_gauss_range))
cv2_imshow(cv.GaussianBlur(img_salt_pepper,(best_gauss_range,best_gauss_range),1))
print("best median range is " + str(best_median_range))
#print("damn" +str(calculateDistance(gray,cv.medianBlur(img_salt_pepper, 5))))
# cv2_imshow(cv.medianBlur(img_salt_pepper, 3))
#cv2_imshow(gray)
cv2_imshow(cv.medianBlur(img_salt_pepper, best_median_range))
print("best bilateral range is " + str(best_bilateral_range))
cv2_imshow(cv.bilateralFilter(img_salt_pepper,best_bilateral_range,150,150))
#cv2_imshow(gray)
# ************************************************
# ********************TASK8***********************
def task8():
img = cv.imread("bonn.png")
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
k1 = np.array([[0.0113,0.0838,0.0113],[0.0838,0.6193,0.0838],[0.0113,0.0838,0.0113]])
k2 = np.array([[-0.8984,0.1472,1.1410],[-1.9075,0.1566,2.1359],[-0.8659,0.0573,1.0337]])
filter1 = cv.filter2D(gray,-1,k1)
filter2 = cv.filter2D(gray,-1,k2)
print("gray image")
cv2_imshow(gray)
print("image filtered by k1")
cv2_imshow(filter1)
print("image filtered by k2")
cv2_imshow(filter2)
u1,s1,v1 = cv.SVDecomp(k1)
u2,s2,v2 = cv.SVDecomp(k2)
filter1_decomposed = cv.filter2D(cv.filter2D(gray,-1,u1[:,:1]),-1,v1[0])
filter2_decomposed = cv.filter2D(cv.filter2D(gray,-1,u2[:,:1]),-1,v2[0])
print("image filtered by decomposed k1")
cv2_imshow(filter1_decomposed)
print("image filtered by decomposed k2")
cv2_imshow(filter2_decomposed)
print("mean pixel wise diffrence squared between filter 1 and gray image is = " + str(calculateDistance(gray, filter1)))
print("mean pixel wise diffrence squared between filter 1 decomposed and gray image is = " + str(calculateDistance(gray, filter1_decomposed)))
print("mean pixel wise diffrence squared between filter 2 and gray image is = " + str(calculateDistance(gray, filter2)))
print("mean pixel wise diffrence squared between filter 2 decomposed and gray image is = " + str(calculateDistance(gray, filter2_decomposed)))
#Sigma = np.zeros((k1.shape[0], k1.shape[1]))
#Sigma[:k1.shape[1], :k1.shape[1]] = np.diag(s)
#print(np.linalg.svd(k2))
Editor is loading...