Skip to content

Commit

Permalink
local threshold binarize methods + refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
drewdru committed Apr 12, 2017
1 parent d695ada commit 026084c
Show file tree
Hide file tree
Showing 5 changed files with 253 additions and 238 deletions.
49 changes: 23 additions & 26 deletions controllers/binarizeController.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
sys.path.append(os.path.abspath(os.path.dirname(__file__) + '/' + '../..'))

from imageProcessor import histogramService, imageService, imageComparison
from imageBinarize import globalThresholding, histogramBinarize
from imageBinarize import globalThresholding, localThresholding
from PyQt5.QtCore import QCoreApplication, QDir
from PyQt5.QtCore import QObject, pyqtSlot
from PyQt5.QtQml import QJSValue
Expand All @@ -36,32 +36,29 @@ def otsuBinarize(self, colorModelTag, currentImageChannelIndex, isOriginalImage,
return
img = img.convert(mode='L')
methodTimer = time.time()
histogramBinarize.histogramSegmentation(img.load(), img.size, 'otsu', otsu_k)
# if colorModelTag == 'RGB':
# filters.meanFilter(colorModelTag, currentImageChannelIndex, img.load(),
# img.size, (filterWidth, filterHeight))
# methodTimer = time.time() - methodTimer
# self.histogramService.saveHistogram(img=img, model=colorModelTag)
# if colorModelTag == 'YUV':
# colorModel.rgbToYuv(img.load(), img.size)
# filters.meanFilter(colorModelTag, currentImageChannelIndex, img.load(),
# img.size, (filterWidth, filterHeight))
# colorModel.yuvToRgb(img.load(), img.size)
# methodTimer = time.time() - methodTimer
# self.histogramService.saveHistogram(img=img, model=colorModelTag)
# if colorModelTag == 'HSL':
# data = numpy.asarray(img, dtype="float")
# data = colorModel.rgbToHsl(data)
# filters.meanFilter(colorModelTag, currentImageChannelIndex, data,
# data.shape, (filterWidth, filterHeight))
# methodTimer = time.time() - methodTimer
# self.histogramService.saveHistogram(data=data, model=colorModelTag)
# timerTemp = time.time()
# data = colorModel.hslToRgb(data)
# img = Image.fromarray(numpy.asarray(numpy.clip(data, 0, 255), dtype="uint8"))
# methodTimer = time.time() - timerTemp + methodTimer
globalThresholding.histogramSegmentation(img.load(), img.size, 'otsu', otsu_k)
methodTimer = time.time() - methodTimer
logFile = '{}/temp/log/otsuBinarize.log'.format(self.appDir)
with open(logFile, "a+") as text_file:
text_file.write("Timer: {}: {}\n".format(colorModelTag, methodTimer))
img = img.convert(mode='RGB')
img.save('{}/temp/processingImage.png'.format(self.appDir))
imageComparison.calculateImageDifference(colorModelTag, logFile)

logFile = '{}/temp/log/meanFilter.log'.format(self.appDir)
@pyqtSlot(str, int, bool, int)
def histThresholdBinarize(self, colorModelTag, currentImageChannelIndex, isOriginalImage,
otsu_k):
"""
Otsu Binarize
"""
img = self.imageService.openImage(isOriginalImage)
if img is None:
return
img = img.convert(mode='L')
methodTimer = time.time()
globalThresholding.histogramSegmentation(img.load(), img.size, 'histPeakValue', otsu_k)
methodTimer = time.time() - methodTimer
logFile = '{}/temp/log/histThresholdBinarize.log'.format(self.appDir)
with open(logFile, "a+") as text_file:
text_file.write("Timer: {}: {}\n".format(colorModelTag, methodTimer))
img = img.convert(mode='RGB')
Expand Down
184 changes: 97 additions & 87 deletions imageBinarize/globalThresholding.py
Original file line number Diff line number Diff line change
@@ -1,92 +1,102 @@
"""Global thresholding segmentations"""
"""Gloabal thresholding binarize"""
import math
from PIL import PSDraw
def getHistogramRGB(img, size):
"""
@use:
img = Image.open(filepath)
img = img.convert(mode='RGB')
img = img.resize(size, Image.ANTIALIAS)
histogramR, histogramG, histogramB = get_histogramRGB(img, size)
"""
histogramR = []
histogramG = []
histogramB = []
for i in range(256):
histogramR.append(0)
histogramG.append(0)
histogramB.append(0)
for i in range(size[0]):
for j in range(size[1]):
r, g, b = img.getpixel((i, j))
histogramR[r] += 1
histogramG[g] += 1
histogramB[b] += 1
histogram = []
for i in range(256):
histogramR[i] /= size[0]*size[1]
histogramG[i] /= size[0]*size[1]
histogramB[i] /= size[0]*size[1]
return histogramR, histogramG, histogramB

def getAperturePosition(x, y, imgSize, i, j, filterSize):
pixelPosX = x + j - filterSize / 2
pixelPosY = y + i - filterSize / 2
if pixelPosX<0:
pixelPosX+=filterSize / 2
if pixelPosY<0:
pixelPosY+=filterSize / 2
if pixelPosX>imgSize[0] or pixelPosY>imgSize[1]:
return -1, -1
if pixelPosX==imgSize[0]:
pixelPosX=imgSize[0] - (filterSize / 2)-1
if pixelPosY==imgSize[1]:
pixelPosY=imgSize[1]-(filterSize / 2)-1
return pixelPosX, pixelPosY
def getHistogram(pixels, size):
histogram = []
for i in range(256):
histogram.append(0)
for i in range(size[0]):
for j in range(size[1]):
color = pixels[i, j]
histogram[color] += 1
for i in range(256):
histogram[i] /= size[0]*size[1]
return histogram

def histogramPeak3(histogram, size, k):
k /= 256
peak = []
lastValue = 0
maxVal = max(histogram)
for indx, v in enumerate(histogram):
if maxVal - v > k:
peak.append(indx-1)
return peak

# Bersen's method
def bernsen(img, imgSize, filterSize):
pixels = img.load()
for x in range(imgSize[0]):
for y in range(imgSize[1]):
minValue = 255
maxValue = 0
for i in range(filterSize):
for j in range(filterSize):
pixelPosX, pixelPosY = getAperturePosition(x, y, imgSize, i, j, filterSize)
if pixelPosX == -1 or pixelPosY == -1:
continue
if pixels[pixelPosX,pixelPosY] > maxValue:
maxValue = pixels[pixelPosX,pixelPosY]
if pixels[pixelPosX,pixelPosY] < minValue:
minValue = pixels[pixelPosX,pixelPosY]
avg = (minValue + maxValue) / 2
for i in range(filterSize):
for j in range(filterSize):
pixelPosX, pixelPosY = getAperturePosition(x, y, imgSize, i, j, filterSize)
if pixelPosX == -1 or pixelPosY == -1:
continue
if pixels[pixelPosX,pixelPosY] < avg:
pixels[pixelPosX,pixelPosY] = 0
else:
pixels[pixelPosX,pixelPosY] = 255
img.show()
def otsuPeak(histogram, size, k):
peak = []
lastValue = 0
w1 = 0
w2 = 0
w = 0
mu1 = 0
mu2 = 0
mu = 0
for indx, v in enumerate(histogram):
if (indx <= k):
w1 += v
else:
w2 += v
for indx, v in enumerate(histogram):
value = indx*v
if (indx <= k):
mu1 += value/w1
else:
mu2 += value/w2
mu += (value/w1 + value/w2)
nu = (w1*w2*((mu2-mu1)**2))/(mu**2)
for indx, v in enumerate(histogram):
if v < nu:
peak.append(indx-1)
return peak

# Niblack`s method
def niblack(img, imgSize, filterSize):
k = 0.2
pixels = img.load()
for x in range(imgSize[0]):
for y in range(imgSize[1]):
minValue = 255
maxValue = 0
for i in range(filterSize):
for j in range(filterSize):
pixelPosX, pixelPosY = getAperturePosition(x, y, imgSize, i, j, filterSize)
if pixelPosX == -1 or pixelPosY == -1:
continue
if pixels[pixelPosX,pixelPosY] > maxValue:
maxValue = pixels[pixelPosX,pixelPosY]
if pixels[pixelPosX,pixelPosY] < minValue:
minValue = pixels[pixelPosX,pixelPosY]
avg = (minValue + maxValue) / 2
RMS = 0
differenceSum = 0
for i in range(filterSize):
for j in range(filterSize):
pixelPosX, pixelPosY = getAperturePosition(x, y, imgSize, i, j, filterSize)
if pixelPosX == -1 or pixelPosY == -1:
continue
differenceSum = (pixels[pixelPosX,pixelPosY] - avg) ** 2
RMS += math.sqrt(differenceSum/filterSize)
# value = int(avg + k*RMS)
# for i in range(filterSize):
# for j in range(filterSize):
# pixelPosX, pixelPosY = getAperturePosition(x, y, imgSize, i, j, filterSize)
# if pixelPosX == -1 or pixelPosY == -1:
# continue
# pixels[pixelPosX,pixelPosY] = value
value = int(avg + k*RMS)
for i in range(filterSize):
for j in range(filterSize):
pixelPosX, pixelPosY = getAperturePosition(x, y, imgSize, i, j, filterSize)
if pixelPosX == -1 or pixelPosY == -1:
continue
if pixels[pixelPosX,pixelPosY] < value:
pixels[pixelPosX,pixelPosY] = 0
else:
pixels[pixelPosX,pixelPosY] = 255
img.show()
def histogramSegmentation(pixels, size, method, threshold):
"""http://www.ijcset.net/docs/Volumes/volume2issue1/ijcset2012020103.pdf"""
# pixels = img.load()
histogram = getHistogram(pixels, size)
if method == 'otsu':
peak = otsuPeak(histogram, size, threshold)
for i in range(size[0]):
for j in range(size[1]):
if pixels[i, j] in peak:
pixels[i, j] = 255
else:
pixels[i, j] = 0
# img.show('histogramPeak')
if method == 'histPeakValue':
peak = histogramPeak3(histogram, size, threshold)
for i in range(size[0]):
for j in range(size[1]):
if pixels[i, j] in peak:
pixels[i, j] = 255
else:
pixels[i, j] = 0
# img.show('histogramPeak3')
102 changes: 0 additions & 102 deletions imageBinarize/histogramBinarize.py

This file was deleted.

Loading

0 comments on commit 026084c

Please sign in to comment.