ich will über ein gesamtes Bildes jedes Pixel mit einem Schwellwert vergleichen. Nachdem das ganze Bild durch gegangen wurde und darüber eine Maske und weitere Berechnungen durchgeführt wurden,soll das ganze wieder von vorne mit einem anderen Schwellwert beginnen. Der Schwellwert ist ein einfacher 1D-Array dessen Größe und Inhalt bekannt sind allerdings geringer als die des Bildes.
Aktuell erhalte ich immer folgende Fehler:
TypeError: only integer scalar arrays can be converted to a scalar index oder IndexError: index 66 is out of bounds for axis 0 with size 65
Muss ich das mit der äußersten for schleife anders lösen ? Kann ich die iterationsvariablen i und j irgendwie zurücksetzen?
[optional]
Kann ich die ganzen Schleifen auch durch sogenannte "Universal Functions" ersetzen ?
https://jakevdp.github.io/PythonDataSci ... funcs.html
Mein Code:
Code: Alles auswählen
import cv2
import time
import numpy
import numpy as np
from matplotlib import pyplot as plt
import json
start_time= time.time()
# Load image as BGR
img_bgr = cv2.imread(r'bilder/test-patches/cloudy_sun_DARK_SKY.jpg', cv2.IMREAD_COLOR)
img_gray = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY)
# Split BGR Picture in each chanel
(b, g, r) = cv2.split(img_bgr)
# get width and height
height, width = img_bgr.shape[:2]
#create binary mask
binary_mask = np.empty([height, width])
# Cast Array to int-values for bigger number space, cause cv.split has limit to 255 (8bit-colorspace)
b_value, g_value, r_value = b.astype(int), g.astype(int), r.astype(int)
# Calculate Sum ∑ of R,G,B Channels
sum_rgb = b_value + g_value + r_value
# Compute pixel composition delta
delta = 4 * (r - g) ** 2 + (r - b) ** 2 + 4 * (g - b) ** 2
# Blur image for better initial threshold
img_blur = cv2.medianBlur(img_bgr,5)
threshold,image_result = cv2.threshold(img_gray, 0, 255, cv2.THRESH_OTSU)
# Lowered Otsu Threshold to ensure low initial value but save some iterations for improving calculation time
initial_threshold = int(threshold/3)
# Limiting condition
limit_condition1 = 760
# Create iterable threshold array
threshold_iterations = np.arange(initial_threshold,224,3,dtype= int )
for k in np.nditer(threshold_iterations):
for i in range(height):
for j in range(width):
if delta[i][j] >= threshold_iterations[k]:
if sum_rgb[i][j] > limit_condition1:
binary_mask[i][j] = 0
else:
binary_mask[i][j] =1
elif delta[i][j] < threshold_iterations[k]:
if (b_value[i][j] > r_value[i][j]) and (b_value[i][j] > g_value[i][j]):
binary_mask[i][j] = 0
else:
binary_mask[i][j] = 1
print("--- %s seconds ---" % (time.time() - start_time))