(9 intermediate revisions by the same user not shown)
Line 5: Line 5:
  
  
<center><font size= 4>Implementation of Quantization</font size>
+
<center><font size= 4>Implementation of Color Quantization</font size>
  
 
A [http://www.projectrhea.org/learning/slectures.php slecture] by [[ECE]] student Yitong Wang  
 
A [http://www.projectrhea.org/learning/slectures.php slecture] by [[ECE]] student Yitong Wang  
Line 13: Line 13:
 
----
 
----
 
----
 
----
==1. Introduction (''Implementation of Quantization'')==
+
==1. Introduction (''Implementation of Color Quantization'')==
Quantization is an operation to compress a signal by reducing a range of values to a single value. This slecture will describe the implementation of image quantization
+
Quantization is an operation to compress a signal by reducing a range of values to a single value. Color quantization is a process to reduce the number of colors in an image in order to reduce the size. This slecture will describe the color quantization using kmean function in matlab. As we studied in lab9, there are usually 3 color channels in image, which are red, green and blue. We can operate color quantization separately on these 3 channels, then reform the image.
<math> f(x)= \frac{1}{5} \sin x \int_{-\infty}^\alpha \pi^y dy</math>
+
 
  
 
----
 
----
 
==2. Background ==
 
==2. Background ==
*The brightness of a photo at each pixel is typically distributed among integers from 0 to 255. However, since some images are desired to take only several values, the brightness value has to be rounded off to fit the size of the image.  
+
*The brightness of a photo at each pixel is typically distributed among integers from 0 to 255. However, since some images are desired to limit the number of colors, the brightness value has to be rounded off to fit the size of the image.
 +
*During the procedure of quantization, there must be some loss of data. To avoid high error percentage and remain quality as good as possible, using kmean function is a good choice.
  
*Since the quantization will obviously cause loss of data, we should try the quantization in different levels and the analyse the error.
+
*Max quantizier
 
==3. Theory ==
 
==3. Theory ==
To operate an uniform quantization, the first things is to determine the uniform quantization step Δ.
+
*[ind,color] = kmean(input,k) function performs k-means clustering to partition the observations of the n-by-m data matrix input into k clusters, and returns an n-by-1 vector which represent the index of the values of colors after operation. The operation uses the squared Euclidean distance measure.
 +
*To start the operation, the image have to be reshaped into a X-3 matrix that contains R,G and B as we used in lab 5. Where X is the number of pixels in each row.  
  
<math> Δ = \frac{Max(X) - Min(X)}{N-1} </math>
+
%Seperate the image matrix into R G and B
  
Where X is the signal to be quantized and N is the number of quantization levels.
+
R = rgb(:,:,1);
The quantization step Δ is the range of values will to be quantized. Since the input image is not type double, it can't be processed in mathematical operation in Matlab. It should be converted to type double.
+
  
<math> X = double(input) </math>
+
G = rgb(:,:,2);
  
After this, we can do the quantization.
+
B = rgb(:,:,3);
  
Y = round((X-min(X(:)))/delta).*delta + min(X(:));
+
%reform a image matrix in size-3 matrix
 +
 
 +
img(:,1) = R(:);
 +
 
 +
img(:,2) = G(:);
 +
 
 +
img(:,3) = B(:);
 +
 
 +
*Operate the kmean function to get the quantize reference value
 +
 
 +
[ind,Color] = kmean(double(imag),N)
 +
 
 +
%Where N is the number of colors you want to keep in image
 +
 
 +
*Map the image into the closest reference value
 +
 
 +
imgQ = pdist2(img,Color) %Pairwise distance between original image and quantized reference
 +
 
 +
[minQ,indmin] = min(imgQ,[],2);%Target the location of the closest distance.
 +
 
 +
New = C(indmin,:) % map the image to the closest reference value
 +
 
 +
*reform the image
 +
 
 +
s = size(rgb);
 +
 
 +
s1 = s(1);
 +
 
 +
s2 = s(2);% record the size of original image
 +
 
 +
i(:,:,1)=reshape(New(:,1),s1,s2);
 +
 
 +
i(:,:,2)=reshape(New(:,2),s1,s2);
 +
 
 +
i(:,:,3)=reshape(New(:,3),s1,s2);%reshape the vector into m-n-p matrix as the original image
 +
 
 +
i = uint8(i);%convert to uint8 type
 +
 
 +
image(i);%print
  
 
----
 
----
[[Image:nature.jpg]]
+
[[File:///home/shay/b/wang1368/lab9b/minip.jpg|thumbnail]]
 +
 
 +
Here is the 4 color quantization compared with the original image. The girl's can still be distinguished but not that clear because there are only 4 colors.
  
 
----
 
----
==4. Conclusion (''Replace by appropriate section title'')==
+
==4. Conclusion ==
Text of fourth section goes here.
+
Color Quantization is commonly used in image processing. We studied Uniform Quantizer and Max Quantizer in lab 5. But this quantization performs better than just operating Max Quantizer or Uniform Quantizer on image. And it's not that hard.  In this matlab function, however, minQ is not necessary. It would be the reason why the function perform slowly if the N became too large.  
 
----
 
----
 
==5. References==
 
==5. References==
*Reference 1
+
*Purdue University,  "ECE438 - Digital Signal Processing with Applications1ECE438 - Laboratory 10:Image Processing," Purdue University October 6, 2010.
*reference 2
+
*Mireille Boutin, "ECE438 Digital Signal Processing with Applications," Purdue University August 26,2009.
 +
*[ONLINE] Available at: https://en.wikipedia.org/wiki/Color_quantization [Accessed 29 November 2015].
 +
 
 
----
 
----
 
----
 
----

Latest revision as of 21:58, 29 November 2015


Implementation of Color Quantization

A slecture by ECE student Yitong Wang

Partly based on the ECE438 Fall 2015 lecture material of Boutin.



1. Introduction (Implementation of Color Quantization)

Quantization is an operation to compress a signal by reducing a range of values to a single value. Color quantization is a process to reduce the number of colors in an image in order to reduce the size. This slecture will describe the color quantization using kmean function in matlab. As we studied in lab9, there are usually 3 color channels in image, which are red, green and blue. We can operate color quantization separately on these 3 channels, then reform the image.



2. Background

  • The brightness of a photo at each pixel is typically distributed among integers from 0 to 255. However, since some images are desired to limit the number of colors, the brightness value has to be rounded off to fit the size of the image.
  • During the procedure of quantization, there must be some loss of data. To avoid high error percentage and remain quality as good as possible, using kmean function is a good choice.
  • Max quantizier

3. Theory

  • [ind,color] = kmean(input,k) function performs k-means clustering to partition the observations of the n-by-m data matrix input into k clusters, and returns an n-by-1 vector which represent the index of the values of colors after operation. The operation uses the squared Euclidean distance measure.
  • To start the operation, the image have to be reshaped into a X-3 matrix that contains R,G and B as we used in lab 5. Where X is the number of pixels in each row.

%Seperate the image matrix into R G and B

R = rgb(:,:,1);

G = rgb(:,:,2);

B = rgb(:,:,3);

%reform a image matrix in size-3 matrix

img(:,1) = R(:);

img(:,2) = G(:);

img(:,3) = B(:);

  • Operate the kmean function to get the quantize reference value

[ind,Color] = kmean(double(imag),N)

%Where N is the number of colors you want to keep in image

  • Map the image into the closest reference value

imgQ = pdist2(img,Color) %Pairwise distance between original image and quantized reference

[minQ,indmin] = min(imgQ,[],2);%Target the location of the closest distance.

New = C(indmin,:) % map the image to the closest reference value

  • reform the image

s = size(rgb);

s1 = s(1);

s2 = s(2);% record the size of original image

i(:,:,1)=reshape(New(:,1),s1,s2);

i(:,:,2)=reshape(New(:,2),s1,s2);

i(:,:,3)=reshape(New(:,3),s1,s2);%reshape the vector into m-n-p matrix as the original image

i = uint8(i);%convert to uint8 type

image(i);%print


Here is the 4 color quantization compared with the original image. The girl's can still be distinguished but not that clear because there are only 4 colors.


4. Conclusion

Color Quantization is commonly used in image processing. We studied Uniform Quantizer and Max Quantizer in lab 5. But this quantization performs better than just operating Max Quantizer or Uniform Quantizer on image. And it's not that hard. In this matlab function, however, minQ is not necessary. It would be the reason why the function perform slowly if the N became too large.


5. References

  • Purdue University, "ECE438 - Digital Signal Processing with Applications1ECE438 - Laboratory 10:Image Processing," Purdue University October 6, 2010.
  • Mireille Boutin, "ECE438 Digital Signal Processing with Applications," Purdue University August 26,2009.
  • [ONLINE] Available at: https://en.wikipedia.org/wiki/Color_quantization [Accessed 29 November 2015].


Questions and comments

If you have any questions, comments, etc. please post them here.


Back to 2015 Fall ECE 438 Boutin


Alumni Liaison

Basic linear algebra uncovers and clarifies very important geometry and algebra.

Dr. Paul Garrett