Gdal substract 1 bit raster gives me hard time - raster

I want to use gdal_calc to subtract one file from another. It works, but I get the wrong result all the time no matter what command line i try.
For your understanding I created 3 files.
Picture A:
Picture B:
Picture C:
As you can see I always get the overlap as result but I need the other part.
Desired Result:
gdal_calc.py -A pica.tif -B picb.tif --outfile=result.tif --calc="A-B"
Change the line to
gdal_calc.py -A pica.tif -B picb.tif --outfile=result.tif --calc="B-A"
or swap filenames doesn't work? I always get the same result.
I also delete the output file before trying new stuff so thats not the problem that I always look at the same file.

What are the values of your black and white pixels? Usually white would be 1 (or 255), and black 0, but that's just a guess. If that's the case, you probably want the inverse, so something like (1-A)-(1-B) (if your values are really 1bit).
For testing\debugging, it's a good idea to first convert both input images to an unsigned integer datatype. For example:
gdal_translate -ot Int32 pica.tif pica_int32.tif
That would allow negative values in the result, because gdal_calc does not convert the data to a new "working datatype" (as gdalwarp can do with -wtfor example). So that would mean you're doing the calculation in the input datatype (probably Byte?). See the "Note" box at:
https://gdal.org/programs/gdal_calc.html#cmdoption-type
And if you're subtracting, and the datatype is Byte, it probably causes results beyond the valid range of that datatype (like -1). Normal Numpy behavior is to simply "wrap around" (instead of clip etc), so that might cause some surprising results, for example:
import numpy as np
a = np.array([1, 0, 1, 1], dtype=np.uint8)
b = np.array([0, 1, 254, 255], dtype=np.uint8)
a - b
Results in:
array([ 1, 255, 3, 2], dtype=uint8)

Related

Plothraw PARIGP (or similar) doesn't work (latexit crash)

I'm a new user of PARI/GP, and after writing my script, I wanted to make a graph of it. As my function take an integer and return a number, it's closer to a sequence. Actually, I didn't know how to do it, so I read the documentation of PARI/GP, and after that I made some test in order to obtain a graph from a list.
After reading an answer in stackoverflow (Plotting multiple lists in Pari), I wanted to test with the following code:
plothraw([0..200], apply(i->cos(i*3*Pi/200), [0..200]), 0);
But when I do it, it tries to open something on latexit, but then it crash and give me a problem report.
I didn't even know that I had an app named latextit, maybe it was install during the installation of PARI/GP. Anyway, how can I fix this?
PARI/GP definitely doesn't install latexit.
The way hi-res graphics work on the Win32 version of PARI/GP is to write down an Enhanced Metafile (.EMF) in a temp directory and ask the system to
"open" it. When you installed latexit it probably created an association in the registry to let it open .EMF files
i3Pi does not mean what you think, it just creates a new variable with that name. You want i * 3 * Pi instead.
The following constructions both work in my setup
plothraw([0..200], apply(i->cos(i*3*Pi/200), [0..200]), 0);
plothraw([0..200], apply(i->cos(i*3*Pi/200), [0..200]), 1);
(the second one being more readable because a red line is drawn between successive points; I have trouble seeing the few tiny blue dots)
Instead of apply, you can use a direct constructor as in
vector(201, i, cos((i-1) * 3 * Pi / 200))
which of course can be computed more efficiently as
real( powers(exp(3*I*Pi/200), 200) )
(of course, it doesn't matter here, but compare both commands at precision \p10000 or so ...)

Transform byte flags in error status codes to int

I'm confused in bitmasks, bitflags, and other "bit" stuff.
I have a list of error codes - like [102, 104, 108, 80000000, 8000000, 10000000, 1000000, 0x0008000, ....]. Part of it writing in system logs like bitflags, f.e. "67108864" (and part of them not, and return normal numbers). I need to match them, but I can not find any algorithm. How I can do that? Maybe it used only for codes like "0x0008000"? If it is true how to define which of this?
The problem is deeper. I do not understand byte flags. I understand that flags usually used for boolean states, but here is logged status codes. For example, 102 is (1100110)2. Byte flag for that number is 2+4+32+64 ... It is how I do transform int to binary form, and it is obviously equal 102. So I'm confused.
Thanks for your time!
You have to separate
numbered error/message codes (usually values in some range like 101, 102, 103... or in some ranges) - they just specify what error is occured, this error (like "file isn't found") is fully qualified (at least message creators believe :))
codes containing set of flags (usually written in hex or binary notation if available) - they give us details, every bit contains a piece of information.

How to get the size of symbols in the symbol table of Mach-O file?

Before watch the mail list, I'm confused with the lack of "size" of symbol table in the Mach-o file. And I found the solution in source file posted in that E-Mail, which note that:
//Mach-O symbol table does have size in it
//so need to scan ahead to find symbol with next highest address.
But when I parse out the symbol table in a Mach-O file (I got the symbol table from the symtab_command and the following nlists) and trying to calculate the size of one global symbol as the same way, I was confused again when I compared the symbol table from the output of dwarfdump (dwarfdump -ae). The end address of the symbol in the symbol table from the dwarfdump is different from the result my program's output. Is there some problem with the symbol table I parsed out? Or is there some other way to work out it?
Some of the output from my program:
<start address> <section index> <method>
0x0006d030 1 ___arclite_objc_autoreleasePoolPop
0x0006d048 1 _patch_lazy_pointers
0x0006d1f0 1 ___arclite_objc_autoreleasePoolPush
The corresponding part of the output from dwarfdump:
0x0014a37b: [0x0006d030 - 0x0006d046) __arclite_objc_autoreleasePoolPop
0x0014a122: [0x0006d048 - 0x0006d1ee) patch_lazy_pointers
0x0014a3a0: [0x0006d1f0 - 0x0006d212) __arclite_objc_autoreleasePoolPush
So if I use the way in the "MachONormalizedFileToAtoms.cpp" to calculate the end address of the symbol (look ahead to find symbol with next highest address), the result must be different from the output of dwarfdump. And does anyone know how dwarfdump calculate it?
Thank you!
From the answer by Nick Kledzik:
The compiler often aligns functions to start at aligned address (e.g. 8 or 16 bytes). So, there is padding bytes (usually NOPs) after the end of a function and before the start of the next function.
dwarfdump has access to the debug info which does have size info for functions. So dwarfdump can show the size of a function without the alignment padding at the end. Whereas the linker just looks at the next symbol address. There is not much point in the linker digging through the debug info to get a function’s true size, because when writing the output, the linker has to align the next function which would just add back the pad bytes.
I hope that can help others who has the same confusion.

Prolog out of local stack space/ infinite recursion

I have read other similar questions and answers on this site, but can't seem to find the answer to my particular problem. I am trying to encode a maze in Prolog.
From region 0, you can move freely to regions 1 or region 3. From region 3, you can move freely to region 0, region 4, or region 5, etc. I want to find the all paths of length 7 from the beginning to the end (from 0 to 14). I have encoded the problem in the following manner in SWI-Prolog:
region(0).
region(1).
region(2).
region(3).
region(4).
region(5).
region(6).
region(7).
region(8).
region(9).
region(10).
region(11).
region(12).
region(13).
region(14).
region(15).
connection(0,1). %connection exists between these two regions
connection(0,3).
connection(1,2).
connection(1,8).
connection(1,7).
connection(3,4).
connection(3,5).
connection(7,9).
connection(7,5).
connection(7,8).
connection(5,6).
connection(8,10).
connection(8,11).
connection(11,12).
connection(11,13).
connection(13,15).
connection(13,14).
double_link(X,Y) :-
region(X),
region(Y),
( connection(X,Y) | connection(Y,X) ). %can go from region X to region Y, and vice versa
path(X,Y) :-
double_link(X,Y).
path(X,Y) :-
double_link(X,Z),
path(Z,Y).
When I execute path(14,0). I get true. However, when I execute path(0,14)., I run out of local stack space. I don't know how that can be. Thanks for any help!
You said:
When I execute path(14,0). I get true.
That is half of the truth! Oh, even less than that! In fact you get true not once but many times!
?- path(14,0).
true
; true
; true
; true
; true
; true
; ... .
There is a simple way to avoid typing ; or SPACE all the time. Simply use the goal false.
?- path(14,0), false
loops.
And now, you can also add such false-goals into your program. In this manner you are ruling out parts of the program ; and if the remaining part still loops, you know that there must be a problem there. This is what I got:
region(0) :- false.
% ...
region(12) :- false.
region(13).
region(14).
region(15) :- false.
connection(0,1) :- false.
% ...
connection(13,15) :- false.
connection(13,14).
double_link(X,Y) :-
region(X),
region(Y),
( connection(X,Y) ; connection(Y,X) ).
path(X,Y) :- false,
double_link(X,Y).
path(X,Y) :-
double_link(X,Z),
path(Z,Y), false.
So in this remaining part that loop has to be addressed. It should be now self-explanatory, isn't it?
The problem arises because you can go in circles in the maze. E.g. in your maze you have the connections 0 - 1 - 7 - 5 - 3 - 0. You have not taken any measures to ensure that the search does not follow those circles blindly.
A usual approach would be to add an argument to your path predicate that contains the already visited regions (initially empty). Then you have to ensure when you go to a new location X that X is not in that list (e.g. with nonmember(X,Visited)).

matplotlib mathtext does not work

I'm doing a hist plot and I want some numbers shown in the plot, so I put in a text box using mathtext for the text, but I doesn't work and I can't see why.
a = [2086., 360.5, 1000.]
b = [977., 37., 498.]
c = [4512., 690., 378.]
textstr = r'$\per50=%.2f$\n$\per16=%.2f$\n$\per84=%.2f$'%(a[0],b[0],c[0])
# these are matplotlib.patch.Patch properties
props = dict(boxstyle='round', facecolor='wheat', alpha=0.75)
# place a text box in upper left in axes coords
ax.text(0.05, 0.95, textstr, transform=ax.transAxes, fontsize=14,
verticalalignment='top', bbox=props)
At the end of my figure I'm getting this error:
matplotlib.pyparsing.ParseFatalException: Expected end of math '$'
$\per50=2086.00$\n$\per16=977.00$\n$\per84=4512.00$ (at char 0), (line:1, col:1)
I hope you can help me!
You are getting that error because the command $\per$ does not exist. Is that a latex command you defined? If you set the matplotlib parameter text.usetex=True, it is possible to set a latex preamble and define commands there, e.g.:
rc('text', usetex=True)
rc('text.latex', preamble='something')
But I don't think this can be used to define new commands (and the use of preamble is discouraged). So your best bet is to write explicitly in matplotlib whatever \per stands for.

Resources