2017-12-25 29 views
-1

나는 C++ (opencv 버전 2.4)을 사용하여 오류 수준 분석 알고리즘을 구현했으며 cython을 사용하여 오류 래퍼를 작성하려고합니다. C++에 대한 cython 문서의 일부를 읽었지만 도움이되지 않았으며 온라인에서 래퍼 구현을위한 추가 정보를 찾지 못했습니다. 누군가 나를 안내하고이 문제를 해결할 수 있다면 정말 좋을 것입니다.cython을 사용하여 오류 수준 분석 알고리즘의 opencv 구현 랩핑

#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

공헌을 환영합니다 :

이 내가 pyhton 래퍼를 구축하려는 내 코드입니다. 감사합니다.

+0

나는 당신이 원하는 것에 대해 자세히 생각할 필요가 있다고 생각합니다 : 귀하의 github에 따르면 당신은 그 매개 변수를 무시하고 아무것도 반환하지 않는 하나의 기능을 가지고 있습니다 - 이것은 의미있는 감싸기가 될 것처럼 보이지 않습니다 Cython에서. 또한 명령 줄 인수로 일부 파일 이름을 받아들이는 opencv를 호출하는 주 프로그램이 있습니다.이 프로그램은 Cython에서 다시 래핑하기에 좋은 후보는 아닙니다. 또한 코드를 오프 사이트에 두었으므로 여기에 자체 포함 된 질문이 아닙니다. 마지막으로 이미 파이썬을위한 opencv 래퍼가 있습니까? 왜 그것을 사용하지 않습니까? – DavidW

+0

선생님, 사이먼을 사용하는 것은 저에게 제공되는 작업의 일부이며이를 완료하는 데 필요합니다. 나는 Cython을 처음 사용하기 때문에 위의 코드를 cython을 사용하여 래핑하는 방법을 안내 할 수 있다면 정말 멋질 것입니다. 래핑 된 것과 다른 관련 물건을 얻는 데 필요한 클래스는 무엇이 될까요? 고맙습니다. –

답변

1

당신이 이것을 통해 달성하고자하는 것은 분명하지 않지만, Cython에서 호출 할 수있는 함수를 만드는 것은 꽤 쉽습니다. main을 약간 변경하여 시작합니다. 더 이상 프로그램의 주 기능으로 사용되지 않도록 이름을 바꿔야하며, 두 번째 명령 줄 인수 만 파일 이름으로 사용하기 때문에 다음과 같이 변경해야합니다.

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

그런 다음 Cython 래퍼 cy_wrap.pyx을 만드십시오. 이것에는 두 부분이 있습니다. 먼저 Cython에게 두 개의 C++ 함수 (cdef extern from)에 대해 알려야합니다. 당신이 processImagePysome_functionPy를 호출 할 수 있습니다이 모듈을 사용

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) 

: 두 번째 파이썬에서 다음을 호출 할 수있는 작은 래퍼 함수를 ​​작성해야합니다.

파이썬 모듈로 컴파일하려면 setup.py 파일을 작성해야합니다. 나는 당신이 the template given in the Cython documentation을 따라 가기를 권한다. 소스 파일은 cy_wrap.pyxela.cpp입니다. 아마도 OpenCV 라이브러리에 링크하고 싶을 것입니다. 당신은해야 할 것입니다 specify language="c++"

+0

고맙습니다. 저는 cython을 사용하는 프로그램에 대해 [wrapper] (https://github.com/shreyneil/image_test) 클래스를 빌드 할 수 있었지만 테스트하는 동안 다음 오류가 발생했습니다 : ImportError :/home/shreyash/Desktop/New/ela_c.so : 정의되지 않은 기호 : _ZN2cv14createTrackbarERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES7_PiiPFviPvES9_ –

+0

OpenCV의 일부처럼 보입니다. 'libraries'에있는 것을 바꿀 필요가 있습니다. 나는 OpenCV를 사용한 적은 한번도 없었으므로 도움을 청할 수는 없다. – DavidW

+0

선생님, 고맙습니다.하지만 공유 객체 파일을 파이썬과 연결할 수 없기 때문에 오류가 발생합니다. 그렇게 할 수있는 단계를 제공해 주시겠습니까? 정말 도움이 될 것입니다. 고맙습니다. –