<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 This notebook shows how a cloud camera could be calibrated with the pyclamster and sun positions.
Step1:  Load pickled coordinates for the first Hungriger Wolf camera
Step2:  Set the paramters for the image clustering
Step3:  Load image and preprocess it
Step4:  Predict the labels with the trained model and convert it into a mask store
Step5:  Denoise the cloud mask and label the clouds
Step6:  Load the second image and cutted
Step7:  Move the cloud around to find the best matching point
Step8:  Example for SpatialCloud
<ASSISTANT_TASK:>
Python Code:
%matplotlib inline
import os
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
import logging
import pyclamster
import pickle
import scipy
import scipy.misc
from skimage.feature import match_template
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logging.debug("test")
Explanation: This notebook shows how a cloud camera could be calibrated with the pyclamster and sun positions.
End of explanation
filename = "../examples/calibration/wolf-3-calibration.pk"
calibration = pickle.load(open(filename, 'rb'))
cal_coords = calibration.create_coordinates()
cal_coords.z = 2000
plt.subplot(221)
plt.title("elevation on the image [deg]")
plt.imshow(cal_coords.elevation*360/(2*np.pi))
plt.colorbar()
plt.subplot(222)
plt.title("azimuth on the image [deg]")
plt.imshow(cal_coords.azimuth*360/(2*np.pi))
plt.colorbar()
plt.subplot(223)
plt.title("[z=2000 plane]\nreal-world x on the image [m]")
plt.imshow(cal_coords.x)
plt.colorbar()
plt.subplot(224)
plt.title("[z=2000 plane]\nreal-world y on the image [m]")
plt.imshow(cal_coords.y)
plt.colorbar()
plt.tight_layout()
Explanation: Load pickled coordinates for the first Hungriger Wolf camera
End of explanation
base_folder = "../"
image_directory = os.path.join(base_folder, "examples", "images", "wolf")
trained_models = os.path.join(base_folder, "trained_models")
good_angle = 45
center = int(1920/2)
good_angle_dpi = int(np.round(1920 / 180 * good_angle))
denoising_ratio = 10
#all_images = glob.glob(os.path.join(image_directory, "Image_20160531_114000_UTCp1_*.jpg"))
#print(all_images)
all_images = [
    os.path.join(image_directory, "Image_20160531_114100_UTCp1_3.jpg"),
    os.path.join(image_directory, "Image_20160531_114100_UTCp1_4.jpg")]
kmeans = pickle.load(open(os.path.join(trained_models, "kmeans.pk"), "rb"))
Explanation: Set the paramters for the image clustering
End of explanation
image = pyclamster.Image(all_images[0])
image.coordinates = cal_coords
cutted_image = image.cut([960, 960, 1460, 1460])
plt.title("The raw cutted image")
plt.imshow(cutted_image)
plt.axis('off')
image.data = pyclamster.clustering.preprocess.LCN(size=(25,25,3), scale=False).fit_transform(image.data)
image = image.cut([960, 960, 1460, 1460])
w, h, _ = original_shape = image.data.shape
raw_image = pyclamster.clustering.functions.rbDetection(image.data).reshape((w*h, -1))
Explanation: Load image and preprocess it
End of explanation
label = kmeans.predict(raw_image)
label.reshape((w, h), replace=True)
plt.title("The masked clouds")
plt.imshow(label.labels, cmap='gray')
plt.axis('off')
masks = label.getMaskStore()
Explanation: Predict the labels with the trained model and convert it into a mask store
End of explanation
masks.denoise([0], 1000)
cloud_labels, _ = masks.labelMask([0,])
plt.title("The labeled clouds")
plt.imshow(cloud_labels.labels, cmap='gray')
plt.axis('off')
cloud_store = cloud_labels.getMaskStore()
clouds = [cloud_store.getCloud(cutted_image, [k,]) for k in cloud_store.masks.keys()]
cloud1 = cloud_store.cutMask(cutted_image, [1,])
print(cloud1.data.shape)
Explanation: Denoise the cloud mask and label the clouds
End of explanation
image = pyclamster.Image(all_images[1])
image = image.cut([850, 850, 1460, 1460])
plt.title("The raw cutted image")
plt.imshow(image)
plt.axis('off')
Explanation: Load the second image and cutted
End of explanation
result = match_template(image.data, cloud1.data, pad_input=True, mode='reflect', constant_values=0)
plt.title("The matching result")
plt.imshow(result, cmap='gray')
plt.colorbar()
plt.axis('off')
#print(np.unravel_index(np.argmax(result), result.shape))
Explanation: Move the cloud around to find the best matching point
End of explanation
image = pyclamster.Image(all_images[1])
image.coordinates = cal_coords            # Fake the second coordiante
image.crop([931, 981, 1430, 1480])
cloud2 = pyclamster.matching.Cloud(image)
sCloud = pyclamster.matching.SpatialCloud(pyclamster.matching.Cloud(cloud1), cloud2)
position = sCloud._calc_position()
print(position[2])
plt.title("A faked height map")
plt.imshow(position[2], cmap='gray')
plt.colorbar()
plt.axis('off')
Explanation: Example for SpatialCloud
End of explanation
<END_TASK>