I have two images that will be identical in dimensions.
The images are numpy arrays and I am itterating the pixels and aquiring the r
g
b
of each pixel like this:
for i in range(len(img1)):
for j in range(len(img1[0])):
pimg1 = img1[i][j]
pimg2 = img2[i][j]
r1 = pimg1[0]
r2 = pimg2[0]
g1 = pimg1[1]
g2 = pimg2[1]
b1 = pimg1[2]
b2 = pimg2[2]
I then acquire the MSE between the two pixels like:
mse = math.sqrt(((r1 - r2) ** 2) + ((g1 - g2) ** 2) + ((b1 - b2) ** 2))
The problem is this is ridiculously slow. Is there a more efficient way to do this?
The final goal
I am looking to make all pixels that have a certain "thresholded" similarity, between the two images, black. And all pixels that have a larger difference the pixel of img2
.
if mse > threshold:
new_img[i][j] = pimg2
else:
new_img[i][j] = [0, 0, 0] # black pixel
background image
entered image
I am capturing images like:
for frame in cam.camera.capture_continuous(raw, format="rgb", use_video_port=True):
img = frame.array
cv2.imwrite("image.png", img)
I am getting the images like:
dir = 'images/compare/'
bg = cv2.imread(dir+'bg.png')
img = cv2.imread(dir+'in.png')