Home > Enterprise >  iterating through two image arrays to create merged image
iterating through two image arrays to create merged image

Time:03-29

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.

input 128x128

output 128x128

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.

  • Related