calcBlurriness() not working in .exe [closed]

asked 2017-09-07 01:54:33 -0500

LifeIs42 gravatar image

updated 2017-09-07 02:02:59 -0500

Hi I am new to openCV and C++. I have found a grate function to test the blurriness of a webcam frames. I am saving individual frames from the webcam and then testing with the calcBlurriness function to see how focused the image is. Everything works very well but when I convert the project to an .EXE file the function calcBlurriness no longer works. The video capture works just fin and all other things work in the exe. I have narrowed it down to the if statement if( in this if statement dos not show up in the exe file but in the visual studio solution there is no problem I am using OpenCV 3 and visual studio 2107 community

#include "opencv2\opencv.hpp"
#include "opencv2\opencv.hpp"
#include "opencv2\highgui\highgui.hpp"
using namespace cv;
using namespace std;

// caculates how bluery the image is
float calcBlurriness(const Mat &src)
    Mat Gx, Gy;
    Sobel(src, Gx, CV_32F, 1, 0);
    Sobel(src, Gy, CV_32F, 0, 1);
    double normGx = norm(Gx);
    double normGy = norm(Gy);
    double sumSq = normGx * normGx + normGy * normGy;
    return static_cast<float>(1. / (sumSq / src.size().area() + 1e-6));

int main(int argc, char** argv)
    string FoucusNumber;

    cout << "Detect Blur Has Started" << endl;
    cout << "Pleases focus the camera until the Foucus Number # is lest then 0.000023" << endl;

    VideoCapture Capture(0); // open the default camera
    if (!Capture.isOpened())  // check if we succeeded
        cout << "Can't load WebCam" << endl;
        return -1;

    namedWindow("Video", 1); // open window for video stream

    while (1)
        Mat WebCamFrame;
        Capture >> WebCamFrame;         // get a new frame from web camera;      // Grabs, decodes and returns the next video frame.

        if (WebCamFrame.empty())
            cout << "WebCam frame is empty" << endl;
            return -1;

        stringstream file;
        file << "C:/Users/MyAccount/source/repos/Focus_Check/Focus_Check/test_blur" << ".jpg";
        imwrite(file.str(), WebCamFrame);
        char* TestImage = argc >= 2 ? argv[1] : (char*)"test_blur.jpg";
        Mat FrameToTest = imread(TestImage);

        if (
            //cout << "testing blurr" << calcBlurriness(src) << endl;
            FoucusNumber = "Foucus Number: " + to_string(calcBlurriness(FrameToTest));
        putText(WebCamFrame, FoucusNumber, Point(0, 450), 
                        CV_FONT_HERSHEY_PLAIN, 2, Scalar(0, 0, 0), 2, CV_AA);
        putText(WebCamFrame, "Foucus Number <= to 0.000023", Point(0, 425), 
                         CV_FONT_HERSHEY_PLAIN, 2, Scalar(0, 0, 0), 2, CV_AA);
        putText(WebCamFrame, "Press 'e' to escape", Point(0, 400), 
                         CV_FONT_HERSHEY_PLAIN, 2, Scalar(0, 0, 0), 2, CV_AA);
        imshow("Video", WebCamFrame);

        // Press 'c' to escape
        if (waitKey(30) == 'c')
    return 0;
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by sturkmen
close date 2017-09-08 13:09:14.434546



You're writing image to disk and loading it again for test ? Why you're not passing Webcamframe ?

Ziri gravatar imageZiri ( 2017-09-07 03:50:35 -0500 )edit

because i need evidence that the image has be focused properly.

LifeIs42 gravatar imageLifeIs42 ( 2017-09-07 17:41:16 -0500 )edit

I am now passing the webcam frame and yes the problem is solved but i don't understand why it doesn't work with the file. I would really appreciate if you explain the problem. Thank you for you help

LifeIs42 gravatar imageLifeIs42 ( 2017-09-07 18:08:20 -0500 )edit

I can think of two issues here :

You're mixing C and C++ Api : you'll have to use "const string" not "char* for image path. You're using absolute path to write and relative path to read image.

In general it's not a good practice to write image to disc and read it again ( you can do that in debug mode).

Ziri gravatar imageZiri ( 2017-09-07 20:11:18 -0500 )edit

Thank you for your help

LifeIs42 gravatar imageLifeIs42 ( 2017-09-07 20:17:29 -0500 )edit