Reputation: 1369
Given these two arrays:
a = np.array(
[
[
[1, 102, 103, 255],
[201, 2, 202, 255],
[201, 202, 202, 255]
],
[
[11, 120, 0, 255],
[0, 0, 0, 255],
[1, 22, 142, 255]
],
])
b = np.array(
[
[
[1, 102, 103, 255],
[201, 2, 202, 255]
],
[
[11, 120, 0, 255],
[221, 222, 13, 255]
],
[
[91, 52, 53, 255],
[0, 0, 0, 255]
],
])
a.shape # => (2, 3, 4)
b.shape # => (3, 3, 4)
I want to overlay a
and b
at 0, 0
and output a mask that represents when a
values equal b
values. The values compared are the full pixel values, so in this case [1, 102, 103, 255]
is a value.
An output mask like this would be great:
result = np.array([
[
true,
true,
false
],
[
true,
false,
false
],
[
false,
false,
false
],
])
A perfect answer in my case would be where matching values become [255, 0, 0, 255]
and mismatching values become [0, 0, 0, 0]
:
result = np.array([
[
[255, 0, 0, 255],
[255, 0, 0, 255],
[0, 0, 0, 0]
],
[
[255, 0, 0, 255],
[0, 0, 0, 0],
[0, 0, 0, 0]
],
[
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
],
])
result.shape # => (3, 3, 4)
It looks like this:
[![diff between a and b][1]][1]
Upvotes: 3
Views: 487
Reputation: 53029
Here is one possibility using slicing.
outer = np.maximum(a.shape, b.shape)
inner = *map(slice, np.minimum(a.shape, b.shape)),
out = np.zeros(outer, np.result_type(a, b))
out[inner][(a[inner]==b[inner]).all(2)] = 255,0,0,255
out
# array([[[255, 0, 0, 255],
# [255, 0, 0, 255],
# [ 0, 0, 0, 0]],
#
# [[255, 0, 0, 255],
# [ 0, 0, 0, 0],
# [ 0, 0, 0, 0]],
#
# [[ 0, 0, 0, 0],
# [ 0, 0, 0, 0],
# [ 0, 0, 0, 0]]])
Upvotes: 3