Ich studiere mit einem Buch namens Digital Image Processing. Ich habe implementiert (Kontrastumwandlung), um das Verständnis zu vertiefen.
Der Kontrast ist der Grad der Änderung von Helligkeit und Farbe. (Persönliche Interpretation) Starker Kontrast → Leicht verständliche Helligkeit und Farbübertragung, leicht verständliche Umrisse eines Objekts. Schwacher Kontrast → Es ist schwierig, die Helligkeit und die Farbübertragung zu verstehen, und es ist schwierig, die Umrisse eines Objekts zu verstehen.
Durch Ändern der Beziehung zwischen Eingabe und Ausgabe kann der Farbton des Bildes geändert werden. Daher wird es leicht zu sehen oder schwer zu sehen.
Verwenden Sie ein dunkles Beispiel wie das im Bild unten, um den Effekt besser erkennen zu können.
python
#Erforderliche Bibliotheken
import cv2
import numpy as np
import matplotlib.pyplot as plt
#Fotopfad
pic_path = 'pet-bottle-pic/test/camera_pic/aquari3.jpg'
#Lesen
pic_image = cv2.imread(pic_path)
#In grau konvertieren
pic_image = cv2.cvtColor(pic_image, cv2.COLOR_BGR2GRAY)
#Beim Lesen mit Lebenslauf, plt.cmap bei Verwendung von imshow='gray'Beachten Sie, dass es nur grau ist, wenn Sie dies angeben
plt.imshow(pic_image, cmap='gray')
plt.title('sample_dark')
plt.savefig("aquari_dark.jpg ")
python
#Pixel
pic_image.shape # (2464, 3280)
python
#In einer Dimension abflachen, um ein Histogramm zu erstellen
flat_data = np.ravel(pic_image)
#Balkenspanne
bins_range = range(0, 260, 5)
#Histogramm ausgeben
plt.hist(flat_data, bins = bins_range)
plt.title('sample_dark')
plt.xlabel('pixel_value')
plt.ylabel('frequency')
plt.savefig("hist_aquari_dark.png ")
Je nachdem, wie es hergestellt wird, kann der Kontrast in jedem Bereich erhöht werden. (Obwohl es ein wenig unnatürlich ist) Da das Beispielbild im Bereich der Pixelwerte 0 bis 50 dicht ist, wird der Winkel in diesem Bereich eingestellt.
python
def bend_line(X):
y = X*5
y = np.where(y>=255, 255, y)
return y
python
X = np.arange(0, 255, 1)
y = bend_line(X)
plt.title('sample_dark_bend_line')
plt.xlabel('INPUT')
plt.ylabel('OUTPUT')
plt.plot(X, y)
plt.savefig("bend_line.png ")
python
pic_func1 = bend_line(pic_image)
plt.imshow(pic_func1, cmap='gray')
plt.title('sample_dark_after_bend_line')
plt.savefig("aquari_dark_after_bend_line.jpg ")
python
flat_data = np.ravel(pic_func1)
bins_range = range(0, 260, 5)
#Histogramm ausgeben
plt.hist(flat_data, bins = bins_range)
plt.title('sample_dark_after_bend_line')
plt.xlabel('pixel_value')
plt.ylabel('frequency')
plt.savefig("hist_aquari_dark_after_bend_line.png ")
Geeignet zum Korrigieren von Bildern, die zu dunkel oder zu hell sind, um sie zu verstehen. Geeignet zur Erhöhung des Kontrastes von Bildern mit einer Verteilung in der Nähe der Ränder.
Implementieren Sie die folgende Formel, die als Gammafunktion bezeichnet wird.
python
def g_function(X, gamma):
y = 255*(X/255)**(1/gamma)
return y
python
X = np.arange(0, 255, 1)
gamma = [3, 2, 1.5, 1, 0.5, 0.33]
labels = ["3", "2", "1.5", "1", "0.5", "0.33"]
plt.title('g_function')
plt.xlabel('INPUT')
plt.ylabel('OUTPUT')
for g, l in zip(gamma, labels):
y = g_function(X, g)
plt.plot(X, y, label = l)
plt.legend()
plt.savefig("g_function.png ")
γ ist groß → Verstärkt den Kontrast dunkler Bilder insgesamt. γ ist klein → Erhöht den Kontrast heller Bilder insgesamt.
python
gamma = 3
pic_gfunc = g_function(pic_image, gamma)
plt.imshow(pic_gfunc, cmap='gray')
plt.title('sample_dark_after_gfunc')
plt.savefig("aquari_dark_after_gfunc.jpg ")
python
flat_data = np.ravel(pic_gfunc)
bins_range = range(0, 260, 5)
#Histogramm ausgeben
plt.hist(flat_data, bins = bins_range)
plt.title('sample_dark_after_gfunc')
plt.xlabel('pixel_value')
plt.ylabel('frequency')
plt.savefig("hist_aquari_dark_after_gfunc.png ")
Eigenschaften: Normal
python
#Fotopfad
pic_path = 'pet-bottle-pic/test/camera_pic/aquari.jpg'
#Lesen
pic_image = cv2.imread(pic_path)
#In grau konvertieren
pic_image = cv2.cvtColor(pic_image, cv2.COLOR_BGR2GRAY)
#Beim Lesen mit Lebenslauf, plt.cmap bei Verwendung von imshow='gray'Beachten Sie, dass es nur grau ist, wenn Sie dies angeben
plt.imshow(pic_image, cmap='gray')
plt.title('sample_normal')
plt.savefig("aquari.jpg ")
python
#In einer Dimension abflachen, um ein Histogramm zu erstellen
flat_data = np.ravel(pic_image)
#Balkenspanne
bins_range = range(0, 260, 5)
#Histogramm ausgeben
plt.hist(flat_data, bins = bins_range)
plt.title('sample_normal')
plt.xlabel('pixel_value')
plt.ylabel('frequency')
plt.savefig("hist_aquari.png ")
Es ist geeignet, um den Kontrast von Bildern mit einer Verteilung nahe der Mitte zu erhöhen.
Implementieren Sie die folgende Formel
python
def s_func(X, a):
y = 255/(1 + np.exp(a*(-1*X + 127)))
return y
python
X = np.arange(0, 255, 1)
a = 0.05
y = s_func(X, a)
plt.title('s_function')
plt.xlabel('INPUT')
plt.ylabel('OUTPUT')
plt.plot(X, y)
plt.savefig("s_function.png ")
python
a = 0.05
y = s_func(pic_image, a)
plt.imshow(y, cmap='gray')
plt.title('sample_after_sfunc')
plt.savefig("aquari_after_sfunc.jpg ")
python
flat_data = np.ravel(y)
bins_range = range(0, 260, 5)
#Histogramm ausgeben
plt.hist(flat_data, bins = bins_range)
plt.title('sample_after_sfunc')
plt.xlabel('pixel_value')
plt.ylabel('frequency')
plt.savefig("hist_aquari_sfunc.png ")
Die Verwendung ist mysteriös, aber ich habe sie implementiert.
Hell → dunkel Dunkel → hell werden.
python
def rev_func(X):
y = -X + 255
return y
python
X = np.arange(0, 255, 1)
y = rev_func(X)
plt.title('Rev_function')
plt.xlabel('INPUT')
plt.ylabel('OUTPUT')
plt.plot(X, y)
plt.savefig("rev_func.png ")
python
y = rev_func(pic_image)
plt.imshow(y, cmap='gray')
plt.title('sample_after_rev_func')
plt.savefig("aquari_after_rev_func.jpg ")
python
flat_data = np.ravel(y)
bins_range = range(0, 260, 5)
#Histogramm ausgeben
plt.hist(flat_data, bins = bins_range)
plt.title('sample_after_rev_func')
plt.xlabel('pixel_value')
plt.ylabel('frequency')
plt.savefig("hist_aquari_rev_func.png ")
Es sieht aus wie ein Poster.
Intervall: 64 Erste Stufe: 64 Zweite Stufe: 128 Dritte Stufe: 192 Vierte Stufe: 255
python
def post_func(X):
y = np.where(X>=192, 255, X)
y = np.where((y >=128 ) & (y < 192), 170, y)
y = np.where((y >=64 ) & (y < 128), 85, y)
y = np.where(y<64, 0, y)
return y
python
X = np.arange(0, 255, 1)
y = post_func(X)
plt.title('post_function')
plt.xlabel('INPUT')
plt.ylabel('OUTPUT')
plt.plot(X, y)
plt.savefig("post_func.png ")
python
y = post_func(pic_image)
plt.imshow(y, cmap='gray')
plt.title('sample_after_after_postfunc')
plt.savefig("aquari_after_postfunc.jpg ")
python
flat_data = np.ravel(y)
bins_range = range(0, 260, 5)
#Histogramm ausgeben
plt.hist(flat_data, bins = bins_range)
plt.title('sample_after_postfunc')
plt.xlabel('pixel_value')
plt.ylabel('frequency')
plt.savefig("hist_aquari_postfunc.png ")
Schwarz und Weiß klar]
python
def to_func(X):
y = np.where(X>=127, 255, X)
y = np.where(y<127, 0, y)
return y
python
X = np.arange(0, 255, 1)
y = to_func(X)
plt.title('2_function')
plt.xlabel('INPUT')
plt.ylabel('OUTPUT')
plt.plot(X, y)
plt.savefig("2_func.png ")
python
y = to_func(pic_image)
plt.imshow(y, cmap='gray')
plt.title('sample_after_after_tofunc')
plt.savefig("aquari_after_tofunc.jpg ")
python
flat_data = np.ravel(y)
bins_range = range(0, 260, 5)
#Histogramm ausgeben
plt.hist(flat_data, bins = bins_range)
plt.title('sample_after_totfunc')
plt.xlabel('pixel_value')
plt.ylabel('frequency')
plt.savefig("hist_aquari_tofunc.png ")
Es fühlt sich an wie ein Film, wenn man ein Foto entwickelt.
python
from math import pi
def sora_func(X):
X = (3*pi/255)*X
y = -127.5*(np.cos(X))+127.5
return y
python
X = np.arange(0, 255, 1)
y = sora_func(X)
plt.title('sora_function')
plt.xlabel('INPUT')
plt.ylabel('OUTPUT')
plt.plot(X, y)
plt.savefig("sora_func.png ")
python
y = sora_func(pic_image)
plt.imshow(y, cmap='gray')
plt.title('sample_after_sorafunc')
plt.savefig("aquari_after_sorafunc.jpg ")
python
flat_data = np.ravel(y)
bins_range = range(0, 260, 5)
#Histogramm ausgeben
plt.hist(flat_data, bins = bins_range)
plt.title('sample_after_soratfunc')
plt.xlabel('pixel_value')
plt.ylabel('frequency')
plt.savefig("hist_aquari_sorafunc.png ")
Ende
Recommended Posts