def odbtcoding(image, height, width, ksize): kernel = ditherArray(int(0), int(0), int(ksize), int(0), int(1)) kheight, kwidth = tools.getInfo(kernel) bs = kheight loopHeight = np.uint8(height / bs) loopWidth = np.uint8(width / bs) image = tools.normalize(image) for i in range(0, loopHeight): for j in range(0, loopWidth): tempImg = image[i*bs:i*bs+bs,j*bs:j*bs+bs] nMax = tools.getMax(tempImg) nMin = tools.getMin(tempImg) k = nMax - nMin for x in range(0, bs): for y in range(0, bs): DA = k * kernel[x][y] th = DA + nMin if image[i*bs+x][j*bs+y] >= th: image[i*bs+x][j*bs+y] = nMax else: image[i*bs+x][j*bs+y] = nMin return np.uint8(image * 255)
def edbs(dirName, prior): print("Processing for direct binary search !") listImgs = tools.loadDirectory(dirName) outDir = 'results' tools.createDir(outDir) for(idx, imgname) in enumerate(listImgs): image = tools.readImage(imgname) height, width = tools.getInfo(image) source = np.copy(image) if prior == 'ED': # the configs are fixed ktype = 'floyd-steinberg' threshold = 127.5 pad = 1 imageHT = algorithms.errorDif(image, height, width, ktype, threshold, pad) elif prior == 'OD': # the configs are fixed ksize = 8 imageHT = algorithms.orderedDith(image, height, width, ksize) else: print('Error ! Please choose your prior halftoning method correctly ! Options available = ED and OD .') image = algorithms.edbSearch(image, imageHT, height, width) tools.saveImage(image, idx, "EDBS") print(tools.psnr(np.uint8(source), np.uint8(image)))
def errorDiffusion(dirName, ktype, threshold): # # # parameter needed : directory, kernel type, threshold. # # directory : your dataset directory. # # ktype : kernel type, only accept two kind of parameters: 'floyd-steinberg' or 'jajuni' (jarvis, judice, ninke) # # threshold : your selected threshold. print("Processing for error diffusion !") listImgs = tools.loadDirectory(dirName) outDir = 'results' tools.createDir(outDir) for(idx, imgname) in enumerate(listImgs): image = tools.readImage(imgname) height, width = tools.getInfo(image) source = np.copy(image) if ktype == 'floyd-steinberg': pad = 1 elif ktype == 'jajuni': pad = 2 else: print("Error ! Please choose your kernel type correctly ! Options available = floyd-steinberg and jajuni . ") sys.exit(1) image = algorithms.errorDif(image, height, width, ktype, threshold, pad) tools.saveImage(image, idx, "ED") print(tools.psnr(np.uint8(source), np.uint8(image)))
def ddbtcoding(image, height, width, ktype): ordKernel = orderMat(ktype) kheight, kwidth = tools.getInfo(ordKernel) # handle location for the order map patLoc = np.zeros((kheight * kwidth, 2)) for m in range(0, kheight): for n in range(0, kwidth): patLoc[ordKernel[m][n]][0] = m patLoc[ordKernel[m][n]][1] = n patMaps = np.zeros((height, width)) # get weight kernel (error kernel) weightKernel = matErrorDD() # create a dummy array to help calculation dumArray = np.copy(image) for i in range(0, height, kheight): for j in range(0, width, kwidth): index = 0 tempImg = image[i:i+kheight,j:j+kwidth] mean = tools.getMean(tempImg) nMax = tools.getMax(tempImg) nMin = tools.getMin(tempImg) while index != (kheight * kwidth): ni = int(i + patLoc[index][0]) nj = int(j + patLoc[index][1]) if dumArray[ni][nj] > mean: dummy = nMax else: dummy = nMin error = dumArray[ni][nj] - dummy image[ni][nj] = dummy patMaps[ni][nj] = 1 fm = 0 for m in range(-1, 2): for n in range(-1, 2): if (ni + m >= 0) and (ni + m < height) and (nj + n >= 0) and (nj + n < width): if patMaps[ni + m][nj + n] == 0: fm = fm + weightKernel[m + 1][n + 1] for m in range(-1, 2): for n in range(-1, 2): if (ni + m >= 0) and (ni + m < height) and (nj + n >= 0) and (nj + n < width): if patMaps[ni + m][nj + n] == 0: dumArray[ni + m][nj + n] = dumArray[ni + m][nj + n] + (error * weightKernel[m + 1][n + 1] / fm) index = index + 1 return np.uint8(image)
def btc(dirName, bs): print("Processing for block truncation coding !") listImgs = tools.loadDirectory(dirName) outDir = 'results' tools.createDir(outDir) for(idx, imgname) in enumerate(listImgs): image = tools.readImage(imgname) height, width = tools.getInfo(image) source = np.copy(image) image = algorithms.btcoding(image, height, width, bs) tools.saveImage(image, idx, "BTC") print(tools.psnr(np.uint8(source), np.uint8(image)))
def orderedDithering(dirName, ksize): # # # parameter needed : directory, kernel type. # # directory : your dataset directory. # # ktype : kernel type, only accept two kind of parameters: 'bayer8dispersed' or 'bayer8clustered' print("Processing for ordered dithering !") listImgs = tools.loadDirectory(dirName) outDir = 'results' tools.createDir(outDir) for(idx, imgname) in enumerate(listImgs): image = tools.readImage(imgname) height, width = tools.getInfo(image) source = np.copy(image) image = algorithms.orderedDith(image, height, width, ksize) tools.saveImage(image, idx, "OD") print(tools.psnr(np.uint8(source), np.uint8(image)))
def ddbtc(dirName, ktype): # # # parameter needed : directory, kernel (order) type, threshold. # # directory : your dataset directory. # # ktype : kernel order type. print("Processing for dot diffusion block truncation coding !") listImgs = tools.loadDirectory(dirName) outDir = 'results' tools.createDir(outDir) for(idx, imgname) in enumerate(listImgs): image = tools.readImage(imgname) height, width = tools.getInfo(image) source = np.copy(image) image = algorithms.ddbtcoding(image, height, width, ktype) tools.saveImage(image, idx, "DDBTC") print(tools.psnr(np.uint8(source), np.uint8(image)))
def orderedDith(image, height, width, ksize): kernel = ditherArray(int(0), int(0), int(ksize), int(0), int(1)) kheight, kwidth = tools.getInfo(kernel) bs = kheight loopHeight = np.uint8(height / bs) loopWidth = np.uint8(width / bs) image = tools.normalize(image) for i in range(0, loopHeight): for j in range(0, loopWidth): tempImg = image[i*bs:i*bs+bs,j*bs:j*bs+bs] for x in range(0, bs): for y in range(0, bs): if image[i*bs+x][j*bs+y] >= kernel[x][y]: image[i*bs+x][j*bs+y] = 255 else: image[i*bs+x][j*bs+y] = 0 return np.uint8(image)