Ask Your Question
0

Convert image to binary using openCV. Error: Assertion failed (scn==3 || scn==4)

asked 2014-07-17 03:58:58 -0600

hamybomi gravatar image

updated 2014-07-17 16:32:51 -0600

aledalgrande gravatar image

My simple code is as follows:

#include "iostream"

#include "opencv2/opencv.hpp"

using namespace std;
using namespace cv;

#define TRACKBAR_MAX_VALUE  255
#define THRESHOLD_MAX_VALUE 255 

Mat sourceImage;
Mat grayImage;
Mat binaryImage;

String windowNameBinarization = "Binarization";

int levels = 128;   

void binarization( void )
{
    cvtColor( sourceImage, grayImage, CV_BGR2GRAY );
    threshold( grayImage, binaryImage, levels, THRESHOLD_MAX_VALUE, THRESH_BINARY );
    imshow( windowNameBinarization, binaryImage );
}

void on_change ( int pos, void* )
{
    binarization();
}

int main( int argc, char **argv )
{
    String trackbarNameThreshold = "Threshold";
    sourceImage = imread("C:\2.jpg", 1);
    namedWindow( windowNameBinarization );
    createTrackbar( trackbarNameThreshold,  windowNameBinarization, &levels, TRACKBAR_MAX_VALUE, on_change );

    binarization();
    waitKey( 0 );

    return 0;
}
edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
2

answered 2014-07-17 04:23:16 -0600

You should check your imread returned value:

if( sourceImage.empty() )
{
    cerr << "Unable to load image" << endl;
    return -1;
}

It's probably wrong, as you have to escape \ character under C/C++:

sourceImage = imread("C:\\2.jpg", 1 );
edit flag offensive delete link more

Comments

1

Actually just use a front slash and all your problems will disappear :)

StevenPuttemans gravatar imageStevenPuttemans ( 2014-07-17 04:42:56 -0600 )edit

You right… ;-) I agree, using a C / C++ convention is always a better idea! Thanks.

Mathieu Barnachon gravatar imageMathieu Barnachon ( 2014-07-17 04:46:42 -0600 )edit

thanks for your comments, i tried it, but that error was still appeared...

hamybomi gravatar imagehamybomi ( 2014-07-17 04:53:20 -0600 )edit

Which error? The assertion or the "Unable to load image" message?

Mathieu Barnachon gravatar imageMathieu Barnachon ( 2014-07-17 04:56:21 -0600 )edit

yes, i couldn't unload image, and my error: assertion failed (scn==3 || scn==4) in unknow fuction

hamybomi gravatar imagehamybomi ( 2014-07-17 05:01:10 -0600 )edit

and this is the message error : Unhandled exception at 0x74bdc42d in text.exe

hamybomi gravatar imagehamybomi ( 2014-07-17 05:05:20 -0600 )edit

have you built your OpenCV with Qt support? Afaik that is needed for using the trackbar functionality.

StevenPuttemans gravatar imageStevenPuttemans ( 2014-07-17 05:46:19 -0600 )edit

no, i didn't use QT, i use VC 2010. what is QT?

hamybomi gravatar imagehamybomi ( 2014-07-17 06:00:51 -0600 )edit

Qt is a visualisation framework. As far as I know extra functionality for visualisation windows can only be achieved by having Qt support. It has nothing to do with VC2010, rather with the framework being on your system. However I am not quite sure so somebody should first of all confirm this one.

StevenPuttemans gravatar imageStevenPuttemans ( 2014-07-17 06:31:50 -0600 )edit

Question Tools

Stats

Asked: 2014-07-17 03:58:58 -0600

Seen: 689 times

Last updated: Jul 17 '14