def histogram_match(source_img, template_img):

    img = source_img
    img_ref = template_img

    height = img.shape[0]
    width = img.shape[1]
    pixels = width * height

    height_ref = img_ref.shape[0]
    width_ref = img_ref.shape[1]
    pixels_ref = width_ref * height_ref

    hist = h.histogram(img)
    hist_ref = h.histogram(img_ref)

    cum_hist = ch.cumulative_histogram(hist)
    cum_hist_ref = ch.cumulative_histogram(hist_ref)

    prob_cum_hist = cum_hist / pixels

    prob_cum_hist_ref = cum_hist_ref / pixels_ref

    K = 256
    new_values = np.zeros((K))

    for a in np.arange(K):
        j = K - 1
        while True:
            new_values[a] = j
            j = j - 1
            if j < 0 or prob_cum_hist[a] > prob_cum_hist_ref[j]:
                break

    for i in np.arange(height):
        for j in np.arange(width):
            a = img.item(i, j)
            b = new_values[a]
            img.itemset((i, j), b)

    return img
Exemplo n.º 2
0
def mac(self):

    image = Image.open(files)
    image.save("c.png")
    img = cv2.imread('c.png', cv2.IMREAD_GRAYSCALE)

    height = img.shape[0]
    width = img.shape[1]
    pixels = width * height

    hist = h.histogram(img)
    cum_hist = ch.cumulative_histogram(hist)

    p = 0.005

    a_low = 0
    for i in np.arange(256):
        if cum_hist[i] >= pixels * p:
            a_low = i
            break

    a_high = 255
    for i in np.arange(255, -1, -1):
        if cum_hist[i] <= pixels * (1 - p):
            a_high = i
            break

    for i in np.arange(height):
        for j in np.arange(width):
            a = img.item(i, j)
            b = 0
            if a <= a_low:
                b = 0
            elif a >= a_high:
                b = 255
            else:
                b = float(a - a_low) / (a_high - a_low) * 255
            img.itemset((i, j), b)

    cv2.imwrite('output/output.png', img)
    pixmap = QtGui.QPixmap("output/output.png") # Setup pixmap with the provided image
    pixmap = pixmap.scaled(self.imagelab2.width(), self.imagelab2.height(), QtCore.Qt.KeepAspectRatio) # Scale pixmap
    self.imagelab2.setPixmap(pixmap) # Set the pixmap onto the label
    self.imagelab2.setAlignment(QtCore.Qt.AlignCenter)

    #cv2.imshow('image', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
Exemplo n.º 3
0
cv2.destroyAllWindows()

if len(screenCnt) == 4:
    warped = four_point_transform(orig, screenCnt.reshape(4, 2) * ratio)
else:
    warped = orig

# convert the warped image to grayscale, then threshold it
# to give it that 'black and white' paper effect
warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY)
height = warped.shape[0]
width = warped.shape[1]
pixels = width * height

hist = h.histogram(warped)
cum_hist = ch.cumulative_histogram(hist)
brightness = 40
p = 0.005

a_low = 0
for i in np.arange(256):
    if cum_hist[i] >= pixels * p:
        a_low = i
        break

a_high = 255
for i in np.arange(255, -1, -1):
    if cum_hist[i] <= pixels * (1 - p):
        a_high = i
        break
img = cv2.imread('images/cat.jpg', cv2.IMREAD_GRAYSCALE)
img_ref = cv2.imread('images/img1.jpg', cv2.IMREAD_GRAYSCALE)

height = img.shape[0]
width = img.shape[1]
pixels = width * height

height_ref = img_ref.shape[0]
width_ref = img_ref.shape[1]
pixels_ref = width_ref * height_ref

hist = h.histogram(img)
hist_ref = h.histogram(img_ref)

cum_hist = ch.cumulative_histogram(hist)
cum_hist_ref = ch.cumulative_histogram(hist_ref)

prob_cum_hist = cum_hist / pixels

prob_cum_hist_ref = cum_hist_ref / pixels_ref

K = 256
new_values = np.zeros((K))

for a in np.arange(K):
    j = K - 1
    while True:
        new_values[a] = j
        j = j - 1
        if j < 0 or prob_cum_hist[a] > prob_cum_hist_ref[j]: