Ask Your Question

Revision history [back]

Difference in same operations between Python and C++ OpenCV Code

I am converting laplace.cpp file present in samples/cpp to python code but the output that i am getting from python code is different from c++ output. Here is the code for c++:

#include "opencv2/videoio.hpp"

include "opencv2/highgui.hpp"

include "opencv2/imgproc.hpp"

include <ctype.h>

include <stdio.h>

include <iostream>

using namespace cv; using namespace std;

static void help() { cout << "\nThis program demonstrates Laplace point/edge detection using OpenCV function Laplacian()\n" "It captures from the camera of your choice: 0, 1, ... default 0\n" "Call:\n" "./laplace -c=<camera #,="" default="" 0=""> -p=<index of="" the="" frame="" to="" be="" decoded="" captured="" next="">\n" << endl; }

enum {GAUSSIAN, BLUR, MEDIAN};

int sigma = 3; int smoothType = GAUSSIAN;

int main( int argc, char** argv ) { cv::CommandLineParser parser(argc, argv, "{ c | 0 | }{ p | | }"); help();

VideoCapture cap;
string camera = parser.get<string>("c");
if (camera.size() == 1 && isdigit(camera[0]))
    cap.open(parser.get<int>("c"));
else
    cap.open(samples::findFileOrKeep(camera));
if (!cap.isOpened())
{
    cerr << "Can't open camera/video stream: " << camera << endl;
    return 1;
}
cout << "Video " << parser.get<string>("c") <<
    ": width=" << cap.get(CAP_PROP_FRAME_WIDTH) <<
    ", height=" << cap.get(CAP_PROP_FRAME_HEIGHT) <<
    ", nframes=" << cap.get(CAP_PROP_FRAME_COUNT) << endl;
int pos = 0;
if (parser.has("p"))
{
    pos = parser.get<int>("p");
}
if (!parser.check())
{
    parser.printErrors();
    return -1;
}

if (pos != 0)
{
    cout << "seeking to frame #" << pos << endl;
    if (!cap.set(CAP_PROP_POS_FRAMES, pos))
    {
        cerr << "ERROR: seekeing is not supported" << endl;
    }
}

namedWindow("Laplacian", WINDOW_AUTOSIZE);
createTrackbar("Sigma", "Laplacian", &sigma, 15, 0);

Mat smoothed, laplace, result;

for(;;)
{
    Mat frame;
    cap >> frame;
    if( frame.empty() )
        break;

    int ksize = (sigma*5)|1;
    if(smoothType == GAUSSIAN)
        GaussianBlur(frame, smoothed, Size(ksize, ksize), sigma, sigma);
    else if(smoothType == BLUR)
        blur(frame, smoothed, Size(ksize, ksize));
    else
        medianBlur(frame, smoothed, ksize);

    Laplacian(smoothed, laplace, CV_16S, 5);
    convertScaleAbs(laplace, result, (sigma+1)*0.25);
    imshow("Laplacian", result);

    char c = (char)waitKey(30);
    if( c == ' ' )
        smoothType = smoothType == GAUSSIAN ? BLUR : smoothType == BLUR ? MEDIAN : GAUSSIAN;
    if( c == 'q' || c == 'Q' || c == 27 )
        break;
}

return 0;

}

Code for python: from __future__ import print_function

import numpy as np
import cv2 as cv
import sys

if __name__ == "__main__":
    print(__doc__)
    ddepth = cv.CV_16S
    sigma = 3
    cap=cv.VideoCapture(0)
    cv.namedWindow("Laplace of Image", cv.WINDOW_AUTOSIZE)
    cv.createTrackbar("Kernel Size Bar", "Laplace of Image", sigma, 15, lambda x:x)
    while(1):
        sigma = cv.getTrackbarPos("Kernel Size Bar", "Laplace of Image")
        ksize = (sigma*5)|1
        ret, frame = cap.read()
        if ret == False:
            continue
        src = cv.GaussianBlur(frame, (ksize, ksize), sigma, sigma)
        dst = cv.Laplacian(src, ddepth, 5)
        abs_dst = cv.convertScaleAbs(dst, (sigma+1)*0.25)
        cv.imshow("Laplace of Image", abs_dst)
        k = cv.waitKey(30) & 0xFF
        if k == 27:
            break
    cv.destroyAllWindows()
click to hide/show revision 2
None

updated 2019-03-13 11:03:03 -0600

berak gravatar image

Difference in same operations between Python and C++ OpenCV Code

I am converting laplace.cpp file present in samples/cpp to python code but the output that i am getting from python code is different from c++ output. Here is the code for c++:

#include "opencv2/videoio.hpp"

include "opencv2/highgui.hpp"

include "opencv2/imgproc.hpp"

include <ctype.h>

include <stdio.h>

include <iostream>

#include "opencv2/highgui.hpp" #include "opencv2/imgproc.hpp" #include <ctype.h> #include <stdio.h> #include <iostream> using namespace cv; using namespace std;

std; static void help() { cout << "\nThis program demonstrates Laplace point/edge detection using OpenCV function Laplacian()\n" "It captures from the camera of your choice: 0, 1, ... default 0\n" "Call:\n" "./laplace -c=<camera #,="" default="" 0=""> #, default 0> -p=<index of="" the="" frame="" to="" be="" decoded="" captured="" next="">\n" of the frame to be decoded/captured next>\n" << endl; }

} enum {GAUSSIAN, BLUR, MEDIAN};

MEDIAN}; int sigma = 3; int smoothType = GAUSSIAN;

GAUSSIAN; int main( int argc, char** argv ) { cv::CommandLineParser parser(argc, argv, "{ c | 0 | }{ p | | }"); help();

help();

    VideoCapture cap;
 string camera = parser.get<string>("c");
 if (camera.size() == 1 && isdigit(camera[0]))
     cap.open(parser.get<int>("c"));
 else
     cap.open(samples::findFileOrKeep(camera));
 if (!cap.isOpened())
 {
     cerr << "Can't open camera/video stream: " << camera << endl;
     return 1;
 }
 cout << "Video " << parser.get<string>("c") <<
     ": width=" << cap.get(CAP_PROP_FRAME_WIDTH) <<
     ", height=" << cap.get(CAP_PROP_FRAME_HEIGHT) <<
     ", nframes=" << cap.get(CAP_PROP_FRAME_COUNT) << endl;
 int pos = 0;
 if (parser.has("p"))
 {
     pos = parser.get<int>("p");
 }
 if (!parser.check())
 {
     parser.printErrors();
     return -1;
 }

 if (pos != 0)
 {
     cout << "seeking to frame #" << pos << endl;
     if (!cap.set(CAP_PROP_POS_FRAMES, pos))
     {
         cerr << "ERROR: seekeing is not supported" << endl;
     }
 }

 namedWindow("Laplacian", WINDOW_AUTOSIZE);
 createTrackbar("Sigma", "Laplacian", &sigma, 15, 0);

 Mat smoothed, laplace, result;

 for(;;)
 {
     Mat frame;
     cap >> frame;
     if( frame.empty() )
         break;

     int ksize = (sigma*5)|1;
     if(smoothType == GAUSSIAN)
         GaussianBlur(frame, smoothed, Size(ksize, ksize), sigma, sigma);
     else if(smoothType == BLUR)
         blur(frame, smoothed, Size(ksize, ksize));
     else
         medianBlur(frame, smoothed, ksize);

     Laplacian(smoothed, laplace, CV_16S, 5);
     convertScaleAbs(laplace, result, (sigma+1)*0.25);
     imshow("Laplacian", result);

     char c = (char)waitKey(30);
     if( c == ' ' )
         smoothType = smoothType == GAUSSIAN ? BLUR : smoothType == BLUR ? MEDIAN : GAUSSIAN;
     if( c == 'q' || c == 'Q' || c == 27 )
         break;
 }

 return 0;
}

}

Code for python: python:

from __future__ import print_function

print_function

import numpy as np
import cv2 as cv
import sys

if __name__ == "__main__":
    print(__doc__)
    ddepth = cv.CV_16S
    sigma = 3
    cap=cv.VideoCapture(0)
    cv.namedWindow("Laplace of Image", cv.WINDOW_AUTOSIZE)
    cv.createTrackbar("Kernel Size Bar", "Laplace of Image", sigma, 15, lambda x:x)
    while(1):
        sigma = cv.getTrackbarPos("Kernel Size Bar", "Laplace of Image")
        ksize = (sigma*5)|1
        ret, frame = cap.read()
        if ret == False:
            continue
        src = cv.GaussianBlur(frame, (ksize, ksize), sigma, sigma)
        dst = cv.Laplacian(src, ddepth, 5)
        abs_dst = cv.convertScaleAbs(dst, (sigma+1)*0.25)
        cv.imshow("Laplace of Image", abs_dst)
        k = cv.waitKey(30) & 0xFF
        if k == 27:
            break
    cv.destroyAllWindows()