Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions image.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
# -*- coding: utf-8 -*-
# encoding=utf-8

import os.path
import numpy as np

# from scipy import misc
import imageio
Comment thread
Rachine marked this conversation as resolved.
import matplotlib.pyplot as plt
from scipy import ndimage, misc
import pdb
import numpy as np


def weightedAverage(pixel):
"""
"""
return 0.299 * pixel[0] + 0.587 * pixel[1] + 0.114 * pixel[2]


def exponential_euclidean(canal, sigma):
return np.exp(-(canal - 0.5)**2 / (2 * sigma**2))
return np.exp(-(canal - 0.5) ** 2 / (2 * sigma ** 2))


def show(color_array):
Expand All @@ -36,21 +39,23 @@ class Image(object):
def __init__(self, fmt, path, crop=False, n=0):
self.path = os.path.join("image_set", fmt, str(path))
self.fmt = fmt
self.array = misc.imread(self.path)
self.array = imageio.imread(self.path)
self.array = self.array.astype(np.float32) / 255
if crop:
self.crop_image(n)
self.shape = self.array.shape

def crop_image(self, n):
resolution = 2**n
"""
"""
resolution = 2 ** n
(height, width, _) = self.array.shape
(max_height, max_width) = (resolution * (height // resolution),
resolution * (width // resolution))
(begin_height, begin_width) = ((height - max_height) / 2,
(width - max_width) / 2)
self.array = self.array[begin_height:max_height + begin_height,
begin_width:max_width + begin_width]
self.array = self.array[int(begin_height):int(max_height + begin_height),
int(begin_width):int(max_width + begin_width)]

@property
def grayScale(self):
Expand All @@ -65,8 +70,8 @@ def saturation(self):
green_canal = self.array[:, :, 1]
blue_canal = self.array[:, :, 2]
mean = (red_canal + green_canal + blue_canal) / 3.0
saturation = np.sqrt(((red_canal - mean)**2 + (green_canal - mean)**2 +
(blue_canal - mean)**2) / 3)
saturation = np.sqrt(((red_canal - mean) ** 2 + (green_canal - mean) ** 2 +
(blue_canal - mean) ** 2) / 3)
return saturation

def contrast(self):
Expand All @@ -79,13 +84,15 @@ def contrast(self):
# [ -1, 8, -1 ],
# [ -1, -1, -1 ]])
kernel = np.array([[0, 1, 0], [1, -4, 1], [0, 1, 0]])

for row in range(self.shape[0]):
for col in range(self.shape[1]):
contrast[row][col] = np.abs(
(kernel *
grey_extended[row:(row + 3), col:(col + 3)]).sum())
contrast = (contrast - np.min(contrast))
contrast = contrast / np.max(contrast)

return contrast

def sobel(self):
Expand Down
112 changes: 72 additions & 40 deletions laplacianfusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
@author: Rachid & Chaima
"""

import cv2
import numpy as np
import matplotlib.pyplot as plt
from scipy import ndimage, misc
from scipy import misc

import image
import utils
import pdb


#
#def div0( a, b ):
# def div0( a, b ):
# """ ignore / 0, div0( [-1, 0, 1], 0 ) -> [0, 0, 0] """
# with np.errstate(divide='ignore', invalid='ignore'):
# c = np.true_divide( a, b )
Expand All @@ -25,93 +26,124 @@ class LaplacianMap(object):
"""Class for weights attribution with Laplacian Fusion"""

def __init__(self, fmt, names, n=3):
"""names is a liste of names, fmt is the format of the images"""
"""
names is a liste of names, fmt is the format of the images
"""
self.images = []
for name in names:
self.images.append(image.Image(fmt, name, crop=True, n=n))

self.shape = self.images[0].shape
self.num_images = len(self.images)
self.height_pyr = n

def get_weights_map(self, w_c, w_s, w_e):
"""Return the normalized Weight map"""
"""
Return the normalized Weight map
"""
self.weights = []
sums = np.zeros((self.shape[0], self.shape[1]))

for image_name in self.images:
contrast = image_name.contrast()
saturation = image_name.saturation()
exposedness = image_name.exposedness()
weight = (contrast**w_c) * (saturation**w_s) * (exposedness**
w_e) + 1e-12
weight = (contrast ** w_c) * (saturation ** w_s) * (exposedness **
w_e) + 1e-12
self.weights.append(weight)
sums = sums + weight
for index in range(self.num_images):
self.weights[index] = self.weights[index] / sums

return self.weights

def get_gaussian_pyramid(self, image, n):
"""Return the Gaussian Pyramid of an image"""
gaussian_pyramid_floors = [image]
"""
Return the Gaussian Pyramid of an image
"""
gaussian_pyramids = [image]

for floor in range(1, n):
gaussian_pyramid_floors.append(
utils.Reduce(gaussian_pyramid_floors[-1], 1))
return gaussian_pyramid_floors
layer = utils.Reduce(gaussian_pyramids[-1], 1)
gaussian_pyramids.append(layer)

return gaussian_pyramids

def get_gaussian_pyramid_weights(self):
"""Return the Gaussian Pyramid of the Weight map of all images"""
"""
Return the Gaussian Pyramid of the Weight map of all images
"""
self.weights_pyramid = []

for index in range(self.num_images):
self.weights_pyramid.append(
self.get_gaussian_pyramid(self.weights[index],
self.height_pyr))
gauss_pyramids = self.get_gaussian_pyramid(self.weights[index], self.height_pyr)
self.weights_pyramid.append(gauss_pyramids)

return self.weights_pyramid

def get_laplacian_pyramid(self, image, n):
"""Return the Laplacian Pyramid of an image"""
gaussian_pyramid_floors = self.get_gaussian_pyramid(image, n)
laplacian_pyramid_floors = [gaussian_pyramid_floors[-1]]
"""
Return the Laplacian Pyramid of an image
"""
# if len(image.shape) == 3:
# image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

gauss_pyramids = self.get_gaussian_pyramid(image, n)
laplacian_pyramids = [gauss_pyramids[-1]]

for floor in range(n - 2, -1, -1):
new_floor = gaussian_pyramid_floors[floor] - utils.Expand(
gaussian_pyramid_floors[floor + 1], 1)
laplacian_pyramid_floors = [new_floor] + laplacian_pyramid_floors
return laplacian_pyramid_floors
expanded = utils.Expand(gauss_pyramids[floor + 1], 1)
new_floor = gauss_pyramids[floor] - expanded
laplacian_pyramids = [new_floor] + laplacian_pyramids

return laplacian_pyramids

def get_laplacian_pyramid_images(self):
"""Return all the Laplacian pyramid for all images"""
"""
Return all the Laplacian pyramid for all images
"""
self.laplacian_pyramid = []

for index in range(self.num_images):
self.laplacian_pyramid.append(
self.get_laplacian_pyramid(self.images[index].array,
self.height_pyr))
img = self.images[index].array
laplace_pyramids = self.get_laplacian_pyramid(img, self.height_pyr)
self.laplacian_pyramid.append(laplace_pyramids)

return self.laplacian_pyramid

def result_exposure(self, w_c=1, w_s=1, w_e=1):
"""
"""
"Return the Exposure Fusion image with Laplacian/Gaussian Fusion method"
print "weights"
print("weights")

self.get_weights_map(w_c, w_s, w_e)
print "gaussian pyramid"
print("gaussian pyramid")

self.get_gaussian_pyramid_weights()
print "laplacian pyramid"
print("laplacian pyramid")

self.get_laplacian_pyramid_images()
result_pyramid = []
for floor in range(self.height_pyr):
print 'floor ', floor
print('floor ', floor)
result_floor = np.zeros(self.laplacian_pyramid[0][floor].shape)
for index in range(self.num_images):
print 'image ', index
for canal in range(3):
result_floor[:, :,
canal] += self.laplacian_pyramid[index][floor][:, :,
canal] * self.weights_pyramid[index][floor]
print('image ', index)
for channel in range(3):
result_floor[:, :, channel] += self.laplacian_pyramid[index][floor][:, :, channel] \
* self.weights_pyramid[index][floor]
result_pyramid.append(result_floor)

# Get the image from the Laplacian pyramid
self.result_image = result_pyramid[-1]
for floor in range(self.height_pyr - 2, -1, -1):
print 'floor ', floor
self.result_image = result_pyramid[floor] + utils.Expand(
self.result_image, 1)
print('floor ', floor)
self.result_image = result_pyramid[floor] + utils.Expand(self.result_image, 1)

self.result_image[self.result_image < 0] = 0
self.result_image[self.result_image > 1] = 1

return self.result_image


Expand Down
80 changes: 38 additions & 42 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,49 @@
# coding: utf-8

import argparse

import image
import naivefusion
import laplacianfusion
import naivefusion

# Loading the arguments

parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument(
'-l',
'--list',
dest='names',
type=str,
default='list_images.txt',
help='The text file which contains the names of the images')
parser.add_argument(
'-f',
'--folder',
dest='folder',
type=str,
required=True,
help='The folder containing the images')
parser.add_argument(
'-hp',
'--heightpyr',
dest='height_pyr',
type=int,
default=6,
help='The height of the Laplacian pyramid')
parser.add_argument(
'-wc',
dest='w_c',
type=float,
default=1.0,
help='Exponent of the contrast')
parser.add_argument(
'-ws',
dest='w_s',
type=float,
default=1.0,
help='Exponent of the saturation')
parser.add_argument(
'-we',
dest='w_e',
type=float,
default=1.0,
help='Exponent of the exposedness')
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)

parser.add_argument('-l',
'--list',
dest='names',
type=str,
default='list_images.txt',
help='The text file which contains the names of the images')
parser.add_argument('-f',
'--folder',
type=str,
default='./mask/',
dest='folder',
help='The folder containing the images')
parser.add_argument('-hp',
'--heightpyr',
dest='height_pyr',
type=int,
default=6,
help='The height of the Laplacian pyramid')
parser.add_argument('-wc',
dest='w_c',
type=float,
default=1.0,
help='Exponent of the contrast')
parser.add_argument('-ws',
dest='w_s',
type=float,
default=1.0,
help='Exponent of the saturation')
parser.add_argument('-we',
dest='w_e',
type=float,
default=1.0,
help='Exponent of the exposedness')

args = parser.parse_args()
params = vars(args) # convert to ordinary dict

Expand Down
Loading