Home > Blockchain >  Find the percent overlap between two different images?
Find the percent overlap between two different images?

Time:07-24

I have 2 different images, one image is a red channel and another image is a green channel. I finally have a merged imaged which shows both red and green channels. I have attached all 3 images. Essentially, I want to find the percent overlap (how much of the green image is being overlapped with the red image. By this, the pixel overlap between red and green).

I was wondering what type of python package I could use or any other helpful advice that I can do this.

Overlay:

overlay

Green:

green

Red:

red

CodePudding user response:

Here's how you can find the overlap regions of an image:

import os
import cv2
import matplotlib.pyplot as plt
green = cv2.imread('test_images/green_image.jpg')
red = cv2.imread('test_images/red_image.jpg')

plt.imshow(green)

enter image description here

plt.imshow(red)

enter image description here

green_grey = cv2.cvtColor(green, cv2.COLOR_RGB2GRAY)
red_grey = cv2.cvtColor(red, cv2.COLOR_RGB2GRAY)
bitwiseand = cv2.bitwise_and(green_grey, red_grey)
plt.imshow(bitwiseand)

enter image description here

type(bitwiseand)
>>> numpy.ndarray
bitwiseand.shape
>>> (532, 611)

total = green_grey red_grey
total_pixels = total[total>0].shape[0]

matches = bitwiseand[bitwiseand > 0].shape[0]
percentage = (100*matches/total_pixels)
percentage
>>> 12.341323771795391%

So logically:

  1. Load images into cv2 numpy arrays
  2. Convert to greyscale
  3. Use cv2 bitwise_and to find the overlaps
  4. Divide the number of values that are > 0 in bitwiseand & > 0 in the added images together
  5. that's it!

hope this helps!

CodePudding user response:

If the red and green channels are binarized, it suffices to count the green pixels and for every green pixel tell if there is a red in the other channel. It is an easy matter of scanning the images.

If the channels are not binarized, do binarize them and proceed as above.

  • Related