There is any function for image cropping basing on svg annotation in Cairo python? - opencv

I have JPG images and with inputsvgdraw, a flash tool for image annotation (http://www.mainada.net/inputdraw).
As Gimp or Picasa, when i crop an image and i save as new image only the cropped part. I need a function that take svg path (representing new image bourders) as parameter and then basing on that, create the new image.
svg data:
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 488 325"><g fill="none" stroke-miterlimit="6" stroke-linecap="round" stroke-linejoin="round"><path d="M 307 97 l 0 -1 l -2 -1 l -10 -2 l -20 -1 l -25 5 l -22 9 l -10 9 l 0 9 l 2 12 l 16 18 l 25 11 l 25 5 l 17 -1 l 6 -4 l 3 -7 l -1 -12 l -6 -16 l -7 -13 l -11 -12 l -11 -14 l -9 -5" opacity="1" stroke="rgb(170,37,34)" stroke-width="5"/></g></svg>.
How can i extract and save in a new image inside circle part image? There is any library that handle this?
There is a way convert these datas for a function in a image library in python and cropping image?

Related

Object does not exist in the pdf file structure

%PDF-1.5
...
10737 0 obj
<</MarkInfo<</Marked true>>/Metadata 161 0 R/PageLayout/OneColumn/Pages 10732 0 R/StructTreeRoot 206 0 R/Type/Catalog>>
endobj
10738 0 obj
<</Contents[10740 0 R 10741 0 R 10747 0 R 10748 0 R 10749 0 R 10750 0 R 10751 0 R 10752 0 R]/CropBox[0.0 0.0 516.0 728.64]/MediaBox[0.0 0.0 516.0 728.64]/Parent 10733 0 R/Resources<</ColorSpace<</CS0 10771 0 R/CS1 10772 0 R>>/ExtGState<</GS0 10773 0 R>>/Font<</C2_0 10778 0 R/C2_1 10783 0 R/C2_2 10788 0 R/C2_3 10793 0 R/C2_4 10798 0 R/TT0 10800 0 R/TT1 10802 0 R/TT2 10804 0 R/TT3 10806 0 R/TT4 10808 0 R>>/XObject<</Im0 10769 0 R>>>>/Rotate 0/StructParents 0/Tabs/S/Type/Page>>
endobj
10739 0 obj
<</Filter/FlateDecode/First 410/Length 3756/N 38/Type/ObjStm>>stream
10771 0 10772 21 10773 42 10774 138 10775 190 10776 442 10777 741 10778 752 10779 869 10780 921 10781 1190 10782 2050 10783 2061 10784 2192 10785 2244 10786 2504 10787 3456 10788 3467 10789 3587 10790 3639 10791 3903 10792 6058 10793 6069 10794 6196 10795 6248 10796 6507 10797 8153 10798 8164 10799 8284 10800 8496 10801 9662 10802 9894 10803 11072 10804 11325 10805 11779 10806 11985 10807 13147 10808 13395
[/ICCBased 10753 0 R][/ICCBased 10754 0 R]
<</AIS false/BM/Normal/CA 1.0/OP false/OPM 1/SA true/SMask/None/Type/ExtGState/ca 1.0/op false>>
<</Ordering(Identity)/Registry(Adobe)/Supplement 0>><</Ascent 858/CIDSet 10757 0 R/CapHeight 719/Descent -148/Flags 4/FontBBox[-16 -148 1008 858]/FontFamily(\xfe\xff\x00H\x00Y\xc9\x11\xac\xe0\xb5\x15)/FontFile2 10758 0 R/FontName/YDRADB+H2gtrM/FontStretch/Normal/FontWeight 400/ItalicAngle 0/StemV 60/Type/FontDescriptor/XHeight 520>>
...
endstream
endobj
...
No. - Type
10732 - Pages
206 - StructTreeRoot
10771, 10772, 10773, 10778 ... - Font
Many indirect objects including 10732, 206, 10771 and 10772 do not exist in the pdf file.
But I think I found objects 10771~10808 in object 10739 stream.
Q1. Why are there no object 10732(Pages) and 206(StructTreeRoot) in the pdf file?
Q2. Why are indirect objects in stream?
I would be grateful if you would suggest any explanations or resources for reference.
Starting with version 1.5 PDF supports so called object streams, i.e. stream objects which contain other non-stream objects.
Your object 10739 is such an object stream as you can see in its Type ObjStm.
This allows those other objects to be compressed. In particular structure tree objects which otherwise can substantially increase the size of a PDF, can be compressed fairly well, reducing their impact on the document size.
For details please study the PDF specification, section 7.5.7 – Object Streams, in either the current PDF specification ISO 32000-2 or its predecessor ISO 32000-1.
Adobe has shared a copy of ISO 32000-1 on their web site which merely has its ISO page headers replaced. Simply google for "PDF32000_2008"; currently it is located at https://www.adobe.com/content/dam/acom/en/devnet/pdf/pdfs/PDF32000_2008.pdf but as far as I know this isn't a permalink.

Parse input text that is located using left padding spaces

I have the following text structure. The values below JTT JNX JNA JNO belong to previous line.
9 8 11 56507785 93
JTT JNX JNA JNO
76 98
9 8 60 3269557 58
9 8 53 7269558 150
JTT JNX JNA JNO
132 71 45-7705678
9 8 62 439559 82
I'd like to parse it in order to print the corresponding values in a single line like below:
H1 H2 H3 H4 H5 JTT JNX JNA JNO
9 8 11 56507785 93 76 98
9 8 60 3269557 58
9 8 53 7269558 150 132 71 45-7705678
9 8 62 439559 82
My issue is when I use awk with FS = space (default FS) then it takes JTT as first field and JTT has 9 spaces before, so I think should be use some technique that counts how may spaces are from left until JTT JNX JNA JNO and count number of spaces from beginning until the values below JTT JNX JNA JNO in order to positionate correctly each value. I mean, 76 and 132 below JTT header, 971 below JNX, 98 below JNA and 45-7705678 below JNO.
How can this be done in awk?
$ awk --version
GNU Awk 5.0.0, API: 2.0 (GNU MPFR 4.0.2, GNU MP 6.1.2)
Copyright (C) 1989, 1991-2019 Free Software Foundation.
$ uname -srv
CYGWIN_NT-6.1 3.0.7(0.338/5/3) 2019-04-30 18:08
Thanks in advance.
With GNU awk (which you have) for FIELDWIDTHS:
$ cat tst.awk
BEGIN {
OFS = ","
print "H1", "H2", "H3", "H4", "H5", "JTT", "JNX", "JNA", "JNO"
}
!NF || ($1 == "JTT") { next }
!/^ / {
if (NR>1) {
print rec
}
FS = " "
$0 = $0
$1 = $1
rec = $0
}
/^ / {
FIELDWIDTHS = "12 5 5 *"
$0 = $0
$1 = $1
for (i=1; i<=NF; i++) {
gsub(/^\s+|\s+$/,"",$i)
}
rec = rec OFS $0
}
END {
print rec
}
.
$ awk -f tst.awk file
H1,H2,H3,H4,H5,JTT,JNX,JNA,JNO
9,8,11,56507785,93,76,,98
9,8,60,3269557,58
9,8,53,7269558,150,132,71,,45-7705678
9,8,62,439559,82
$ awk -f tst.awk file | column -s, -t
H1 H2 H3 H4 H5 JTT JNX JNA JNO
9 8 11 56507785 93 76 98
9 8 60 3269557 58
9 8 53 7269558 150 132 71 45-7705678
9 8 62 439559 82
Replace OFS="," with OFS="\t" or otherwise massage to suit...

Getting a 2D histogram of a grayscale image in Julia

Using the Images package, I can open up a color image, convert it to Gray scale and then :
using Images
img_gld = imread("...path to some color jpg...")
img_gld_gs = convert(Image{Gray},img_gld)
#change from floats to Array of values between 0 and 255:
img_gld_gs = reinterpret(Uint8,data(img_gld_gs))
Now I've got a 1920X1080 array of Uint8's:
julia> img_gld_gs
1920x1080 Array{Uint8,2}
Now I want to get a histogram of the 2D array of Uint8 values:
julia> hist(img_gld_gs)
(0.0:50.0:300.0,
6x1080 Array{Int64,2}:
1302 1288 1293 1302 1297 1300 1257 1234 … 12 13 13 12 13 15 14
618 632 627 618 623 620 663 686 189 187 187 188 185 183 183
0 0 0 0 0 0 0 0 9 9 8 7 8 7 7
0 0 0 0 0 0 0 0 10 12 9 7 13 7 9
0 0 0 0 0 0 0 0 1238 1230 1236 1235 1230 1240 1234
0 0 0 0 0 0 0 0 … 462 469 467 471 471 468 473)
But, instead of 6x1080, I'd like 256 slots in the histogram to show total number of times each value has appeared. I tried:
julia> hist(img_gld_gs,256)
But that gives:
(2.0:1.0:252.0,
250x1080 Array{Int64,2}:
So instead of a 256x1080 Array, it's 250x1080. Is there any way to force it to have 256 bins (without resorting to writing my own hist function)? I want to be able to compare different images and I want the histogram for each image to have the same number of bins.
Assuming you want a histogram for the entire image (rather than one per row), you might want
hist(vec(img_gld_gs), -1:255)
which first converts the image to a 1-dimensional vector. (You can also use img_gld_gs[:], but that copies the data.)
Also note the range here: the hist function uses a left-open interval, so it will omit counting zeros unless you use something smaller than 0.
hist also accepts a vector (or range) as an optional argument that specifies the edge boundaries, so
hist(img_gld_gs, 0:256)
should work.

I have inserted an image into pdf digital signature layer n2 using mupdf, but it not showed up

I am using mupdf(git clone at commit 2014/3/5 4:05) to sign a pdf.
And I've already succeeded to sign a digital signature in pdf with function 'pdf_sign_signature'.
Now I'm trying to insert an image into layer n2 of that signature when I'm signing it.
There's a function called 'pdf_set_signature_appearance' in '/source/pdf/pdf-appearance.c'.
I try to add codes below to insert the image:
image_doc = (image_document *)fz_open_document(ctx, jpgFilePathAndName);
fz_fill_image(dev, image_doc->image, page_ctm, 1.0f);
these codes are inserted between:
rect = annot->rect;
rect.x0 = (rect.x0 + rect.x1)/2.0f;
text = fit_text(ctx, &font_rec, (char *)bufstr, &rect);
fz_fill_text(dev, text, page_ctm, cs, font_rec.da_rec.col, 1.0f);
and
rect = annot->rect;
fz_transform_rect(&rect, page_ctm);
pdf_set_annot_appearance(doc, annot, &rect, dlist);
After executing, I got a pdf file like this:
%PDF-1.4
%μῦ
1 0 obj
<<
/Type /Font
/BaseFont /Helvetica
/Subtype /Type1
/Encoding /WinAnsiEncoding
>>
endobj
2 0 obj
<<
/Length 63
>>
stream
q
BT
36 806 Td
0 -18 Td
/F1 12 Tf
(Hello World!)Tj
0 0 Td
ET
Q
endstream
endobj
3 0 obj
<<
/Count 1
/Type /Pages
/ITXT (5.3.0)
/Kids [ 4 0 R ]
>>
endobj
4 0 obj
<<
/Annots 10 0 R
/Contents 2 0 R
/MediaBox [ 0 0 595 842 ]
/Parent 3 0 R
/Resources <<
/Font <<
/F1 1 0 R
>>
/ProcSet [ /PDF /Text /ImageB /ImageC /ImageI ]
>>
/Type /Page
>>
endobj
5 0 obj
<<
/AcroForm 8 0 R
/Metadata 9 0 R
/Pages 3 0 R
/Type /Catalog
>>
endobj
6 0 obj
<<
/CreationDate (D:20120620091727+02'00')
/ModDate (D:20120804153425+02'00')
/Producer (iText\256 5.3.0 \2512000-2012 1T3XT BVBA)
>>
endobj
7 0 obj
<<
/AP <<
/N 17 0 R
>>
/DA (/Helv 0 Tf 0 g)
/F 4
/FT /Sig
/MK <<
>>
/P 4 0 R
/Rect [ 41.3670998 713.710022 237.352997 781.257996 ]
/Subtype /Widget
/T (Signature1)
/Type /Annot
/V 15 0 R
>>
endobj
8 0 obj
<<
/DA (/Helv 0 Tf 0 g )
/DR <<
/Encoding <<
/PDFDocEncoding 14 0 R
>>
/Font <<
/Helv 12 0 R
/ZaDb 13 0 R
>>
/XObject <<
/DSz 11 0 R
>>
>>
/Fields [ 7 0 R ]
/SigFlags 1
>>
endobj
9 0 obj
<<
/Length 3304
/Subtype /XML
/Type /Metadata
>>
stream
<?xpacket begin="Ôªø" id="W5M0MpCehiHzreSzNTczkc9d"?>
<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 4.2.1-c043 52.372728, 2009/01/18-15:08:04 ">
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description rdf:about=""
xmlns:xmp="http://ns.adobe.com/xap/1.0/">
<xmp:CreateDate>2012-06-20T09:17:27+02:00</xmp:CreateDate>
<xmp:ModifyDate>2012-08-04T15:34:25+02:00</xmp:ModifyDate>
<xmp:MetadataDate>2012-08-04T15:34:25+02:00</xmp:MetadataDate>
</rdf:Description>
<rdf:Description rdf:about=""
xmlns:pdf="http://ns.adobe.com/pdf/1.3/">
<pdf:Producer>iText® 5.3.0 ©2000-2012 1T3XT BVBA</pdf:Producer>
</rdf:Description>
<rdf:Description rdf:about=""
xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:format>application/pdf</dc:format>
</rdf:Description>
<rdf:Description rdf:about=""
xmlns:xmpMM="http://ns.adobe.com/xap/1.0/mm/">
<xmpMM:DocumentID>uuid:547fb234-f327-4f3d-8dd1-7b7ea87df336</xmpMM:DocumentID>
<xmpMM:InstanceID>uuid:72437fbf-7b45-4062-98f1-2b53d3ad86d0</xmpMM:InstanceID>
</rdf:Description>
</rdf:RDF>
</x:xmpmeta>
<?xpacket end="w"?>endstream
endobj
10 0 obj
[ 7 0 R ]
endobj
11 0 obj
<<
/BBox [ 41.3670998 713.710022 237.352997 781.257996 ]
/Length 67
/Resources <<
/XObject <<
/Img0 16 0 R
>>
>>
/Subtype /Form
/Type /XObject
/Matrix [ 1 0 0 1 0 0 ]
>>
stream
1.000000 0.000000 0.000000 -1.000000 0.000000 1.000000 cm
/Img0 Do
endstream
endobj
12 0 obj
<<
/BaseFont /Helvetica
/Encoding 14 0 R
/Name /Helv
/Subtype /Type1
/Type /Font
>>
endobj
13 0 obj
<<
/BaseFont /ZapfDingbats
/Name /ZaDb
/Subtype /Type1
/Type /Font
>>
endobj
14 0 obj
<<
/Differences [ 24 /breve /caron /circumflex /dotaccent /hungarumlaut
/ogonek /ring /tilde 39 /quotesingle 96 /grave 128 /bullet
/dagger /daggerdbl /ellipsis /emdash /endash /florin /fraction
/guilsinglleft /guilsinglright /minus /perthousand /quotedblbase
/quotedblleft /quotedblright /quoteleft /quoteright /quotesinglbase
/trademark /fi /fl /Lslash /OE /Scaron /Ydieresis /Zcaron
/dotlessi /lslash /oe /scaron /zcaron 160 /Euro 164 /currency
166 /brokenbar 168 /dieresis /copyright /ordfeminine 172
/logicalnot /.notdef /registered /macron /degree /plusminus
/twosuperior /threesuperior /acute /mu 183 /periodcentered
/cedilla /onesuperior /ordmasculine 188 /onequarter /onehalf
/threequarters 192 /Agrave /Aacute /Acircumflex /Atilde /Adieresis
/Aring /AE /Ccedilla /Egrave /Eacute /Ecircumflex /Edieresis
/Igrave /Iacute /Icircumflex /Idieresis /Eth /Ntilde /Ograve
/Oacute /Ocircumflex /Otilde /Odieresis /multiply /Oslash
/Ugrave /Uacute /Ucircumflex /Udieresis /Yacute /Thorn /germandbls
/agrave /aacute /acircumflex /atilde /adieresis /aring /ae
/ccedilla /egrave /eacute /ecircumflex /edieresis /igrave
/iacute /icircumflex /idieresis /eth /ntilde /ograve /oacute
/ocircumflex /otilde /odieresis /divide /oslash /ugrave /uacute
/ucircumflex /udieresis /yacute /thorn /ydieresis ]
/Type /Encoding
>>
endobj
15 0 obj
<<
/ByteRange[0 6712 10814 12756] /Contents
...
/Filter /Adobe.PPKLite
/SubFilter /adbe.pkcs7.detached
>>
endobj
16 0 obj
<<
/Type /XObject
/Subtype /Image
/Width 400
/Height 332
/ColorSpace /DeviceRGB
/BitsPerComponent 8
/Length 11097
>>
stream
...
endstream
endobj
17 0 obj
<<
/BBox [ 41.3670998 713.710022 237.352997 781.257996 ]
/FormType 1
/Length 7
/Matrix [ 1 0 0 1 0 0 ]
/Resources <<
/ProcSet [ /PDF /Text ]
/XObject <<
/FRM 18 0 R
>>
>>
/Subtype /Form
/Type /XObject
>>
stream
/FRM Doendstream
endobj
18 0 obj
<<
/BBox [ 41.3670998 713.710022 237.352997 781.257996 ]
/FormType 1
/Length 51
/Matrix [ 1 0 0 1 0 0 ]
/Resources <<
/ProcSet [ /PDF /Text ]
/XObject <<
/n0 19 0 R
/n2 11 0 R
>>
>>
/Subtype /Form
/Type /XObject
>>
stream
q 1 0 0 1 0 0 cm /n0 Do Q q 1 0 0 1 0 0 cm /n2 Do Qendstream
endobj
19 0 obj
<<
/BBox [ 41.3670998 713.710022 237.352997 781.257996 ]
/FormType 1
/Length 9
/Matrix [ 1 0 0 1 0 0 ]
/Resources <<
/ProcSet [ /PDF /Text ]
>>
/Subtype /Form
/Type /XObject
>>
stream
% DSBlankendstream
endobj
xref
0 20
0000000000 65536 f
0000000017 00000 n
0000000123 00000 n
0000000238 00000 n
0000000320 00000 n
0000000544 00000 n
0000000634 00000 n
0000000790 00000 n
0000001026 00000 n
0000001272 00000 n
0000004664 00000 n
0000004691 00000 n
0000004990 00000 n
0000005101 00000 n
0000005196 00000 n
0000006628 00000 n
0000010882 00000 n
0000022144 00000 n
0000022423 00000 n
0000022763 00000 n
trailer
<<
/Size 20
/Info 6 0 R
/Root 5 0 R
/ID [ <D0151A7ADF2410CF3E72A04B9CB83452> <FA2030FCC5A66742887F23E51DEC5875> ]
>>
startxref
23003
%%EOF
It seems to be correct, since there's /n2 11 0 R in 18 0 obj and /Img0 16 0 R in 11 0 obj and 16 0 obj looks like a well-formed Image XObject.
But the image doesn't show up when I using pdf reader to open this pdf.
Could someone please give me a clue about what's wrong with this pdf file, and how to insert the image correctly.
Thanks a lot.
ps.
signed_with_image_only.pdf
signed_with_text_and_image.pdf
signed_with_image_only.pdf is the pdf I've added '\n' before 'endstream'. You can see image doesn't show up in the form which is below 'Hello World!'.
signed_with_text_and_image.pdf is pretty much the same as signed_with_image_only.pdf, but I put some text in layer n2 to show you text insertion is ok. The same image is also inserted, and it also not showed up.
The transformation matrix is not right.
You can use other PDF utility, like PDFBox, to add an image into digital signature appearance, then analyze the transformation matrix on image painting, also pay attention on its BBOx.
I don't know if it's the text formatting but you have a number of streams (objects 17, 18 and 19) where the "endstream" that denotes the end of the stream data is not being written correctly. The "endstream" should be on a line of its own, and a PDF file requires whitespace between tokens and operators. Therefore, things like
/FRM Doendstream
are invalid syntax, they should be written like
/FRM Do
endstream
For images, the current transform matrix must transform from the 1 unit square that images are specified in, to the desired size in PDF units. You're using a transformation matrix of [1 0 0 -1 0 1] to position and scale the image XObject, which will not give your desired result. Typically, for an unrotated image you'll want to use something like [width 0 0 height Xpos Ypos]. Also, I always recommend enclosing image XObject invocation in a graphic state save/restore so that the CTM required can be popped off the graphic state stack.
Finally, looking at the file in Dropbox your image is damaged. It specifies a width of 400 rasters and a height of 332 rasters - in RGB format, the image should have a length of 398,400 but only has a length of 11097. This is way too short for this image data stream. It looks like this is compressed data, in DCT compressed format. For this to render properly you need to specify that it is compressed and in the format, e.g. add
/Filter /DCTDecode
to the image dictionary.

Try to simulate a neural network in MATLAB by myself

I tried to create a neural network to estimate y = x ^ 2. So I created a fitting neural network and gave it some samples for input and output. I tried to build this network in C++. But the result is different than I expected.
With the following inputs:
0 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 -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
and the following outputs:
0 1 4 9 16 25 36 49 64 81 100 121 144 169 196 225 256 289 324 361 400
441 484 529 576 625 676 729 784 841 900 961 1024 1089 1156 1225 1296
1369 1444 1521 1600 1681 1764 1849 1936 2025 2116 2209 2304 2401 2500
2601 2704 2809 2916 3025 3136 3249 3364 3481 3600 3721 3844 3969 4096
4225 4356 4489 4624 4761 4900 5041 1 4 9 16 25 36 49 64 81 100 121 144
169 196 225 256 289 324 361 400 441 484 529 576 625 676 729 784 841
900 961 1024 1089 1156 1225 1296 1369 1444 1521 1600 1681 1764 1849
1936 2025 2116 2209 2304 2401 2500 2601 2704 2809 2916 3025 3136 3249
3364 3481 3600 3721 3844 3969 4096 4225 4356 4489 4624 4761 4900 5041
I used fitting tool network. with matrix rows. Training is 70%, validation is 15% and testing is 15% as well. The number of hidden neurons is two. Then in command lines I wrote this:
purelin(net.LW{2}*tansig(net.IW{1}*inputTest+net.b{1})+net.b{2})
Other information :
My net.b[1] is: -1.16610230053776 1.16667147712026
My net.b[2] is: 51.3266249426358
And net.IW(1) is: 0.344272596370387 0.344111217766824
net.LW(2) is: 31.7635369693519 -31.8082184881063
When my inputTest is 3, the result of this command is 16, while it should be about 9. Have I made an error somewhere?
I found the Stack Overflow post Neural network in MATLAB that contains a problem like my problem, but there is a little difference, and the differences is in that problem the ranges of input and output are same, but in my problem is no. That solution says I need to scale out the results, but how can I scale out my result?
You are right about scaling. As was mentioned in the linked answer, the neural network by default scales the input and output to the range [-1,1]. This can be seen in the network processing functions configuration:
>> net = fitnet(2);
>> net.inputs{1}.processFcns
ans =
'removeconstantrows' 'mapminmax'
>> net.outputs{2}.processFcns
ans =
'removeconstantrows' 'mapminmax'
The second preprocessing function applied to both input/output is mapminmax with the following parameters:
>> net.inputs{1}.processParams{2}
ans =
ymin: -1
ymax: 1
>> net.outputs{2}.processParams{2}
ans =
ymin: -1
ymax: 1
to map both into the range [-1,1] (prior to training).
This means that the trained network expects input values in this range, and outputs values also in the same range. If you want to manually feed input to the network, and compute the output yourself, you have to scale the data at input, and reverse the mapping at the output.
One last thing to remember is that each time you train the ANN, you will get different weights. If you want reproducible results, you need to fix the state of the random number generator (initialize it with the same seed each time). Read the documentation on functions like rng and RandStream.
You also have to pay attention that if you are dividing the data into training/testing/validation sets, you must use the same split each time (probably also affected by the randomness aspect I mentioned).
Here is an example to illustrate the idea (adapted from another post of mine):
%%# data
x = linspace(-71,71,200); %# 1D input
y_model = x.^2; %# model
y = y_model + 10*randn(size(x)).*x; %# add some noise
%%# create ANN, train, simulate
net = fitnet(2); %# one hidden layer with 2 nodes
net.divideFcn = 'dividerand';
net.trainParam.epochs = 50;
net = train(net,x,y);
y_hat = net(x);
%%# plot
plot(x, y, 'b.'), hold on
plot(x, x.^2, 'Color','g', 'LineWidth',2)
plot(x, y_hat, 'Color','r', 'LineWidth',2)
legend({'data (noisy)','model (x^2)','fitted'})
hold off, grid on
%%# manually simulate network
%# map input to [-1,1] range
[~,inMap] = mapminmax(x, -1, 1);
in = mapminmax('apply', x, inMap);
%# propagate values to get output (scaled to [-1,1])
hid = tansig( bsxfun(#plus, net.IW{1}*in, net.b{1}) ); %# hidden layer
outLayerOut = purelin( net.LW{2}*hid + net.b{2} ); %# output layer
%# reverse mapping from [-1,1] to original data scale
[~,outMap] = mapminmax(y, -1, 1);
out = mapminmax('reverse', outLayerOut, outMap);
%# compare against MATLAB output
max( abs(out - y_hat) ) %# this should be zero (or in the order of `eps`)
I opted to use the mapminmax function, but you could have done that manually as well. The formula is a pretty simply linear mapping:
y = (ymax-ymin)*(x-xmin)/(xmax-xmin) + ymin;

Resources