Commit cf35652d authored by Michael Murtaugh's avatar Michael Murtaugh

viewer code and tex/lex

parent eb16a3fa
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta name="generator" content="pandoc" />
<title></title>
<style type="text/css">code{white-space: pre;}</style>
</head>
<body>
<hr />
<p>/ /_____ ____ / / /_ ____ _ <strong> / </strong>/ __ / __ / / __ / __ | |/<em>/ / /</em>/ /<em>/ / /</em>/ / / /<em>/ / /</em>/ /&gt; &lt;<br />
_<em>/_</em><strong>/_</strong><em>/</em>/<em>.</em><strong>/_</strong><em>/</em>/|_|</p>
<p>your personal image processing tools!</p>
<h2 id="installing-building">Installing / Building</h2>
<p>This toolset is heterogeneous; some are written in C with bindings to libraries like opencv and tesseract, others are in the python scripting language and rely on python packages like numpy, python-opencv, pillow, and scipy.</p>
<h3 id="installing-python-dependencies">Installing Python dependencies</h3>
<p>For the python tools, it may be useful to create a &quot;virtualenv&quot;. Otherwise you might eventually have trouble with version conflicts with other tools. Option A describes this approach. Alternatively, you can install the packages in your system (option B).</p>
<h4 id="a.-installing-python-dependencies-in-a-venv">A. Installing Python dependencies in a venv</h4>
<ol style="list-style-type: decimal">
<li><p>Create one:</p>
<p>virtualenv venv</p></li>
<li><p>Activate it:</p>
<p>source venv/bin/activate</p></li>
<li><p>Use pip to install (nb you don't need sudo since you &quot;own&quot; the venv).</p>
<p>pip install scipy pillow numpy</p></li>
</ol>
<h4 id="b.-installing-python-dependencies-to-the-system">B. Installing Python dependencies to the system</h4>
<pre><code>sudo pip install scipy pillow numpy</code></pre>
<h2 id="usage">Usage</h2>
<h3 id="image-gradient">Image Gradient</h3>
<pre><code>python python/imagegradient.py foo.png --output foo.gradient.png --svg foo.gradient.svg</code></pre>
<p>Converts foo.png (input) into two outputs.</p>
</body>
</html>
Building
----------
The makefile uses pkg-config. You may eventually need to:
apt-get install pkg-config
then:
make
Usage
--------
./contours
#-------------------------------------------------
#
# Project created by QtCreator 2012-05-26T20:16:46
#
#-------------------------------------------------
QT += core
QT -= gui
TARGET = contours
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
CONFIG +=link_pkgconfig
PKGCONFIG += opencv
This diff is collapsed.
......@@ -13,11 +13,16 @@ int max_thresh = 305;
RNG rng(12345);
/// Function header
void thresh_callback(int, void* , char** argv);
void thresh_callback(int, void* , int, char** argv);
/** @function main */
int main( int argc, char** argv )
{
if (argc < 2) {
fprintf(stderr, "usage: contours IMAGE [output.png]\n");
return (0);
}
/// Load source image and convert it to gray
src = imread( argv[1], 1 );
......@@ -27,19 +32,19 @@ int main( int argc, char** argv )
blur( src_gray, src_gray, Size(3,3) );
blur( src_gray, src_gray, Size(3,3) );
char* source_window = "Source";
// char* source_window = "Source";
/// Create Window
// namedWindow( source_window, CV_WINDOW_AUTOSIZE );
// imshow( source_window, src );
thresh_callback( 0, 0, argv );
thresh_callback( 0, 0, argc, argv );
// waitKey(0);
return(0);
}
/** @function thresh_callback */
void thresh_callback(int, void* , char **argv)
void thresh_callback(int, void* , int argc, char **argv)
{
int totalpoints=0;
Mat canny_output;
......@@ -80,10 +85,15 @@ void thresh_callback(int, void* , char **argv)
// std::cout << "number of points in last contour: " << lastcontour << "\n";
}
}
std::cout << "</svg>";
std::cout << "</svg>\n";
int totalcontours=contours.size();
int ratio=totalpoints/totalcontours;
// printf("%d\n",ratio);
std::cerr << ratio << "\n";
imwrite(argv[2],drawing);
// std::cerr << ratio << "\n";
fprintf(stderr, "%d\n", ratio);
if (argc >= 3) {
imwrite(argv[2],drawing);
}
}
......@@ -50,7 +50,7 @@ parser.add_argument('input', help='an image path as input')
parser.add_argument('--format', default="hslimage", help='save output format, default hslimage (direction is hue, magnitude is lightness). Other options: magimage (magnitude image), json.')
parser.add_argument('--output', help='save to output path')
parser.add_argument('--width', type=int, default=None, help='resize width, default: None (no resize)')
parser.add_argument('--svg', default=False, action="store_true", help='output svg')
parser.add_argument('--svg', default=None, help='output svg path')
parser.add_argument('--svg-grid-size', type=int, default=10, help='density of svg arrows (default: 10)')
parser.add_argument('--svg-mag-scale', type=float, default=1.0, help='svg arrow magnitude scaler (default: 5.0)')
args = parser.parse_args()
......@@ -90,8 +90,7 @@ dirs = arctan2(imy, imx)
# pil_im.convert("LA").save("dirs.png")
if args.svg:
svgpath = out.replace(".png", ".svg")
svgfile = open(svgpath, 'w')
svgfile = open(args.svg, 'w')
print("""<svg version="1.1" baseProfile="full" width="{0[width]}" height="{0[height]}" xmlns="http://www.w3.org/2000/svg">
<defs
id="defs4">
......
%.gradient.png: %.jpg
python imagegradient/imagegradient.py --output $@ --svg $*.gradient.svg $<
all: README.html
%.html: %.md
pandoc --self-contained $< -o $@
# for cookbook
index.html: $(shell ls -d *)
indexical > $@
# gradient (jpg or png input, svg or png output)
%.gradient.png: %.jpg
python imagegradient/imagegradient.py --output $*.gradient.png --svg $*.gradient.svg $<
%.gradient.svg: %.jpg
python imagegradient/imagegradient.py --output $*.gradient.png --svg $*.gradient.svg $<
%.gradient.png: %.png
python imagegradient/imagegradient.py --output $@ --svg $*.gradient.svg $<
python imagegradient/imagegradient.py --output $*.gradient.png --svg $*.gradient.svg $<
%.gradient.svg: %.png
python imagegradient/imagegradient.py --output $*.gradient.png --svg $*.gradient.svg $<
# CONTOURS
%.contours.svg: %.png
contours/contours $< > $@
%.contours.svg: %.jpg
contours/contours $< > $@
%.contours.png: %.png
contours/contours $< $@ > $*.contours.svg
%.contours.png: %.jpg
contours/contours $< $@ > $*.contours.svg
# TEXTURE
%.texture.svg: %.png
texture/build/texture $< > $@
%.texture.svg: %.jpg
texture/build/texture $< > $@
# CHANNEL
%.red.png: %.png
python channel/channel.py --channel red --output $@ $<
%.red.png: %.jpg
python channel/channel.py --channel red --output $@ $<
%.green.png: %.png
python channel/channel.py --channel green --output $@ $<
%.green.png: %.jpg
python channel/channel.py --channel green --output $@ $<
%.blue.png: %.png
python channel/channel.py --channel blue --output $@ $<
%.blue.png: %.jpg
python channel/channel.py --channel blue --output $@ $<
# HOUGH LINES
%.hough.svg: %.jpg
houghlines/build/houghlines $< > $@
%.hough.svg: %.png
houghlines/build/houghlines $< > $@
# VIEWER
%.html: %.jpg
python viewer/makeviewer.py $< --output $@
......@@ -42,10 +42,11 @@ int main(int argc, char** argv)
tesseract::ResultIterator* ri = api->GetIterator();
tesseract::PageIteratorLevel level = tesseract::RIL_SYMBOL;
printf("<svg version=\"1.1\" baseProfile=\"full\" width=\"%d\" height=\"%d\" xmlns=\"http://www.w3.org/2000/svg\">\n", image->w, image->h);
printf("<svg version=\"1.1\" baseProfile=\"full\" width=\"%d\" height=\"%d\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:inkscape=\"http://www.inkscape.org/namespaces/inkscape\" >\n", image->w, image->h);
printf("<style>\n");
printf("/* <![CDATA[ */\n");
printf("rect { fill: none; stroke: #FF00FF; stroke-width: 1px; }\n");
printf("g.symbol rect { fill: none; stroke: #FF00FF; stroke-width: 1px; }\n");
printf("g.word rect { fill: none; stroke: #00FFFF; stroke-width: 1px; }\n");
printf("text { font-size: 12; font-family: sans-serif; fill: #000000; dominant-baseline: text-after-edge }\n");
printf("/* ]]> */\n");
printf("</style>\n");
......@@ -56,7 +57,7 @@ int main(int argc, char** argv)
int confidence_level = 0;
int j = 0;
printf("<g class=\"texturesymbols\">\n");
printf("<g class=\"texturesymbols\" id=\"symbols\" inkscape:label=\"symbols\" inkscape:groupmode=\"layer\">\n");
if (ri != 0) {
do {
const char* word = ri->GetUTF8Text(level);
......@@ -65,11 +66,12 @@ int main(int argc, char** argv)
ri->BoundingBox(level, &x1, &y1, &x2, &y2);
bool ignore;
int psize,fid;
ri->WordFontAttributes(&ignore,&ignore,&ignore,&ignore,&ignore,&ignore,&psize,&fid);
fprintf(stderr, "SYM:'%s', conf: %.2f; BoundingBox: %d,%d,%d,%d; psize: %d, font id: %d\n", word, conf, x1, y1, x2, y2, psize, fid);
if(conf>50) {
// printf(" <rect x=\"%d\" y=\"%d\" width=\"%d\" height=\"%d\"/>\n", x1, y1, (x2-x1), (y2-y1));
printf(" <g class=\"symbol\" transform=\"translate(%d,%d)\"><rect x=\"0\" y=\"0\" width=\"%d\" height=\"%d\"/><text x=\"0\" y=\"%d\" style=\"font-size: %dpx\"><![CDATA[%s]]></text></g>\n", x1, y1, (x2-x1), (y2-y1), 0, psize, word);
printf(" <g class=\"symbol\" transform=\"translate(%d,%d)\"><rect x=\"0\" y=\"0\" width=\"%d\" height=\"%d\"/><text x=\"0\" y=\"%d\" style=\"font-size: %dpx\"><![CDATA[%s]]></text></g>\n", x1, y1, (x2-x1), (y2-y1), (y2-y1), psize/2, word);
// printf(" <g class=\"word\" transform=\"translate(%d,%d)\"><rect x=\"0\" y=\"0\" width=\"%d\" height=\"%d\"/><text x=\"0\" y=\"0\"><![CDATA[%s]]></text></g>\n", x1, y1, (x2-x1), (y2-y1), word);
confidence_level+=conf;
j++;
......@@ -83,6 +85,32 @@ int main(int argc, char** argv)
// printf("num symbols: %d \n",j);
// printf("confidence level: %d \n",confidence_level);
ri = api->GetIterator();
level = tesseract::RIL_WORD;
printf("<g class=\"texturesymbols\" id=\"words\" inkscape:label=\"words\" inkscape:groupmode=\"layer\">\n");
if (ri != 0) {
do {
const char* word = ri->GetUTF8Text(level);
float conf = ri->Confidence(level);
int x1, y1, x2, y2;
ri->BoundingBox(level, &x1, &y1, &x2, &y2);
bool ignore;
int psize,fid;
ri->WordFontAttributes(&ignore,&ignore,&ignore,&ignore,&ignore,&ignore,&psize,&fid);
fprintf(stderr, "WORD:'%s', conf: %.2f; BoundingBox: %d,%d,%d,%d; psize: %d, font id: %d\n", word, conf, x1, y1, x2, y2, psize, fid);
if(conf>50) {
// printf(" <rect x=\"%d\" y=\"%d\" width=\"%d\" height=\"%d\"/>\n", x1, y1, (x2-x1), (y2-y1));
printf(" <g class=\"word\" transform=\"translate(%d,%d)\"><rect x=\"0\" y=\"0\" width=\"%d\" height=\"%d\"/><text x=\"0\" y=\"%d\" style=\"font-size: %dpx\"><![CDATA[%s]]></text></g>\n", x1, y1, (x2-x1), (y2-y1), (y2-y1), psize/2, word);
// printf(" <g class=\"word\" transform=\"translate(%d,%d)\"><rect x=\"0\" y=\"0\" width=\"%d\" height=\"%d\"/><text x=\"0\" y=\"0\"><![CDATA[%s]]></text></g>\n", x1, y1, (x2-x1), (y2-y1), word);
confidence_level+=conf;
j++;
}
delete[] word;
} while (ri->Next(level));
}
printf("</g>\n");
api->End();
// delete [] outText;
pixDestroy(&image);
......
(function () {
/*
todo: reordering possibilities!
*/
function toa (x) {
return Array.prototype.slice.apply(x);
}
function layers (control_elt, layer_elts) {
var elt = control_elt,
layer_id = 0;
toa(layer_elts).forEach(function (imgelt) {
// console.log("*", imgelt);
var div = document.createElement("div"),
input = document.createElement("input"),
label = document.createElement("label"),
lid = "aalayercntrl" + (++layer_id),
name = imgelt.getAttribute("class");
elt.appendChild(div);
div.appendChild(input);
div.appendChild(label);
input.setAttribute("type", "checkbox");
input.setAttribute("id", lid);
label.setAttribute("for", lid);
label.innerHTML = name;
var s = getComputedStyle(imgelt);
var hidden = (s.display == "none");
input.checked = !hidden;
input.addEventListener("change", function () {
if (this.checked) {
imgelt.style.display = "block";
} else {
imgelt.style.display = "none";
}
});
// console.log("s", s.display);
});
}
window.layers = layers;
})();
#!/usr/bin/env python
from __future__ import print_function
from jinja2 import Environment, FileSystemLoader
import os
from argparse import ArgumentParser
ap = ArgumentParser("")
ap.add_argument("input")
ap.add_argument("--template", default="viewer/viewer.html")
ap.add_argument("--output", default=None)
args = ap.parse_args()
outpath = None
original = args.input
if args.output:
outpath, _ = os.path.split(args.output)
original = os.path.relpath(original, outpath)
basename, _ = os.path.splitext(original)
env = Environment(loader=FileSystemLoader("."))
t = env.get_template(args.template)
if args.output:
with open(args.output, "w") as f:
print (t.render(original=original, basename=basename).encode("utf-8"), file=f)
else:
print (t.render(original=original, basename=basename).encode("utf-8"))
<!DOCTYPE html>
<html>
<head>
<title>{{basename}}</title>
<meta charset="utf-8">
<style>
#image {
position: relative;
}
#image img.original {
position: static;
}
#image img {
position: absolute;
left: 0; top: 0;
}
#image .layers {
position: fixed;
right: 10px;
top: 10px;
z-index: 1000;
background: #EEE;
border: 1px dashed black;
padding: 5px;
}
</style>
</head>
<body>
<div id="image">
<div class="layers"></div>
<img class="original" src="{{original}}" />
<img class="red" src="{{basename}}.red.png" style="display: none" />
<img class="green" src="{{basename}}.green.png" style="display: none" />
<img class="blue" src="{{basename}}.blue.png" style="display: none" />
<img class="gradient" src="{{basename}}.gradient.svg" style="display: none" />
<img class="contours" src="{{basename}}.contours.svg" style="display: none" />
<img class="houghlines" src="{{basename}}.hough.svg" style="display: none" />
<img class="texture" src="{{basename}}.texture.svg" style="display: none" />
</div>
<script src="../viewer/layers.js"></script>
<script>
layers(document.querySelector("#image .layers"), document.querySelectorAll("#image img"));
</script>
</body>
</html>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment