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