add 1 and 2
This commit is contained in:
1 parent
8d72ef3dc2
commit
b15e6b19ce
14 files changed
+404
No files matched your search
@@ -0,0 +1,50 @@
|
||||
import numpy as np
|
||||
import cv2 as cv
|
||||
import matplotlib.pyplot as plt
|
||||
from sympy import im
|
||||
|
||||
|
||||
def global_linear_transmation(im, c=0, d=255):
|
||||
img = im.copy()
|
||||
maxV = img.max()
|
||||
minV = img.min()
|
||||
if maxV == minV:
|
||||
return np.uint8(img)
|
||||
for i in range(img.shape[0]):
|
||||
for j in range(img.shape[1]):
|
||||
img[i, j] = ((d - c) / (maxV - minV)) * (img[i, j] - minV) + c
|
||||
return np.uint8(img)
|
||||
|
||||
|
||||
def histogram_equalization(im):
|
||||
return np.uint8(cv.equalizeHist(im))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
im = cv.imread(r"E:\OneDrive\Code\Python\2\iris.jpg", cv.IMREAD_GRAYSCALE)
|
||||
im1 = global_linear_transmation(im, 0, 150)
|
||||
im2 = global_linear_transmation(im, 100)
|
||||
im3 = global_linear_transmation(im, 50, 150)
|
||||
im4 = histogram_equalization(im)
|
||||
plt.figure()
|
||||
plt.subplot(241)
|
||||
plt.imshow(im1, cmap="gray")
|
||||
plt.title("darker")
|
||||
plt.subplot(242)
|
||||
plt.imshow(im2, cmap="gray")
|
||||
plt.title("brighter")
|
||||
plt.subplot(243)
|
||||
plt.imshow(im3, cmap="gray")
|
||||
plt.title("lower contrast")
|
||||
plt.subplot(244)
|
||||
plt.imshow(im4, cmap="gray")
|
||||
plt.title("equalized")
|
||||
plt.subplot(245)
|
||||
plt.hist(im1.flatten(), 256, [0, 256])
|
||||
plt.subplot(246)
|
||||
plt.hist(im2.flatten(), 256, [0, 256])
|
||||
plt.subplot(247)
|
||||
plt.hist(im3.flatten(), 256, [0, 256])
|
||||
plt.subplot(248)
|
||||
plt.hist(im4.flatten(), 256, [0, 256])
|
||||
plt.show()
|
||||
@@ -0,0 +1,26 @@
|
||||
import numpy as np
|
||||
import cv2 as cv
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
|
||||
def gamma_trans(img, gamma=1.0):
|
||||
gamma_table = [np.power(x / 255.0, gamma) * 255.0 for x in range(256)]
|
||||
gamma_table = np.round(np.array(gamma_table)).astype(np.uint8)
|
||||
return cv.LUT(img, gamma_table)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
im = cv.imread(r"E:\OneDrive\Code\Python\2\iris.jpg", cv.IMREAD_GRAYSCALE)
|
||||
im1 = gamma_trans(im, 0.5)
|
||||
im2 = gamma_trans(im, 1.5)
|
||||
plt.figure()
|
||||
plt.subplot(131)
|
||||
plt.imshow(im, cmap="gray")
|
||||
plt.title("original")
|
||||
plt.subplot(132)
|
||||
plt.imshow(im1, cmap="gray")
|
||||
plt.title("gamma = 0.5")
|
||||
plt.subplot(133)
|
||||
plt.imshow(im2, cmap="gray")
|
||||
plt.title("gamma = 1.5")
|
||||
plt.show()
|
||||
@@ -0,0 +1,28 @@
|
||||
import cv2 as cv
|
||||
from matplotlib import pyplot as plt
|
||||
import numpy as np
|
||||
|
||||
img = cv.imread(r"E:\OneDrive\Code\Python\2\iris.jpg", 0)
|
||||
fil1 = 1 / 16 * np.array([[1, 2, 1], [2, 4, 2], [1, 2, 1]])
|
||||
fil2 = 1 / 9 * np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]])
|
||||
fil3 = 1 / 10 * np.array([[1, 1, 1], [1, 2, 1], [1, 1, 1]])
|
||||
fil4 = np.array([[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]])
|
||||
ImgSmoothed1 = cv.filter2D(img, -1, fil1, borderType=cv.BORDER_DEFAULT)
|
||||
ImgSmoothed2 = cv.filter2D(img, -1, fil2, borderType=cv.BORDER_DEFAULT)
|
||||
ImgSmoothed3 = cv.filter2D(img, -1, fil3, borderType=cv.BORDER_DEFAULT)
|
||||
ImgSharp = cv.filter2D(img, -1, fil4, borderType=cv.BORDER_DEFAULT)
|
||||
|
||||
plt.figure()
|
||||
plt.subplot(221)
|
||||
plt.imshow(ImgSmoothed1, cmap="gray")
|
||||
plt.title("smoothed1")
|
||||
plt.subplot(222)
|
||||
plt.imshow(ImgSmoothed2, cmap="gray")
|
||||
plt.title("smoothed2")
|
||||
plt.subplot(223)
|
||||
plt.imshow(ImgSmoothed3, cmap="gray")
|
||||
plt.title("smoothed3")
|
||||
plt.subplot(224)
|
||||
plt.imshow(ImgSharp, cmap="gray")
|
||||
plt.title("sharp")
|
||||
plt.show()
|
||||
@@ -0,0 +1,70 @@
|
||||
import random as rd
|
||||
import numpy as np
|
||||
import cv2 as cv
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
|
||||
def addSaltAndPepper(src, percentage):
|
||||
NoiseImg = src.copy()
|
||||
NoiseNum = int(percentage * src.shape[0] * src.shape[1])
|
||||
for i in range(NoiseNum):
|
||||
randX = rd.randint(0, src.shape[0] - 1)
|
||||
randY = rd.randint(0, src.shape[1] - 1)
|
||||
if rd.randint(0, 1) == 0:
|
||||
NoiseImg[randX, randY] = 0
|
||||
else:
|
||||
NoiseImg[randX, randY] = 255
|
||||
return NoiseImg
|
||||
|
||||
|
||||
def addGaussianNoise(src, means, sigma):
|
||||
NoiseImg = src / src.max()
|
||||
rows = NoiseImg.shape[0]
|
||||
cols = NoiseImg.shape[1]
|
||||
for i in range(rows):
|
||||
for j in range(cols):
|
||||
NoiseImg[i, j] = NoiseImg[i, j] + rd.gauss(means, sigma)
|
||||
if NoiseImg[i, j] < 0:
|
||||
NoiseImg[i, j] = 0
|
||||
if NoiseImg[i, j] > 1:
|
||||
NoiseImg[i, j] = 1
|
||||
NoiseImg = np.uint8(NoiseImg * 255)
|
||||
return NoiseImg
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
im = cv.imread(r"E:\OneDrive\Code\Python\2\iris.jpg", cv.IMREAD_GRAYSCALE)
|
||||
im1 = addSaltAndPepper(im, 0.1)
|
||||
im11 = cv.blur(im1, (3, 3))
|
||||
im12 = cv.medianBlur(im1, 3)
|
||||
im13 = cv.GaussianBlur(im1, (3, 3), 1)
|
||||
im2 = addGaussianNoise(im, 0, 0.1)
|
||||
im21 = cv.blur(im2, (3, 3))
|
||||
im22 = cv.medianBlur(im2, 3)
|
||||
im23 = cv.GaussianBlur(im2, (3, 3), 1)
|
||||
plt.figure()
|
||||
plt.subplot(241)
|
||||
plt.imshow(im1, cmap="gray")
|
||||
plt.title("salt and pepper")
|
||||
plt.subplot(242)
|
||||
plt.imshow(im11, cmap="gray")
|
||||
plt.title("blur")
|
||||
plt.subplot(243)
|
||||
plt.imshow(im12, cmap="gray")
|
||||
plt.title("median")
|
||||
plt.subplot(244)
|
||||
plt.imshow(im13, cmap="gray")
|
||||
plt.title("gaussian")
|
||||
plt.subplot(245)
|
||||
plt.imshow(im2, cmap="gray")
|
||||
plt.title("gaussian noise")
|
||||
plt.subplot(246)
|
||||
plt.imshow(im21, cmap="gray")
|
||||
plt.title("blur")
|
||||
plt.subplot(247)
|
||||
plt.imshow(im22, cmap="gray")
|
||||
plt.title("median")
|
||||
plt.subplot(248)
|
||||
plt.imshow(im23, cmap="gray")
|
||||
plt.title("gaussian")
|
||||
plt.show()
|
||||
@@ -0,0 +1,21 @@
|
||||
import cv2 as cv
|
||||
from matplotlib import pyplot as plt
|
||||
import numpy as np
|
||||
|
||||
img = cv.imread(r"E:\OneDrive\Code\Python\2\iris.jpg", 0)
|
||||
lplc = np.array([[0, -1, 0], [-1, 4, -1], [0, -1, 0]])
|
||||
lplcEnhance = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
|
||||
ImgLplc = cv.filter2D(img, -1, lplc, borderType=cv.BORDER_DEFAULT)
|
||||
ImgLplcEnhance = cv.filter2D(img, -1, lplcEnhance, borderType=cv.BORDER_DEFAULT)
|
||||
|
||||
plt.figure()
|
||||
plt.subplot(131)
|
||||
plt.imshow(img, cmap="gray")
|
||||
plt.title("original")
|
||||
plt.subplot(132)
|
||||
plt.imshow(ImgLplc, cmap="gray")
|
||||
plt.title("laplacian")
|
||||
plt.subplot(133)
|
||||
plt.imshow(ImgLplcEnhance, cmap="gray")
|
||||
plt.title("laplacian enhanced")
|
||||
plt.show()
|
||||
BIN
Binary file not shown.
Reference in new issue
Block a user