I have two 128x128 black and white images, black background and white image in centre of image. One is an input into another function and the output is the output of this function. I need to take the bottom right 64x64 pixels of the output image and compare them to the bottom right 64x64 of the input image array. Where a pixel is white in the output I need the corresponding pixel in the input to be made white also. I have tried just using numpy slicing to cut and paste the 64x64 part of the output over the input but occasionally the input is larger than the output so this is not desirable. I've tried looping through a single image with the following type of looping code:
for (i,row) in enumerate(image_mesh):
for (j,pixel) in enumerate(row):
print(pixel)
But am stuck on how to loop through two arrays simultaneously and compare individual pixels. Sample images attached.
CodePudding user response:
Assuming your images are RGB and you are trying to compare black (0, 0, 0)
against white (1, 1, 1)
You can simply compare them
comparison_ab = np.allclose(img_a[64:, 64:, :], img_b[64:, 64:, :]) # 64x64 bool
and create an RGB image out of it using broadcasting
comparison_ab * np.array([1, 1, 1])[None, None, :] # 64x64x3 float
or take the elementwise minimum
np.minimum(img_a, img_b)
CodePudding user response:
Couldn't get Nil's answer to work but resolved it using:
#convert input files to bool
img_a=input > 200
img_b=output[:,:,0] > 200
comparison = img_b>img_a
#convert from bool to uint8
comparison=comparison.astype('uint8')*255
Converting to a bool array was helpful, a direct greater than operation separated out the differences nicely. I was able to paste this in with a slicing operation easily.