pyproj is a language binding for PROJ. The pyproj module provides easy-to-use methods for CRS-to-CRS transformations, For instance, use it to convert global latitude/longitude (degrees) to local coordinates with respect to some coordinate reference system (metres):
>>> from pyproj import Transformer
>>> transformer = Transformer.from_crs(4326, 6677)
>>> transformer.transform(35.9032597, 139.9343755)
(-10728.330840296036, 9120.537451921156)
PROJ provides a C++ API but the documentation is pure crap. There are no sample codes and I couldn't find my way through it.
I'd be really appreciative if anyone could give a clue on how to use the C++ API the same way pyproj works for arbitrary transformations.
You can do something like this
#include <proj.h>
#include <stdio.h>
PJ_CONTEXT *C;
PJ *P;
C = proj_context_create();
P = proj_create_crs_to_crs(C, "EPSG:4326", "EPSG:6677", NULL);
PJ_COORD input_coords,
output_coords; // https://proj.org/development/reference/datatypes.html#c.PJ_COORD
input_coords = proj_coord(35.9032597, 139.9343755, 0, 0);
output_coords = proj_trans(P, PJ_FWD, input_coords);
std::cout << output_coords.xy.x << " " << output_coords.xy.y << std::endl;
/* Clean up */
proj_destroy(P);
proj_context_destroy(C); // may be omitted in the single threaded case
Of course, beyond this initial level, you can make whatever function or class wrappers you need. But this reproduces the pyproj example you posted.
Related
I used a temporary tensor to store data in my customized gpu-based op. For debug purpose, I want to print the data of this tensor by traditional printf inside C++. How can I pull this gpu-based tensor to cpu and then print its contents.
Thank you very much.
If by temporary you mean allocate_temp instead of allocate_output, there is no way of fetching the data on the python side.
I usually return the tensor itself during debugging so that a simple sess.run fetches the result. Otherwise, the only way to display the data is the traditional printf inside C++. Given your tensor is an output of your custom operation a tf.Print eases further debugging.
Example:
Tensor temp_tensor;
OP_REQUIRES_OK(ctx, ctx->allocate_temp(DT_FLOAT, some.shape(), &temp_tensor));
float* host_memory = new float[some.NumElements()];
cudaMemcpy(host_memory, temp_tensor.flat<Dtype>().data(), some.NumElements() * sizeof(float), cudaMemcpyDeviceToHost);
std::cout << host_memory[0] << std::endl;
std::cout << host_memory[1] << std::endl;
std::cout << host_memory[2] << std::endl;
delete[] host_memory;
I do have a struct in a file wich is included by the host code and the kernel
typedef struct {
float x, y, z,
dir_x, dir_y, dir_z;
int radius;
} WorklistStruct;
I'm building this struct in my c++ host code and passing it via a buffer to the OpenCL kernel.
If I'm choosing an CPU device for computation I will get the following result:
printf ( "item:[%f,%f,%f][%f,%f,%f]%d,%d\n", item.x, item.y, item.z, item.dir_x, item.dir_y,
item.dir_z , item.radius ,sizeof(float));
Host:
item:[20.169043,7.000000,34.933712][0.000000,-3.000000,0.000000]1,4
Device (CPU):
item:[20.169043,7.000000,34.933712][0.000000,-3.000000,0.000000]1,4
And if I choose a GPU device (AMD) for computation weird things are happening:
Host:
item:[58.406261,57.786015,58.137501][2.000000,2.000000,2.000000]2,4
Device (GPU):
item:[58.406261,2.000000,0.000000][0.000000,0.000000,0.000000]0,0
Notable is that the sizeof(float) is garbage on the gpu.
I assume there is a problem with the layouts of floats on different devices.
Note: the struct is contained in an array of structs of this type and every struct in this array is garbage on GPU
Anyone does have an idea why this is the case and how I can predict this?
EDIT I added an %d at the and and replaced it by an 1, the result is:1065353216
EDIT: here two structs wich I'm using
typedef struct {
float x, y, z,//base coordinates
dir_x, dir_y, dir_z;//directio
int radius;//radius
} WorklistStruct;
typedef struct {
float base_x, base_y, base_z; //base point
float radius;//radius
float dir_x, dir_y, dir_z; //initial direction
} ReturnStruct;
I tested some other things, it looks like a problem with printf. The values seems to be right. I passed the arguments to the return struct, read them and these values were correct.
I don't want to post all of the related code, this would be a few hundred lines.
If noone has an idea I would compress this a bit.
Ah, and for printing I'm using #pragma OPENCL EXTENSION cl_amd_printf : enable.
Edit:
Looks really like a problem with printf. I simply don't use it anymore.
There is a simple method to check what happens:
1 - Create host-side data & initialize it:
int num_points = 128;
std::vector<WorklistStruct> works(num_points);
std::vector<ReturnStruct> returns(num_points);
for(WorklistStruct &work : works){
work = InitializeItSomehow();
std::cout << work.x << " " << work.y << " " << work.z << std::endl;
std::cout << work.radius << std::endl;
}
// Same stuff with returns
...
2 - Create Device-side buffers using COPY_HOST_PTR flag, map it & check data consistency:
cl::Buffer dev_works(..., COPY_HOST_PTR, (void*)&works[0]);
cl::Buffer dev_rets(..., COPY_HOST_PTR, (void*)&returns[0]);
// Then map it to check data
WorklistStruct *mapped_works = dev_works.Map(...);
ReturnStruct *mapped_rets = dev_rets.Map(...);
// Output values & unmap buffers
...
3 - Check data consistency on Device side as you did previously.
Also, make sure that code (presumably - header), which is included both by kernel & host-side code is pure OpenCL C (AMD compiler sometimes can "swallow" some errors) and that you've imported directory for includes searching, when building OpenCL kernel ("-I" flag at clBuildProgramm stage)
Edited:
At every step, please collect return codes (or catch exceptions). Beside that, "-Werror" flag at clBuildProgramm stage can also be helpfull.
It looks like I used the wrong OpenCL headers for compiling. If I try the code on the Intel platform(OpenCL 1.2) everything is fine. But on my AMD platform (OpenCL 1.1) I get weird values.
I will try other headers.
This might have been asked several times before. But I feel mine is a bit different and since I lack complete understanding of concepts, I am posting it again.
I am working on opencv code written in C++ on Ubuntu that can match vein patterns. I have captured 4 vein images. In my program, I would like to capture a new image from the IR camera and compare it with the images in the images directory. I am planning to use fuzzy C clustering algorithm for my matching. I have created a user menu in which one option is comparing my algorithm with FLANN, SIFT, etc. This comparison is based on the time taken. How do you calculate the time taken for computation?
I am completely new to Fuzzy clustering and any tutorials/Sample codes that might help is greatly appreciated.
Also, can you please suggest how to go about comparing a file captured from camera to a file in directory in linux?
Edit 1: Have uploaded two sample vein patterns with their Canny Edge Detectors.
Vein Pattern 1
Vein Pattern 2
www.i.imgur.com/mvt3kIy.jpg (Canny Edge 1)
www.i.imgur.com/8GwaLTu.jpg (Canny Edge 2)
Please suggest some methods to compare the same.
To calculate the time elapsed between a set of instructions,
#include <time>
int main()
{
// whatever code
clock_t tstart = clock();
/// more code and implementations
cout << "Processing time = " << (double)(clock() - tstart)/(CLOCKS_PER_SEC) << " second(s)" << endl;
}
There are many ways in which you can compare 2 files; if you post some images, I might be able to guide you further. You might try and read some of OpenCV documentation and related papers. This link will give you a head start to feature description..
I use this function for timings:
#include <sys/time.h>
#include <iostream>
inline long getMilliSecs()
{
timeval t;
gettimeofday(&t, NULL);
return t.tv_sec*1000 + t.tv_usec/1000;
}
int main()
{
long start_time = getMilliSecs();
///
//do stuff;
///
long end_time = getMilliSecs();
std::cout << ((double)(end_time - start_time))/1000 << " seconds" << std::endl;
}
I am trying to use Python Ctypes to interface a published (closed source) C++ library. I (tried) wrote a basic C style function wrapper to construct the C++ vector style objects and call the C++ routine. I also (tried) wrote a basic python script to load the shared library. Everything is working except the line that calls the C++ routine which yields:
*** glibc detected *** python: free(): invalid next size (fast): 0x0000000001e73c00 ***
Here are the files, unfortunately I cannot share headers, but I may be able to write something similar if need be...
gaumixmod.cpp:
#include "nr3.h"
#include "cholesky.h"
#include "gaumixmod.h"
extern "C" {
void cGaumixmod(double* D, int Dm, int Dn, double* M, int Mm, int Mn) {
MatDoub ddata(Dm,Dn,*D); // construct Matrix (vector) type
MatDoub mmeans(Mm,Mn,*M); // construct Matrix (vector) type
//XXX test numpy array is coming through as C array and we can rw, checks OK
int i;
// for(i=0;i<Dn*Dm;++i) {
// printf("Address %x : ",(D+i));
// printf("was %f \t" , D[i]);
// D[i]+=1.0;
// printf("now: %f \n" , D[i]);
// }
// check that array D was copied to matrix ddata, and we can r/w
for(i=0;i<Dm*Dn;++i) {
printf("iter %d Address %x : ",i,ddata[i/Dm][i%Dm]);
printf("was %f \t" , ddata[i/Dm][i%Dm]);
ddata[i/Dm][i%Dm]+=1.0;
printf("now: %f \n" ,ddata[i/Dm][i%Dm]);
}
Gaumixmod::Gaumixmod(ddata,mmeans);
//return data from vector to ctypes array C so we can check data returns to python
//via numpy array, checks ok
for(i=0;i<Dm*Dn;++i) {
D[i] = ddata[i/Dm][i%Dm];
}
}
}
goumixmod.py:
import platform,ctypes
import numpy as np
# ------------------------------------------------------------------------
# define correct library from platfrom, assuming 64bit for linux machines
# ------------------------------------------------------------------------
if platform.system()=='Microsoft':
raise Exception('MS not supported.')
elif platform.system()=='Darwin':
libgaumixmod = ctypes.cdll.LoadLibrary("./gaumixmod.so")
elif platform.system()=='Linux':
libgaumixmod = ctypes.cdll.LoadLibrary("./gaumixmod.so")
else:
#hope for the best
libgaumixmod = ctypes.cdll.LoadLibrary("./gaumixmod.so")
# --------------------------------------------------
# define SafeCall
#---------------------------------------------------
def SafeCall(ret):
"""pass, code l8r""
print ret
#---------------------------------------------------
# define arg types and res types of function
# -----------------------------------------------------------------------
_gaumixmod = libgaumixmod.cGaumixmod
_gaumixmod.restype = ctypes.c_int
_gaumixmod.argtypes = [np.ctypeslib.ndpointer(dtype=np.float64,flags='C_CONTIGUOUS'),
ctypes.c_int,
ctypes.c_int,
np.ctypeslib.ndpointer(dtype=np.float64,flags='C_CONTIGUOUS'),
ctypes.c_int,
ctypes.c_int]
def gaumixmod(D,K):
"""Python binding for C++ guassian mixure model code."""
ret = _gaumixmod(D,D.shape[0],D.shape[1],K,K.shape[0],K.shape[1])
SafeCall(ret)
return D,K
D = np.ones((100,100)).astype(np.float64)
K = np.ones((4,1)).astype(np.float64)
print gaumixmod(D,K)
and I compile this jazz with:
g++ -fPIC -c gaumixmod.cpp;
g++ -shared -o gaumixmod.so gaumixmod.o
and run
python gaumixmod.py
My research indicates this error is something akin to a segFault, where python is trying to reach a memory outside its scope... This is the part I don't understand because commenting out the C++ line Gaumixmod::Gaumixmod(), everything works fine, and that routine should be operating on the vectors instantiated in the cGaumixmod() function, not the python numpy arrays. I am really unfamiliar with C++, although I have used C types many times for C libraries. I am hoping that someone with some C++,python,and ctypes experience can give some insight/guidance here.
Thanks!
There is a new library for interfacing with C, you might what to look at it:
http://cffi.readthedocs.org/en/latest/index.html
I have a program that reads battery status in Windows that looks like this (simplified code):
#include <iostream>
#include <windows.h>
using namespace std;
int main(int argc, char *argv[]) {
SYSTEM_POWER_STATUS spsPwr;
if( GetSystemPowerStatus(&spsPwr) ) {
cout << "\nAC Status : " << static_cast<double>(spsPwr.ACLineStatus)
<< "\nBattery Status : " << static_cast<double>(spsPwr.BatteryFlag)
<< "\nBattery Life % : " << static_cast<double>(spsPwr.BatteryLifePercent)
<< endl;
return 0;
} else return 1;
}
spsPwr.BatteryLifePercent holds remaining battery charge in percent and is of type BYTE, which means it can only show reading in round numbers (i.e. int). I notice that an application called BatteryBar can show battery percentage in floating point value.
BatteryBar is a .NET application. How can I get battery percentage reading in float/double using pure C/C++ with Windows API? (Solution that can be compiled with MinGW is preferable)
You can get this information using the WMI . try using the BatteryFullChargedCapacity and BatteryStatus classes both are part of the root\WMI namespace.
To get the remaining battery charge in percent just must use the RemainingCapacity (BatteryStatus) and FullChargedCapacity (BatteryFullChargedCapacity) properties.
The remaining battery charge in percent is
(RemainingCapacity * 100) / FullChargedCapacity
for example if the FullChargedCapacity property report a 5266 value and the RemainingCapacity reports a 5039, the reuslt will be 95,68932776 %
If you don't know how access the WMI from C++ read these articles
WMI C++ Application Examples
Making WMI Queries In C++
Well, as you said, the Windows API provides only an integral percentage value. And, as you implied, .NET provides a floating-point one.
That means that, to use the floating-point one, you have to use .NET. That said, the .NET value is between 0.0 and 1.0, and it's not clear from the documentation whether you actually gain more precision.
The tool states that it does "Statistical Time Prediction" so I doubt it uses the direct value of SYSTEM_POWER_STATUS.
Personally, I hardly can imagine what a floating-point precision would be good for, but anyway you could use ILSpy to see how they are doing it, or maybe you also could ask them how they do.
The .NET version doesn't actually provide you any more precision. It simply divides the BatterLifePercent byte value by 100.0 and returns the result. Here are the contents of the getter in .NET.
public float BatteryLifePercent
{
get
{
this.UpdateSystemPowerStatus();
float num = ((float) this.systemPowerStatus.BatteryLifePercent) / 100f;
if (num <= 1f)
{
return num;
}
return 1f;
}
}
UpdateSystemPowerStatus() calls WINAPI's GetSystemPowerStatus(), which in turn updates systemPowerStatus.