dimanche 29 mars 2015

error: cannot increment value of type 'char [6]'


I am trying to learn pointers and string literals in C/C++. As per my understanding, string literals are char arrays with a null \0 at the end. Also we can basically do all the pointer arithmetic operations on array like increment and decrement.


But when I am trying to run the following code:



#include <iostream>
using namespace std;

int main (){
char *c ="Hello";
char d[6];

while(*d++ = *c++);

cout<<c<<endl<<d;
}


I am getting the following error,



error: cannot increment value of type 'char [6]'
while(*d++ = *c++);


My assumption for this code was, that the values of string literal c will be copied to char array d.




convert mat to ipl image in opencv 3.0


I tried to convert mat image to IplImage but i could not able to convert it , i tried like this





Mat frame=imread("image path");
IplImage* image=IplImage(frame);



I got the error like cannot convert 'IplImage {aka _IplImage}' to 'IplImage* {aka _IplImage*}' in initialization...please any one tell how to convert in opencv 3.0




implementing matlab hist() in c++


here is the line of code I want to implement



kb = [];
for k = 1:length(nRef)
for n=1:length(dCmpInd{k})
x = [centroid(nRef{k}, 1), centroid(dCmpInd{k}(n),1)];
y = [centroid(nRef{k}, 2), centroid(dCmpInd{k}(n),2)];

[x,ind] = sort(x);
y = y(ind);
kb = [kb diff(y) / diff(x)];
end
end

theta = (atan(kb));

[N, X] = hist(abs(theta),2);


here is my c++ code:



std::vector<double> kb;
std::vector<double> theta;
for (int k = 0; k < nRef.size(); k++)
{
for (int n = 0; n < dCmpInd[k].size(); n++)
{
double x1 = centroids[nRef[k]].m_X; double x2 = centroids[dCmpInd[k][n]].m_X;
double y1 = centroids[nRef[k]].m_Y; double y2 = centroids[dCmpInd[k][n]].m_Y;
if (x1 <x2)
{
double tempdiff = (y2-y1)/(x2-x1);
kb.push_back(tempdiff);
theta.push_back(abs(atan(tempdiff)));
}
else
{
double tempdiff = (y1-y2)/(x1-x2);
kb.push_back(tempdiff);
theta.push_back(abs(atan(tempdiff)));
}
}
}


is there a quick way to implement :



[N,X] = hist(theta,2);


I can use openCV 2.4.10 as well but calcHist() isn't really the same, I need to create 2 bins.


my input is 1D array:



0.00598881028540019 1.56120677124307 0.00598881028540019 0.00669537049433832 1.37723800334516 1.37723800334516 1.36424594043624 1.56120677124307 0.0152220988707370


the output is:



X= 0.394793300524817 1.17240228100365
N = 4 5



C++ this pointer equivalent in Python ctypes


I'm calling a C dll from Python using ctypes and having some trouble passing the correct parameters to the IBSU_RegisterCallbacks function, defined below.



int WINAPI IBSU_RegisterCallbacks
(const int handle,
const IBSU_Events event,
void *pCallbackFunction,
void *pContext);


It describes the final parameter as:



/* pContext Pointer to user context that will be passed to callback function.*/


A working C++ example calls the function like this:



RegisterCallbacks(0, 1, OnCallback, this);


So far in Python I have the below code (relevant section shown only), which crashes Python (no traceback) shortly after the event is triggered and callback run! is printed.



from ctypes import *

class IBSU(object):
_dll = WinDLL('ibsu.dll')

_C_CALLBACK = CFUNCTYPE(
None,
c_int, # device handle
c_void_p) # user context

_register_callbacks = _dll.IBSU_RegisterCallbacks
_register_callbacks.restype = c_int
_register_callbacks.argtypes = (c_int, # device handle
c_int, # event type
c_void_p, # callback function
c_void_p) # user context

def _get_c_callback(self):
def callback(handle, # device handle
p_context): # user context
print 'callback run!'
return self._C_CALLBACK(callback)

def register_callbacks(self):
# keep ref to prevent garbage collection
self._c_callback = self._get_c_callback()

self._register_callbacks(0, # device handle
1, # event type
self._c_callback, # callback function
c_void_p()) # user context


What is the equivalent to this in my Python class that I can send instead of just c_void_p()?




does the value of pointers change if exception occurs during assignment?


Consider the following code where pb is a member of class myclass:



myclass& operator=(const myclass& rhs){
myclass *pOrig = pb;
pb = new myclass(*rhs.pb); //exception occurs here .
delete pOrig;
return *this;
}


Will the value of pb remain the same or will it change ? Explain.




What is the difference between the address stored and displayed in C and C++?


In C, if I make a variable and print its address like this:



int a;
int main (void) {
printf ("%p", &a);
return 0;
}


The output was: 00AA


The same program in C++ using the line:



cout << &a << endl;


The output was: 0x8f970aa


What is the difference between these two?


I compiled both the programs using Turbo C.




Face alignment and Fisher face recognition


I am trying out face recognition with LDA Fisher faces. Since Fisher faces are not rotation invariant, is it a good method to do face alignment before face recognition? Please provide a theoretical/math explanation.


Thanks in advance