Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

calcBlurriness() not working in .exe

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(FrameToTest.data).Anything 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
        Capture.read(WebCamFrame);      // 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 (FrameToTest.data)
        {
            //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')
        {
            break;
        }
    }
    return 0;
}

calcBlurriness() not working in .exe

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(FrameToTest.data).Anything 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
        Capture.read(WebCamFrame);      // 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 (FrameToTest.data)
        {
            //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')
        {
            break;
        }
    }
    return 0;
}