Wrapping an opencv implementaion of an error level analysis algorithm using cython

拈花ヽ惹草 提交于 2019-12-16 18:04:45

问题


i have implemented an error level analysis algorithm using c++(opencv version 2.4) and i want to build a python wrapper for it using cython. I have read some part of the documentation of cython for c++ but it did not help me and moreover i did not find any extra information for implementing the wrapper online. It would be really great if someone could guide me and help me solve this problem.

This is my code for which i want to build a pyhton wrapper:

#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <vector> 

// Control
int scale = 15,
quality = 75;

// Image containers
cv::Mat input_image,
compressed_image;

void processImage(int, void*)
{

// Setting up parameters and JPEG compression
std::vector<int> parameters;
parameters.push_back(CV_IMWRITE_JPEG_QUALITY);
parameters.push_back(quality);
cv::imwrite("lena.jpeg", input_image, parameters);

// Reading temp image from the disk
compressed_image = cv::imread("lena.jpeg");

if (compressed_image.empty())
{
  std::cout << "> Error loading temp image" << std::endl;
  exit(EXIT_FAILURE);
}

cv::Mat output_image = cv::Mat::zeros(input_image.size(), CV_8UC3);

// Compare values through matrices
for (int row = 0; row < input_image.rows; ++row)
{
 const uchar* ptr_input = input_image.ptr<uchar>(row);
 const uchar* ptr_compressed = compressed_image.ptr<uchar>(row);
 uchar* ptr_out = output_image.ptr<uchar>(row);

    for (int column = 0; column < input_image.cols; column++)
    {
        // Calc abs diff for each color channel multiplying by a scale factor
        ptr_out[0] = abs(ptr_input[0] - ptr_compressed[0]) * scale;
        ptr_out[1] = abs(ptr_input[1] - ptr_compressed[1]) * scale;
        ptr_out[2] = abs(ptr_input[2] - ptr_compressed[2]) * scale;

        ptr_input += 3;
        ptr_compressed += 3;
        ptr_out += 3;
    }
}

// Shows processed image
cv::imshow("Error Level Analysis", output_image);
} 

int main (int argc, char* argv[])
{
// Verifica se o número de parâmetros necessário foi informado
if (argc < 2)
{
 std::cout << "> You need to provide an image as parameter" << std::endl;
 return EXIT_FAILURE;
}

// Read the image
input_image = cv::imread(argv[1]);

// Check image load
if (input_image.empty())
{
  std::cout << "> Error loading input image" << std::endl;
  return EXIT_FAILURE;
}

// Set up window and trackbar
cv::namedWindow("Error Level Analysis", CV_WINDOW_AUTOSIZE);
cv::imshow("Error Level Analysis", input_image);
cv::createTrackbar("Scale", "Error Level Analysis", &scale, 100,   processImage);
cv::createTrackbar("Quality", "Error Level Analysis", &quality, 100, processImage);

// Press 'q' to quit
while (char(cv::waitKey(0)) != 'q') {};

return EXIT_SUCCESS;
} 

https://github.com/shreyneil/image_test/blob/master/ela.cpp

Contributions are welcome. Thank you.


回答1:


It isn't really clear what you hope to accomplish by this, but it's pretty easy to make the functions callable from Cython. Start by making some small changes to main - it will need renaming so that it no longer acts as the main function for a program, and since you only use the second command-line argument as a file name you should change it to:

void some_function(char* filename) {
    // Read the image
    input_image = cv::imread(filename);
    // everything else the same
}

Then create your Cython wrapper cy_wrap.pyx. There are two parts to this. First you need to tell Cython about your two C++ functions (cdef extern from). Second you'll need to write a small wrapper function that can call these from Python:

cdef extern from "ela.hpp":
    # you'll need to create ela.hpp with declarations for your two functions
    void processImage(int, void*)
    void some_function(char* filename)

# and Python wrappers
def processImagePy():
   # since the parameters are ignored in C++ we can pass anything
   processImage(0,NULL)

def some_functionPy(filename):
   # automatic conversion from string to char*
   some_function(filename)

Using this module you'll be able to call processImagePy and some_functionPy.

To compile it to a Python module you'll need to write a setup.py file. I suggest you follow the template given in the Cython documentation (which you have read, right?). Your source files will be cy_wrap.pyx and ela.cpp. You'll probably want to link to the OpenCV library. You'll need to specify language="c++"



来源:https://stackoverflow.com/questions/47969835/wrapping-an-opencv-implementaion-of-an-error-level-analysis-algorithm-using-cyth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!