Ask Your Question

When I use OpenCV 2.4,the image does not load!

asked 2015-10-15 07:22:50 -0500

Mehdi.Am gravatar image

updated 2015-10-16 03:23:53 -0500

In my prev problem, I found out that the image doesn't load in my project when I use OpenCV 2.4 so I couldn't create CSV file.

when I was using OpenCV 3.0.0 every thing was OK and I could load the image via imread

but now I'm using OpenCV 2.4 but the image doesn't load and always img.empty() command return true

here is my code but the img is null:

// faceRecognitionTest.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include <iostream>
#include <string>
#include "opencv2\contrib\contrib.hpp"
#include "opencv2\core\core.hpp"
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\objdetect\objdetect.hpp"
#include "opencv2\opencv.hpp"
#include "opencv2\imgproc\imgproc.hpp"
#include "FaceRec.h"

//file handling
#include <fstream>
#include <sstream>

using namespace std;
using namespace cv;

Mat img;

int _tmain(int argc, _TCHAR* argv[])
 img = imread("f.jpg",CV_LOAD_IMAGE_GRAYSCALE);
    if (img.empty())
        return -1;


    return 0;
edit retag flag offensive close merge delete


Your code looks fine. So, the thing to check will be if you are linking with the right libraries. Also, are the two versions of OpenCV compiled with the same version of Visual Studio as that makes a difference in some cases (as I discovered painfully).

unxnut gravatar imageunxnut ( 2015-10-15 08:46:39 -0500 )edit

you fail here if (b=img.empty())

pklab gravatar imagepklab ( 2015-10-15 08:52:53 -0500 )edit

b will always be assigned with the img.empty() value and this operation (the assign) is ok and you will always enter the if. So forget about b, why do you need it?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-10-15 10:06:40 -0500 )edit

The last two comments are completely incorrect. b = img.empty() will assign whatever is returned to b, and then b will be evaluated by the if statement. So if img.empty() is false then b will be set to false and you will not enter the if. The if statement is perfectly fine as is.

Brandon212 gravatar imageBrandon212 ( 2015-10-15 15:12:01 -0500 )edit

oops, I have made a confusion with the numbers... sorry

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-10-16 02:30:51 -0500 )edit

ok.. warning here: if (b=img.empty()) is better ;) anyway isn't related to the issue. sorry

pklab gravatar imagepklab ( 2015-10-16 06:12:19 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2015-10-15 15:12:49 -0500

Brandon212 gravatar image

Is f.jpg in the working directory of your program? Usually you want to put the full path to the image there just to ensure you're accessing the right location. My guess is you're not looking in the right place for the image, because the code looks fine overall.

edit flag offensive delete link more



I guess this is indeed the problem. Try using an absolute path instead of a relative one.

StevenPuttemans gravatar imageStevenPuttemans ( 2015-10-16 03:41:55 -0500 )edit

@StevenPuttemans I did it,but it's not working yet!

Mehdi.Am gravatar imageMehdi.Am ( 2015-10-17 01:46:42 -0500 )edit

@Mehdi.Am are you sure is .jpg and not .jpeg?

LorenaGdL gravatar imageLorenaGdL ( 2015-10-17 08:17:59 -0500 )edit

Question Tools

1 follower


Asked: 2015-10-15 07:22:50 -0500

Seen: 423 times

Last updated: Oct 16 '15