
# https://www.globalsino.com/ICs/
# Extract a mask from an image with a threshold 

import numpy as np
import glob
import matplotlib.pyplot as plt
import skimage.io
import skimage.color
import skimage.filters
from skimage.color import rgb2gray

image = skimage.io.imread(r"C:\GlobalSino2\ICs\images\4218j.png")

# fig, ax = plt.subplots()
# plt.imshow(image)
# plt.show()

# convert the image to grayscale
gray_image = rgb2gray(image)
blurred_imageA = skimage.filters.gaussian(gray_image, sigma=1.0)

fig, ax = plt.subplots()
plt.imshow(blurred_imageA, cmap="gray")
plt.show()

# create a histogram of the blurred grayscale image
histogram, bin_edges = np.histogram(blurred_imageA, bins=256, range=(0.0, 1.0))

# fig, ax = plt.subplots()
# plt.plot(bin_edges[0:-1], histogram)
# plt.title("Grayscale Histogram")
# plt.xlabel("grayscale value")
# plt.ylabel("pixels")
# plt.xlim(0, 1.0)
# plt.show()

# create a mask based on the threshold
tA = 0.3
binary_maskA = (blurred_imageA > tA)

fig, ax = plt.subplots()
plt.imshow(binary_maskA, cmap="gray")
plt.savefig(r"C:\GlobalSino2\ICs\images\4218jA.png")
plt.show()

gray_image = rgb2gray(image)
blurred_imageB = skimage.filters.gaussian(gray_image, sigma=1.0)

fig, ax = plt.subplots()
plt.imshow(blurred_imageB, cmap="gray")
plt.show()

# create a histogram of the blurred grayscale image
histogram, bin_edges = np.histogram(blurred_imageB, bins=256, range=(0.0, 1.0))

# create a mask based on the threshold
tB = 0.1
binary_maskB = (blurred_imageB > tB)

fig, ax = plt.subplots()
plt.imshow(binary_maskB, cmap="gray")
plt.savefig(r"C:\GlobalSino2\ICs\images\4218jB.png")
plt.show()

import cv2
import numpy as np
 
MyImageA = cv2.imread(r"C:\GlobalSino2\ICs\images\4218jA.png")
MyImageB = cv2.imread(r"C:\GlobalSino2\ICs\images\4218jB.png")

imgSubtract = MyImageB - MyImageA

cv2.imshow('Image Subtraction',imgSubtract)
cv2.imwrite(r"C:\GlobalSino2\ICs\images\4218jC.png", imgSubtract)
cv2.waitKey(0)




