Spaces:
Runtime error
Runtime error
File size: 5,126 Bytes
0f02cf8 9285af4 0f02cf8 9285af4 0f02cf8 7ac80ce 0f02cf8 9285af4 0f02cf8 bbcdd70 0f02cf8 bbcdd70 0f02cf8 9285af4 0f02cf8 9285af4 0f02cf8 9285af4 7ac80ce 9285af4 0f02cf8 | 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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | import cv2 as cv
import numpy as np
from skimage import exposure, morphology
from skimage.filters import threshold_mean, threshold_otsu
from scipy import signal
import matplotlib.pyplot as plt
def show_histogram(image):
_, axes = plt.subplots(1, 2)
if image.ndim == 2:
hist = exposure.histogram(image)
axes[0].imshow(image, cmap=plt.get_cmap("gray"))
axes[0].set_title("Image")
axes[1].plot(hist[0])
axes[1].set_title("Histogram")
else:
axes[0].imshow(image)
axes[0].set_title("Image")
axes[1].set_title("Histogram")
colors = ["red", "green", "blue"]
for i, color in enumerate(colors):
axes[1].plot(exposure.histogram(image[..., i])[0], color=color)
plt.show()
def return_histogram_path(image):
if image.ndim == 2:
hist = exposure.histogram(image)
plt.plot(hist[0])
plt.xlabel("Pixel Intensity")
plt.ylabel("Frequency")
plt.title("Histogram")
else:
plt.title("Histogram")
colors = ["red", "green", "blue"]
for i, color in enumerate(colors):
plt.plot(exposure.histogram(image[..., i])[0], color=color)
plt.savefig("histogram.png")
plt.close()
return "histogram.png"
def mean_treshold(image):
if image.ndim == 3:
image = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
thresh = threshold_mean(image)
binary = (image > thresh) * 225
return binary.astype("uint8")
def otsu_treshold(image):
if image.ndim == 3:
image = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
thresh = threshold_otsu(image)
binary = (image > thresh) * 255
return binary.astype("uint8")
def dilatation(image):
bin_img = binarize(image) / 255
dilation = (morphology.binary_dilation(image=bin_img)) * 255
return dilation.astype("uint8")
def erosion(image):
bin_img = binarize(image) / 255
erosion = (morphology.erosion(image=bin_img)) * 255
return erosion.astype("uint8")
def laplacian(image):
gray_image = gray_scale(image)
lap = cv.Laplacian(gray_image, cv.CV_64F)
lap = np.absolute(lap)
return lap.astype("uint8")
def resize(image: np.ndarray, scale: float = 1.0) -> np.ndarray:
"""Fonction effectuant le changement d'échelle de l'image `image` selon le facteur `scale`
en utilisant l'interpolation linéaire.
Paramètre(s) d'entrée
image : ndarray
Image (niveaux de gris) d'un type reconnu par Python.
scale : float
Paramètre de changement d'échelle. Un nombre réel strictement positif
Paramètre(s) de sortie
----------------------
im_resized : ndarray
Image interpolé àa la nouvelle échelle, de même type que `image`
"""
new_height = int(image.shape[1] * scale)
new_width = int(image.shape[0] * scale)
new_shape = (new_height, new_width)
inter = cv.INTER_AREA if scale <= 1 else cv.INTER_LANCZOS4
im_resized = cv.resize(image, new_shape, interpolation=inter)
return im_resized.astype("uint8")
def negative(image) -> np.ndarray:
neg_image = 255 - image
return neg_image.astype("uint8")
def gray_scale(image):
if image.ndim == 3:
return cv.cvtColor(image, cv.COLOR_RGB2GRAY).astype("uint8")
return image
def binarize(image) -> np.ndarray:
gray_img = gray_scale(image)
bin_image = np.where(gray_img > 128, 255, 0)
return bin_image.astype("uint8")
def log_trans(image) -> np.ndarray:
image = image.astype(float)
c = 255 / np.log(1 + np.max(image))
log_img = c * np.log(1 + image)
log_img = np.clip(log_img, 0, 255)
return log_img.astype("uint8")
def exp_trans(image) -> np.ndarray:
image = image.astype(np.float32)
normalized_img = image / 255.0
exp_img = np.exp(normalized_img) - 1
exp_img = 255 * exp_img / np.max(exp_img)
return exp_img.astype("uint8")
def gamma_trans(image, gamma):
gamma *= 5
gamma = 5 - gamma
gamma_img = image / 255
gamma_img = np.power(gamma_img, gamma) * 225
return gamma_img.astype("uint8")
def equalize(image):
return exposure.equalize_hist(image).astype("uint8")
def gaussian_blur(image):
return cv.GaussianBlur(image, ksize=(5, 5), sigmaX=1.0).astype("uint8")
def rotate(img, angle):
(height, width) = img.shape[:2]
rotPoint = (width // 2, height // 2)
rotMat = cv.getRotationMatrix2D(rotPoint, angle, 1.0)
dimensions = (width, height)
return cv.warpAffine(img, rotMat, dimensions)
def find_contours(image):
thresh = binarize(image)
contours, _ = cv.findContours(thresh, cv.RETR_LIST, cv.CHAIN_APPROX_NONE)
blank = np.zeros(image.shape, dtype="uint8")
contour_img = cv.drawContours(blank, contours, -1, (255, 255, 255), 2)
return contour_img
def find_edges(image):
gray_image = gray_scale(image)
return cv.Canny(gray_image, 125, 175).astype("uint8")
if __name__ == "__main__":
image = cv.imread("profile.jpeg")
# show_histogram(image)
cv.imshow("normal", image)
cv.imshow(
"Gamma",
laplacian(image),
)
cv.waitKey(0)
|