A slightly off-the-wall approach... you could readily save the Mat as an image in OpenCV - preferably a PGM
or a PNG
since they are lossless. Then you could pass the image to a vector-tracer program like potrace
and get it to tell you the outline in SVG format and store that in your database.
So, potrace
likes PGM
files, so you either save your outline as a PGM
in OpenCV or as a PNG
, then you use ImageMagick to make that into a PGM and pass it to potrace
like this:
convert OpenCVImage.png pgm:- | potrace - -b svg -o file.svg
which will get you an svg
file like this:
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg version="1.0" xmlns="http://www.w3.org/2000/svg"
width="3486.000000pt" height="4747.000000pt" viewBox="0 0 3486.000000 4747.000000"
preserveAspectRatio="xMidYMid meet">
<metadata>
Created by potrace 1.13, written by Peter Selinger 2001-2015
</metadata>
<g transform="translate(0.000000,4747.000000) scale(0.100000,-0.100000)"
fill="#000000" stroke="none">
<path d="M0 23735 l0 -23735 17430 0 17430 0 0 23735 0 23735 -17430 0 -17430
0 0 -23735z m20980 6560 l0 -3415 -399 0 c-293 0 -402 3 -407 12 -7 11 -68 11
-2391 -9 l-781 -6 -6 -6576 c-3 -3617 -9 -6840 -12 -7163 l-6 -588 -1939 0
-1939 0 0 10580 0 10580 3940 0 3940 0 0 -3415z"/>
</g>
</svg>
You can view that in web-browser, by the way.
You can recall the image at any time and re-create it with ImageMagick, or other tools, at the command line like this:
convert outline.svg outline.png
I would note that your entire PNG
is actually only 32kB and storage is pretty cheap so it hardly seems worth the trouble to generate a vectorised image to save space. In fact, if you use a decent tool like ImageMagick and convert your image to a single bit PNG, it comes down to 6,150 bytes which is pretty small...
convert YourBigInefficientOutline.png NiceImageMagickOutlineOf6kB.png
And, if you can handle reducing the outline in size to 1/5th of its original, which would still probably be adequate to locate the newspaper article, you could do:
convert YourBig.png -resize 700x900 MySmall.png
which weighs in at just 1,825 bytes.