def mission2(): img1 = cv2.imread(SAVEDPATH + "\guass_noise.jpg", cv2.IMREAD_GRAYSCALE) img2 = cv2.imread(SAVEDPATH + "\salt_and_pepper_noise.jpg", cv2.IMREAD_GRAYSCALE) show(img1) show(img2) median_filter(img1) mean_filter(img2) mean_filter(img1) mean_filter(img2)
def homomorphic(img:np.ndarray): img = img + 0.000000001 print(img) img = np.log(img) print(img) img = np.fft.fft(img) img = BHPF(img) img = np.fft.ifft(img) img = np.exp(img) print(img) img = np.array(img, dtype='f') print(img) show(img) return img
def gauss_noise(picture: np.ndarray, SNR: float = 0.75, verbose: bool = True) -> np.ndarray: orig_size = picture.shape arr = picture.flatten() length = arr.shape[0] choice = np.random.randint(0, length / 3, int((length / 3) * (1 - SNR))) for i in choice: x = np.random.random() arr[i * 3] = np.random.normal(arr[i * 3], 20) arr[i * 3 + 1] = np.random.normal(arr[i * 3 + 1], 20) arr[i * 3 + 2] = np.random.normal(arr[i * 3 + 2], 20) picture = arr.reshape(orig_size) if verbose: show(picture) cv2.imwrite(SAVEDPATH + r'\guass_noise.jpg', picture) return picture #sppicture = salt_and_pepper_noise(picture) #gspicture = gauss_noise(picture)
def salt_and_pepper_noise(picture: np.ndarray, SNR: float = 0.75, verbose: bool = True) -> np.ndarray: orig_size = picture.shape arr = picture.flatten() length = arr.shape[0] choice = np.random.randint(0, length / 3, int((length / 3) * (1 - SNR))) for i in choice: x = np.random.random() if x < 0.5: arr[i * 3] = 0 arr[i * 3 + 1] = 0 arr[i * 3 + 2] = 0 else: arr[i * 3] = 255 arr[i * 3 + 1] = 255 arr[i * 3 + 2] = 255 picture = arr.reshape(orig_size) if verbose: show(picture) cv2.imwrite(SAVEDPATH + r'\salt_and_pepper_noise.jpg', picture) return picture
from Script.lab1.noise import gauss_noise def bilateral(img: np.ndarray, sigmad: float = 200, sigmar: float = 200): img = np.array(img, dtype=np.float) g = np.zeros(img.shape, dtype=np.float) img = samepadding(img, np.eye(5)) for i0 in range(0, img.shape[0] - 4): for j0 in range(0, img.shape[1] - 4): i = i0 + 2 j = j0 + 2 sumup = 0 sumdown = 0 for k in range(i0, i0 + 5): for l in range(j0, j0 + 5): d = np.exp(-(((i - k)**2 + (j - l)**2) / (2 * sigmad**2))) r = np.exp(-((img[i][j] - img[k][l])**2) / (2 * sigmar**2)) w = d * r sumdown += w sumup += w * img[k][l] g[i0][j0] = sumup / sumdown g = np.array(g, dtype='u1') return g img = get() show(img) img = gauss_noise(img, SNR=0.95) img1 = bilateral(img) show(img1)
def test_sobel(): img = get() img = sobel(img) show(img) #test_sobel()
import numpy as np import cv2 from configuration import IMAGEPATH from configuration import SAVEDPATH from Script.util.show import show picture = cv2.imread(IMAGEPATH + '\lena.jpg') show(picture) def salt_and_pepper_noise(picture: np.ndarray, SNR: float = 0.75, verbose: bool = True) -> np.ndarray: orig_size = picture.shape arr = picture.flatten() length = arr.shape[0] choice = np.random.randint(0, length / 3, int((length / 3) * (1 - SNR))) for i in choice: x = np.random.random() if x < 0.5: arr[i * 3] = 0 arr[i * 3 + 1] = 0 arr[i * 3 + 2] = 0 else: arr[i * 3] = 255 arr[i * 3 + 1] = 255 arr[i * 3 + 2] = 255 picture = arr.reshape(orig_size) if verbose: show(picture) cv2.imwrite(SAVEDPATH + r'\salt_and_pepper_noise.jpg', picture)
from Script.lab1.getpicture import get from Script.lab1.noise import gauss_noise def BHPF(img:np.ndarray,D0=360,n=14): filter = np.zeros(img.shape) for i in range(0,filter.shape[0]): for j in range(0,filter.shape[1]): filter[i][j] = np.sqrt((i-filter.shape[0]//2)**2 + (j-filter.shape[1]//2)**2) img1 = img + 0.000000001 return img * (1/(1+(D0/filter)**(2*n))) def homomorphic(img:np.ndarray): img = img + 0.000000001 print(img) img = np.log(img) print(img) img = np.fft.fft(img) img = BHPF(img) img = np.fft.ifft(img) img = np.exp(img) print(img) img = np.array(img, dtype='f') print(img) show(img) return img img = get() img = gauss_noise(img) show(img) img = img / 255 homomorphic(img)
ans[i][j - 1] = 255 ans[i][j + 1] = 255 ans[i][j + 2] = 255 return ans @classmethod def open(cls, img): img = cls.erode(img) img = cls.dilate(img) return img @classmethod def close(cls, img): img = cls.dilate(img) img = cls.erode(img) return img img = get() img = morphology.binaryzation(img) # for i in range(0,10): # img = morphology.open(img) # img = morphology.close(img) er = morphology.erode(img) di = morphology.dilate(img) ans = di - er ans = np.abs(ans) show(ans)
def getlisenceplate(car: np.ndarray): bluef = car[..., 0] bluef = np.array(bluef, dtype='f4') anotherf = car[..., 1] anotherf = np.array(anotherf, dtype='f4') anotherf1 = car[..., 2] anotherf1 = np.array(anotherf1, dtype='f4') ansf1 = bluef - anotherf ansf2 = bluef - anotherf1 ansf = (ansf1 + ansf2) // 2 ansf = (np.abs(ansf) + ansf) // 2 print(ansf) ansf = np.array(ansf, dtype='u1') # cv2.imshow('test',ansf) # cv2.waitKey(0) # cv2.destroyAllWindows() #show(ansf) #ret,ansf = cv2.threshold(ansf,80,255,cv2.THRESH_BINARY) ansfx = cv2.Sobel(ansf, -1, 1, 0) ansfy = cv2.Sobel(ansf, -1, 0, 1) ansf = np.array(ansfx + ansfy, dtype='u1') kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) for i in range(0, 9): ansf = cv2.dilate(ansf, kernel) for i in range(0, 9): ansf = cv2.erode(ansf, kernel) for i in range(0, 5): ansf = cv2.erode(ansf, kernel) for i in range(0, 5): ansf = cv2.dilate(ansf, kernel) ret, ansf = cv2.threshold(ansf, 60, 255, cv2.THRESH_BINARY) #show(ansf) # ansfx = cv2.Sobel(ansf, -1, 1, 0) # ansfy = cv2.Sobel(ansf, -1, 0, 1) # ansf = np.array(ansfx + ansfy,dtype='u1') ansf = cv2.morphologyEx(ansf, cv2.MORPH_GRADIENT, kernel) #ansf = cv2.dilate(ansf, kernel) #show(ansf) # lines = cv2.HoughLines(ansf, 0.8, np.pi/180, 40) # board = np.zeros((ansf.shape[0],ansf.shape[1],3)) # for line in lines: # rho, theta = line[0] # a = np.cos(theta) # b = np.sin(theta) # x0 = a * rho # y0 = b * rho # x1 = int(x0 + 1000 * (-b)) # y1 = int(y0 + 1000 * (a)) # x2 = int(x0 - 1000 * (-b)) # y2 = int(y0 - 1000 * (a)) # cv2.line(board, (x1, y1), (x2, y2), (0, 0, 255)) # show(board) ret, ansf = cv2.threshold(ansf, 254, 255, cv2.THRESH_BINARY) #show(ansf) xmin = ansf.shape[1] - 1 xmax = 0 ymin = ansf.shape[0] - 1 ymax = 0 for i in range(0, ansf.shape[0]): for j in range(0, ansf.shape[1]): if ansf[i][j] >= 254: if xmin > j: xmin = j if ymin > i: ymin = i if xmax < j: xmax = j if ymax < i: ymax = i cv2.line(car, (xmin, ymin), (xmax, ymin), (0, 0, 255), 3) cv2.line(car, (xmin, ymax), (xmax, ymax), (0, 0, 255), 3) cv2.line(car, (xmin, ymin), (xmin, ymax), (0, 0, 255), 3) cv2.line(car, (xmax, ymin), (xmax, ymax), (0, 0, 255), 3) show(car) return car
def median_filter(img): img = medianFilter22(img, (3, 3)) show(img) return img
def mean_filter(img): core = 1 / 9 * np.ones((3, 3)) img = conv(img, core) show(img) return img