Implementation of functions with Python to C ++

asked 2016-12-02 12:36:09 -0500

Good afternoon ! The task is to implement the replacement of face. faced with the problem of transfer of color and glare. I found this article . I need to implement the function code "correct_colours" python to c ++. This is python code: def correct_colours(im1, im2, landmarks1): print RIGHT_EYE_POINTS; a1 = landmarks1[LEFT_EYE_POINTS]; blur_amount = COLOUR_CORRECT_BLUR_FRAC * numpy.linalg.norm( numpy.mean(landmarks1[LEFT_EYE_POINTS], axis=0) - numpy.mean(landmarks1[RIGHT_EYE_POINTS], axis=0))

blur_amount = int(blur_amount)
if blur_amount % 2 == 0:
    blur_amount += 1

    im1_blur = cv2.GaussianBlur(im1, (blur_amount, blur_amount), 0)
im2_blur = cv2.GaussianBlur(im2, (blur_amount, blur_amount), 0)

cv2.imwrite('im1_blur.jpg', im1_blur)
cv2.imwrite('im2_blur.jpg', im2_blur)

# Avoid divide-by-zero errors.
im2_blur += (128 * (im2_blur <= 1.0)).astype(im2_blur.dtype)    

return (im2.astype(numpy.float64) * im1_blur.astype(numpy.float64) /
                                            im2_blur.astype(numpy.float64))

That's how I implemented it in C ++: void correct_colours(Mat &im1,Mat &im2,vector<point2f> points2) {

arma::mat leftEye(EYE_POINTS_COUNT,2);
for(int i = 0; i < EYE_POINTS_COUNT; i++)
{
    leftEye(i,0) = (double)points2[LEFT_EYE_POINTS].x;
    leftEye(i,1) = (double)points2[LEFT_EYE_POINTS].y;
}

arma::mat rightEye(EYE_POINTS_COUNT,2);
for(int i = 0; i < EYE_POINTS_COUNT; i++)
{
    rightEye(i,0) = (double)points2[RIGHT_EYE_POINTS].x;
    rightEye(i,1) = (double)points2[RIGHT_EYE_POINTS].y;
}
arma::mat B = arma::mean(leftEye,0);
arma::mat C = arma::mean(rightEye,0);
arma::mat D = C-B;
int A = COLOUR_CORRECT_BLUR_FRAC*arma::norm(D);
if (A % 2 == 0)
{
   A += 1;
}

cv::Mat im1_blur;
cv::Mat im2_blur;
cv::Size size(A,A);
cv::GaussianBlur(im1, im1_blur,size, 0);
cv::GaussianBlur(im2, im2_blur,size, 0);
//imshow("im1_blur", im1_blur);
//imshow("im2_blur", im2_blur);
Mat rezult = im2_blur.clone();

uchar *p1,*p2,*p3,*p4;
for(int row = 0; row < im2_blur.rows; ++row) {
    p1 = im1_blur.ptr(row);
    p2 = im2_blur.ptr(row);
    p3 = im2.ptr(row);
    p4 = rezult.ptr(row);
    for(int col = 0; col < im2_blur.cols; ++col) {
        if( (*p2) <= 1 )
        {
            *p2 = (*p2)+128;
        }
        (*p4) = (*p3) * (*p1) / (*p2);
    }
}
imshow("rez", rezult);

But the result in C ++ not be what it should be. Please tell me what I'm doing wrong? I think it is a problem in the last two lines of code to python:

im2_blur += (128 * (im2_blur <= 1.0)).astype(im2_blur.dtype)

return (im2.astype(numpy.float64) * im1_blur.astype(numpy.float64) /
                                            im2_blur.astype(numpy.float64))

Rezult image in c++ just blurs image, but not transfer of color and glare.

edit retag flag offensive close merge delete