Welcome, guest | Sign In | My Account | Store | Cart

Improved Gray Scale (IGS) codes are used for the elimination of false contouring in images, and image compression. This Python program generates the IGS codes for a set of input gray level values.

Python, 127 lines
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
"""
Improved Gray Scale (IGS) Quantization implementation
IGS codes are used for the elimination of false contouring in images,
and image compression.
This Python program generates the IGS codes for a set of input
gray level values.

(c) 2003 Premshree Pillai (24/12/03)
http://www.qiksearch.com/
"""

import sys

def dec2bin(num):
	out = []
	while(num != 0):
		out.append(num % 2)
		num = num / 2
	out = pad(out)
	return out

def pad(the_list):
	while(len(the_list) != pix_depth):
		the_list.append(0)
	return the_list

def bin2dec(the_list):
	sum = 0
	i = 0
	while(i < len(the_list)):
		sum = sum + pow(2,i) * the_list[i]
		i = i + 1
	return sum

def rev(the_list):
	i = 0
	while(i < len(the_list)/2):
		temp = the_list[i]
		the_list[i] = the_list[len(the_list) - i - 1]
		the_list[len(the_list) - i - 1] = temp
		i = i + 1
	return the_list

def extractHigherBits(the_list):
	out = []
	i = 0
	while(len(out) != igs_len):
		out.append(the_list[len(the_list) - 1 - i])
		i = i + 1
	return(rev(out))

def extractLowerBits(the_list):
	out = []
	i = 0
	while(len(out) != igs_len):
		out.append(the_list[i])
		i = i + 1
	return(out)

def add(list1,list2):
	out = []
	carry = 0
	i = 0
	while(i < len(list1)):
		out.append(list1[len(list1) - 1 - i] ^ list2[len(list1) - 1 - i] ^ carry)
		if(list1[len(list1) - 1 - i] == list2[len(list1) - 1 - i] == 1):
			carry = 1
		else:
			carry = 0
		i = i + 1
	return rev(out)

def allOnes(the_list):
	if(0 in the_list):
		return 0
	else:
		return 1
	
def main():
	global pix_depth,igs_len
	pix_depth = int(raw_input("Enter pixel depth (i.e., bits per pixel): "))
	igs_len = pix_depth / 2
	num_pixels = int(raw_input("Enter number of pixels: "))
	pixels = []
	igs = []
	i = 0
	while(len(pixels) != num_pixels):
		print "Enter pixel ",(i + 1),":"
		pixels.append(int(raw_input()))
		if(pixels[i] > pow(2,pix_depth) - 1):
			print "With a pixel depth of", pix_depth,", maximum allowable gray level is", pow(2,pix_depth) - 1
			print "Please run the program again!"
			sys.exit()
		pixels[i] = dec2bin(pixels[i])
		i = i + 1
	pixels2 = []
	pixels2 = pixels
	sum = []
	sum = pad(sum)
	sum = pixels[0]
	sum = rev(sum)
	igs.append(extractLowerBits(sum))
	i = 1
	while(len(igs) != num_pixels):
		toAdd = rev(pad(extractLowerBits(rev(sum))))
		if(not(allOnes(extractHigherBits(pixels2[i - 1])))):
			sum = add(rev(pixels[i]),toAdd)
		else:
			sum = rev(pixels[i])
		igs.append(extractLowerBits(sum))
		i = i + 1
	j = 0
	print "\nDecimal\t\tGray Level\t\t\tIGS Code"
	print "-------------------------------------------------------------"
	while(j < len(igs)):
		if(j == 0):
			num = bin2dec(pixels[j])
		else:
			num = bin2dec(rev(pixels[j]))
		print num, "\t\t", rev(pixels[j]), "\t", igs[j]
		j = j + 1

main()

print "\nPress <enter> to exit..."
if(raw_input()):
	exit

This program works by first converting the input pixels (which are in decimal) into a binary number. The number of digits in the binary number is equal to the pixel depth (i.e., bits per pixel). Next, we initialize a variable, sum, to all zeroes. The first input pixel (in the binary form) is added to the sum. The higher order bits of the sum gives the IGS code of that pixel. The lower order bits of each sum is added to the next gray level to give the next sum. However, if the higher order bits of the gray level are all zeroes, nothing is added. Thus, the IGS code for each pixel is generated from the higher order bits of each sum.