0% found this document useful (0 votes)
83 views6 pages

Fuzzy Color Image Enhancement

Fuzzy logic allows for degrees of truth between fully true and fully false. It is used in artificial intelligence to mimic human reasoning. Fuzzy logic image processing uses membership functions to define the degree to which pixels belong to edges or uniform regions, rather than crisp boundaries. The fuzzy logic approach divides an image into windows and calculates means, variances, and enhancement values for each window to adjust pixel intensities and increase contrast.

Uploaded by

arman khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views6 pages

Fuzzy Color Image Enhancement

Fuzzy logic allows for degrees of truth between fully true and fully false. It is used in artificial intelligence to mimic human reasoning. Fuzzy logic image processing uses membership functions to define the degree to which pixels belong to edges or uniform regions, rather than crisp boundaries. The fuzzy logic approach divides an image into windows and calculates means, variances, and enhancement values for each window to adjust pixel intensities and increase contrast.

Uploaded by

arman khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Fuzzy Color Image Enhancement - Fuzzy Logic

What is fuzzy logic ?


The idea of fuzzy logic was first advanced by Lotfi Zadeh of the University
of California at Berkeley in the 1960s. Zadeh was working on the problem of
computer understanding of natural language.
Fuzzy logic in AI
In artificial intelligence (AI) systems, fuzzy logic is used to imitate human
reasoning and cognition. Rather than strictly binary cases of truth, fuzzy logic
includes 0 and 1 as extreme cases of truth but with various intermediate degrees of
truth.
As a result, fuzzy logic is well-suited for the following:
 engineering for decisions without clear certainties and uncertainties, or with
imprecise data -- such as with natural language processing technologies; and
 regulating and controlling machine outputs, according to multiple
inputs/input variables -- such as with temperature control systems.
Fuzzy Logic Image Processing :
An edge is a boundary between two uniform regions. You can detect an edge
by comparing the intensity of neighboring pixels. However, because uniform
regions are not crisply defined, small intensity differences between two
neighboring pixels do not always represent an edge. Instead, the intensity
difference might represent a shading effect.
The fuzzy logic approach for image processing allows you to use
membership functions to define the degree to which a pixel belongs to an edge or a
uniform region.

ARMAN KHAN – 24 RAJESH SAHU - 14


CODE :
import cv2 as cv
import numpy as np
import os as os
import math
import time
from matplotlib import pyplot as plt

n = 2 # number of rows (windows on columns)


m = 2 # number of colomns (windows on rows)
EPSILON = 0.00001
#GAMMA, IDEAL_VARIANCE 'maybe' have to changed from image to another
GAMMA = 1 # Big GAMMA >> Big mean >> More Brightness
IDEAL_VARIANCE = 0.35 #Big value >> Big variance >> Big lamda >> more contrast

img_name = '/content/enishtein.png'
if os.path.exists(img_name):
  img = cv.imread(img_name)
#img = cv.resize(img, (200, 200))
layer = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
WIDTH = layer.shape[1]
HEIGHT = layer.shape[0]
x0, x1, y0, y1 = 0, WIDTH - 1, 0, HEIGHT - 1

def phy(value): # phy: E --> R


    #if ((1+value)/((1-value)+0.0001)) < 0:
    #print(value)
    return 0.5 * np.log((1+value)/((1-value)+EPSILON))

def multiplication(value1, value2): # ExE --> R


    return phy(value1) * phy(value2)

def norm(value):
    return abs(phy(value))

def scalar_multiplication(scalar, value):# value in E ([-1,1])


    s = (1+value)**scalar
    z = (1-value)**scalar
    res = (s-z)/(s+z+EPSILON)
    return res

def addition(value1, value2): # value1,value2 are in E ([-1,1])


    res = (value1+value2)/(1+(value1*value2)+EPSILON)
    return res

ARMAN KHAN – 24 RAJESH SAHU - 14


def subtract(value1, value2): # value1,value2 are in E ([-1,1])
    res = (value1-value2)/(1-(value1*value2)+EPSILON)
    return res

def C(m,i):
    return math.factorial(m)/((math.factorial(i)*math.factorial(m-i))+EPSILON)

def qx(i, x): # i: window index in rows, x: number of current pixel on x-axis
    if (x == WIDTH - 1):
        return 0
    return C(m,i)*(np.power((x-x0)/(x1-x), i) * np.power((x1-x)/(x1-x0), m))
#This is the seconf implementation
    #return C(m,i)*((np.power(x-x0,i) * np.power(x1-x,m-i)) / (np.power(x1-x0,m)
+EPSILON))

def qy(j, y):


    '''
    The second implementation for the formula does not go into overflow.
    '''
    if (y == HEIGHT - 1):
        return 0
    return C(n,j)*(np.power((y-y0)/(y1-y), j) * np.power((y1-y)/(y1-y0), n))
#This is the seconf implementation
    #return C(n,j)*((np.power((y-y0),j) * np.power((y1-y),n-j))/ (np.power(y1-
y0,n)+EPSILON))
   
def p(i, j, x, y):
    return qx(i, x) * qy(j, y)

def mapping(img, source, dest):


    return (dest[1] - dest[0])*((img - source[0]) / (source[1] - source[0])) +
dest[0]

e_layer_gray = mapping(layer, (0, 255), (-1, 1))

def cal_ps_ws(m, n, w, h, gamma):


    ps = np.zeros((m, n, w, h))
    for i in range(m):
        for j in range(n):
            for k in range(w):
                for l in range(h):    
                    ps[i, j, k, l] = p(i, j, k, l)

    ws = np.zeros((m, n, w, h))

ARMAN KHAN – 24 RAJESH SAHU - 14


    for i in range(m):
        for j in range(n):
            ps_power_gamma = np.power(ps[i, j], gamma)
            for k in range(w):
                for l in range(h):    
                    ws[i, j, k, l] = ps_power_gamma[k, l] / (np.sum(ps[:, :, k,
l])+EPSILON)
    return ps, ws
print('Ps and Ws calculation is in progress...')
start = time.time()
ps, ws = cal_ps_ws(m, n, WIDTH, HEIGHT, GAMMA)
end = time.time()
print('Ps and Ws calculation has completed successfully in '+str(end-start)+' s')

def cal_means_variances_lamdas(w, e_layer):


    means = np.zeros((m, n))
    variances = np.zeros((m, n))
    lamdas = np.zeros((m, n))
    taos = np.zeros((m, n))
    def window_card(w):
        return np.sum(w)
    def window_mean(w, i, j):
        mean = 0
        for k in range(HEIGHT):
            for l in range(WIDTH):
                mean = addition(mean, scalar_multiplication(w[i, j, l, k],
e_layer[k, l]))
        mean /= window_card(w[i, j])
        return mean
    def window_variance(w, i, j):
        variance = 0
        for k in range(HEIGHT):
            for l in range(WIDTH):
                variance += w[i, j, l, k] * np.power(norm(subtract(e_layer[k,
l], means[i, j])), 2)
        variance /= window_card(w[i, j])
        return variance
    def window_lamda(w, i, j):
        return np.sqrt(IDEAL_VARIANCE) / (np.sqrt(variances[i, j])+EPSILON)
    def window_tao(w, i, j):
        return window_mean(w, i, j)
    for i in range(m):
        for j in range(n):
            means[i, j] = window_mean(ws, i, j)
            variances[i, j] = window_variance(ws, i, j)

ARMAN KHAN – 24 RAJESH SAHU - 14


            lamdas[i, j] = window_lamda(ws, i, j)
    taos = means.copy()
    return means, variances, lamdas, taos
print('means, variances, lamdas and taos calculation is in progress...')
start = time.time()
means, variances, lamdas, taos = cal_means_variances_lamdas(ws, e_layer_gray)
end = time.time()
print('means, variances, lamdas and taos calculation is finished in ' + str(end-
start) + ' s')

def window_enh(w, i, j, e_layer):


    return scalar_multiplication(lamdas[i, j], subtract(e_layer, taos[i, j]))

def image_enh(w, e_layer):


    new_image = np.zeros(e_layer.shape)
    width = e_layer.shape[1]
    height = e_layer.shape[0]
    for i in range(m):
        for j in range(n):
            win = window_enh(w, i, j, e_layer)
            w1 = w[i, j].T.copy()
            for k in range(width):
                for l in range(height):
                    new_image[l, k] = addition(new_image[l, k],
scalar_multiplication(w1[l, k], win[l, k]))
    return new_image

def one_layer_enhacement(e_layer):
    #card_image = layer.shape[0]*layer.shape[1]
    new_E_image = image_enh(ws, e_layer)
    res_image = mapping(new_E_image, (-1, 1), (0, 255))
    res_image = np.round(res_image)
    res_image = res_image.astype(np.uint8)
    return res_image

res_img = one_layer_enhacement(e_layer_gray)

plt.subplot(1, 2, 1)
plt.imshow(img, cmap = 'gray')
plt.subplot(1, 2, 2)
plt.imshow(res_img, cmap = 'gray')
plt.title('Fuzzy Grayscale image enhacement.')
plt.show()

ARMAN KHAN – 24 RAJESH SAHU - 14


OUTPUT :

ARMAN KHAN – 24 RAJESH SAHU - 14

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy