利用tf.math.logical_and()函数实现
给定参考变化图gt和算法生成的变化图cm生成融合图
def confusion_map(self, target_change_map, change_map):
"""
Compute RGB confusion map for the change map.
True positive - White [1,1,1]
True negative - Black [0,0,0]
False positive - Green [0,1,0]
False negative - Red [1,0,0]
"""
conf_map = tf.concat(
[
target_change_map,
change_map,
tf.math.logical_and(target_change_map, change_map),
],
axis=-1,
name="confusion map",
)
return tf.cast(conf_map, tf.float32)
if __name__ == "__main__":
“”“
shape of gt or cm: [H, W, 1]
”“”
a = confusion_map(gt, cm)
OTSU阈值分割方法(Python版)
差异图通过otsu转变化图
import tensorflow as tf
import matplotlib.pyplot as plt
from PIL import Image
def threshold_otsu(image):
"""Return threshold value based on Otsu's method. Adapted to tf from sklearn
Parameters
----------
image : (N, M) ndarray
Grayscale input image.
nbins : int, optional
Number of bins used to calculate histogram. This value is ignored for
integer arrays.
Returns
-------
threshold : float
Upper threshold value. All pixels with an intensity higher than
this value are assumed to be foreground.
Raises
------
ValueError
If ``image`` only contains a single grayscale value.
References
----------
.. [1] Wikipedia, https://en.wikipedia.org/wiki/Otsu's_Method
Examples
--------
>>> from skimage.data import camera
>>> image = camera()
>>> thresh = threshold_otsu(image)
>>> binary = image <= thresh
Notes
-----
The input image must be grayscale.
"""
if len(image.shape) > 2 and image.shape[-1] in (3, 4):
msg = (
"threshold_otsu is expected to work correctly only for "
"grayscale images; image shape {0} looks like an RGB image"
)
warn(msg.format(image.shape))
# Check if the image is multi-colored or not
tf.debugging.assert_none_equal(
tf.math.reduce_min(image),
tf.math.reduce_max(image),
summarize=1,
message="expects more than one image value",
)
hist = tf.histogram_fixed_width(image, tf.constant([0, 255]), 256)
hist = tf.cast(hist, tf.float32)
bin_centers = tf.range(0.5, 256, dtype=tf.float32)
# class probabilities for all possible thresholds
weight1 = tf.cumsum(hist)
weight2 = tf.cumsum(hist, reverse=True)
# class means for all possible thresholds
mean = tf.math.multiply(hist, bin_centers)
mean1 = tf.math.divide(tf.cumsum(mean), weight1)
# mean2 = (tf.cumsum((hist * bin_centers)[::-1]) / weight2[::-1])[::-1]
mean2 = tf.math.divide(tf.cumsum(mean, reverse=True), weight2)
# Clip ends to align class 1 and class 2 variables:
# The last value of ``weight1``/``mean1`` should pair with zero values in
# ``weight2``/``mean2``, which do not exist.
tmp1 = tf.math.multiply(weight1[:-1], weight2[1:])
tmp2 = (mean1[:-1] - mean2[1:]) ** 2
variance12 = tf.math.multiply(tmp1, tmp2)
idx = tf.math.argmax(variance12)
threshold = bin_centers[:-1][idx]
return threshold
if __name__ == "__main__":
di = plt.imread("DI.bmp")
di = tf.convert_to_tensor(di, dtype=float)
di = di/255
tmp = tf.cast(di * 255, tf.int32)
threshold = threshold_otsu(tmp) / 255
change_map = di >= threshold
change_map = change_map.numpy() * 255
img = Image.fromarray(change_map.astype("uint8"))
img = img.convert("L")
img.save('CM.bmp')
对图片进行缩放(Matlab版)
缩放为原来的1/4倍
a = a(1:4:end, 1:4:end, :);



