Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

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;

}

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

My simple code is as follows:

#include "iostream"
 
#include "opencv2/opencv.hpp"

#include "iostream"

#include "opencv2/opencv.hpp"

using namespace std;

std; using namespace cv;

cv;

#define TRACKBAR_MAX_VALUE 255

255 #define THRESHOLD_MAX_VALUE 255

Mat sourceImage;

sourceImage; Mat grayImage; Mat binaryImage;

Mat grayImage;

Mat binaryImage;

String windowNameBinarization = "Binarization";

"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(); }

{

binarization();

}

int main( int argc, char **argv )

{

) { String trackbarNameThreshold = "Threshold";

"Threshold"; sourceImage = imread("C:\2.jpg", 1);

1); namedWindow( windowNameBinarization );

); createTrackbar( trackbarNameThreshold, windowNameBinarization, &levels, TRACKBAR_MAX_VALUE, on_change );

);

binarization();

binarization(); waitKey( 0 );

);

return 0;

}

0; }

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

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;
}