6/30/2014

Python Sutdy, To use python class in the c source code (Python embedding)

This post is that how to embed python file in C source code.
Most articles in internet is opposed concept, such as to embed C source code into python

"Extending" is called to embed C module into Python
"Embedding" is called  to embed Python module into C

This article is about embedding.
My final goal is that coding opencv in python and relase to C, C++ users by dll type.

This source cod is example how to use python class in the C.
You can know easily if you see the code carefully.

..
main.cpp
#include 

#ifdef _DEBUG           
#pragma comment(lib, "python27_d.lib")  //Now, this option does not run.
#else   
#pragma comment(lib, "python27.lib")   
#endif    



void main()
{
 PyObject *module, *request, *mP;
 float rVal;

 Py_Initialize();
 module = PyImport_ImportModule("emPy"); //.py file name
 if( module == NULL)
 {
  PyErr_Clear();
  printf("Unable to import embed module");
 }

 request = PyObject_CallMethod(module, "myPower", NULL); //class name
 if(request == NULL)
 {
  PyErr_Clear();
  printf("fail to call class");
 }

 mP = PyObject_CallMethod(request, "myPow","f",10.0); //member function name, input value

 if(mP == NULL)
 {
  PyErr_Clear();
  printf("fail to call function");
 }else{
  PyArg_Parse(mP,"f", &rVal);  //get value from class function of .py
  printf("%lf \n", rVal);
 }

 //clear
 if( module != NULL )
  Py_DECREF(module);
 else
  PyErr_Print();

 if( request != NULL )
  Py_DECREF(request);
 else
  PyErr_Print();
 
 if( mP != NULL )
  Py_DECREF(mP);
 else
  PyErr_Print();
 
 Py_Exit(0);
}
..



emPy.py
...
class myPower:
    def myPow(self, inA):
        print inA*inA
        return inA*inA

...

Environment setting
- emPy.py should be located same directory with main.cpp
- Path setting -> include -> "C:\Python27\include"
                           lib        -> "C:\Python27\libs"


6/27/2014

python + opencv study -> class making, opencv and numpy simple usages,

I made simple image subtraction class by python + opencv.
More detail, the class evaluate whether two image is same or diffrent by 2 threshold.
first threshold is the britness different of pixel.
second threshold is percent of change. eg. count(changed pixel) / area(width*height)

This class can be applied detection of motion in continues image.

And you can study how to run opencv in the python.
I am also bigginer of python use.

I studied a part of relation numpy and opencv.

class_ImgSubtraction.py
--
__author__ = 'mare'


import numpy as np
import cv2


class ImgSubtraction:
    #image load
    def __init__(self, r_img, th1, th2):
        self.RImg = r_img
        self.Th1, self.Th2 = th1, th2
        self.cols, self.rows = r_img.shape[:2]
        self.area = self.cols * self.rows

    #image subtraction
    def eval_subtraction(self, c_img):

        #return false if c_img size is different with RImg
        if self.RImg.shape[:2] != c_img.shape[:2]:
            return 0

        ic_img = c_img
        #subtraction
        is_img = np.subtract(self.RImg, np.int_(ic_img))
        #abs
        ia_img = np.abs(is_img)
        #count pixels difference over than th1
        dcount = np.sum(ia_img > self.Th1)
        #image change percent
        dpersent = (dcount/np.float32(self.area) ) * 100

        if dpersent >= self.Th2:
            return 1
        else:
            return 0

--

main.py
--
__author__ = 'mare'


import cv2
from class_ImgSubtraction import ImgSubtraction


RImg = cv2.imread('test.png', 0)
CImg = cv2.imread('test2.png', 0)

e1 = cv2.getTickCount()

cImgSub = ImgSubtraction(RImg, 10, 1)


if cImgSub.eval_subtraction(CImg):
    print ('image different')

e2 = cv2.getTickCount()
time = (e2 - e1)/cv2.getTickFrequency()
print(time, 1/time)

cv2.waitKey(0)

--

you can also download the source code on the github
-> https://gist.github.com/mare90/2ea9b9ca7c80c8c259e1