# Making an HTML table of pixel values with colored cells

Today's post shows you how to make a table with image colors and pixels appear when you publish your MATLAB scripts to HTML using the publish function. The result looks like this:

rgb = imread('peppers.png');
disp(im2html(rgb(88:92,200:204,:)))

 R:80G:50B:77 R:78G:47B:74 R:76G:47B:71 R:95G:65B:73 R:158G:129B:117 R:76G:47B:77 R:75G:46B:71 R:91G:63B:72 R:158G:130B:119 R:192G:165B:141 R:77G:45B:70 R:82G:51B:63 R:148G:120B:114 R:192G:166B:146 R:203G:176B:153 R:75G:43B:65 R:126G: 95B: 97 R:186G:160B:145 R:197G:173B:154 R:208G:180B:160 R:100G: 70B: 72 R:174G:146B:135 R:193G:169B:151 R:198G:175B:158 R:211G:189B:170

I was inspired to do something like this when I saw Printing Variables to HTML Tables in Published Code (by Ned) on the File Exchange a while back. It also produces an HTML table with colored cells and superimposed values.

I was also thinking about the Pixel Region Tool in the Image Processing Toolbox. Here's a screen shot:

I wanted to go a bit further than Ned's original. I wanted to handle all the different kinds of image types (grayscale, truecolor, indexed with direct mapping, indexed with scaled mapping). I also wanted to replicate the feature of the Pixel Region Tool that automatically changed the color of the superimposed text depending on whether the underlying pixel was dark or light. (You can see that effect in the screen shot above.)

I've packaged all this in a function called im2html. You can download this function from the File Exchange.

Here are some examples showing how to use im2html with different types of images.

Display a table of values for a gray-scale image:

I = imread('pout.tif');
disp(im2html(I(125:134, 104:114)))

 112 112 107 97 91 87 86 84 83 84 84 120 126 128 128 114 101 87 87 86 86 87 116 132 138 142 142 132 98 91 89 87 89 110 133 145 150 149 147 121 101 93 93 91 109 133 145 156 159 153 142 130 109 102 99 109 131 143 154 169 171 169 169 154 139 137 108 126 142 151 169 175 186 190 189 180 179 110 121 137 148 158 167 177 187 199 189 185 112 117 136 146 151 159 159 163 189 189 180 114 113 132 142 147 151 156 154 162 184 179
I = magic(10);
disp(im2html(I,[]))

 92 99 1 8 15 67 74 51 58 40 98 80 7 14 16 73 55 57 64 41 4 81 88 20 22 54 56 63 70 47 85 87 19 21 3 60 62 69 71 28 86 93 25 2 9 61 68 75 52 34 17 24 76 83 90 42 49 26 33 65 23 5 82 89 91 48 30 32 39 66 79 6 13 95 97 29 31 38 45 72 10 12 94 96 78 35 37 44 46 53 11 18 100 77 84 36 43 50 27 59

Display a table of values from an indexed image:

[X,map] = imread('trees.tif');
disp(im2html(X(156:160,244:248),map))

 <93>R:0.42G:0.68B:0.87 <93>R:0.42G:0.68B:0.87 <82>R:0.35G:0.65B:0.81 <77>R:0.39G:0.61B:0.81 <93>R:0.42G:0.68B:0.87 <82>R:0.35G:0.65B:0.81 <45>R:0.22G:0.45B:0.68 <50>R:0.42G:0.42B:0.52 <82>R:0.35G:0.65B:0.81 <82>R:0.35G:0.65B:0.81 <93>R:0.42G:0.68B:0.87 <50>R:0.42G:0.42B:0.52 <32>R:0.39G:0.29B:0.55 <44>R:0.52G:0.32B:0.52 <93>R:0.42G:0.68B:0.87 <93>R:0.42G:0.68B:0.87 <93>R:0.42G:0.68B:0.87 <44>R:0.52G:0.32B:0.52 <20>R:0.58G:0.13B:0.29 <27>R:0.45G:0.22B:0.42 <105>R:0.55G:0.74B:0.91 <93>R:0.42G:0.68B:0.87 <77>R:0.39G:0.61B:0.81 <44>R:0.52G:0.32B:0.52 <20>R:0.58G:0.13B:0.29

You can also capture the output of im2html as a string, or write it directly to a file.

s = im2html(magic(10),[]);

im2html(magic(10),[],'OutputFile','magic_table.html')


Give im2html a try. Comment here (or on the File Exchange page) if you find a good use for it, or if you have ideas about making it better.

Next time I'll go into some of the details about how im2html works, including the use of raw HTML in your publishable MATLAB scripts, as well as an obscure thing in the Image Processing Toolbox called imagemodel.

Published with MATLAB® 7.14

|