Abhinav S Rai
Abhinav S Rai

Reputation: 33

Assertion failed in unknown function

I am using this program to just read and display an image. I dont know why it is showing this odd error:

assertion failed (scn==3 || scn ==4) in unknown function,file......\src\modules\imgproc\src\color.cpp line 3326

I changed some images, sometimes it runs without error but, even when it runs and everything, it is showing the window but not the image in it. What is wrong?

#include "stdafx.h"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"


using namespace cv;
using namespace std;

void main()
{

    Mat leftImg,frame=imread("C:\\Users\\user\\Downloads\\stereo_progress.png");
    leftImg=imread("C:\\Users\\user\\Downloads\\dm_sl.gif");//add of left camera


    cvtColor(leftImg,leftImg,CV_BGR2GRAY);
    imwrite("imreadtest.txt",leftImg);


    imshow("cskldnsl",leftImg);
    getchar();
}

Upvotes: 3

Views: 3751

Answers (3)

Barshan Das
Barshan Das

Reputation: 3767

  1. As answered by others, make sure the parameter1 in cvtColor is not 1 channel image. check it by type(). it should be CV_8UC3 and etc.

  2. Put waitKey after imshow. Image will show up.

  3. I do not know why you are saving leftImg in imreadtest.txt. [ Though it is not making the error.]

Upvotes: 3

Niko
Niko

Reputation: 26730

You cannot use the same matrix for both the input matrix and the output matrix when using cvtColor(). If you don't need the colored image later on, passing a copy is a straightforward solution:

cvtColor(leftImg.clone(), leftImg, CV_BGR2GRAY);

Another solution is using a fresh output matrix:

Mat leftImgGray;
cvtColor(leftImg, leftImgGray, CV_BGR2GRAY);
imshow("cskldnsl",leftImgGray);

Upvotes: 1

sansuiso
sansuiso

Reputation: 9379

First, make sure that the image was correctly loaded by testing for leftImg.data != 0.

Then, you can force the number of channels by passing as second parameter to cv::imread() the value CV_LOAD_IMAGE_GRAYSCALE or CV_LOAD_IMAGE_COLOR in order to ensure that you load a grayscale (1 channel) or color (3 channels) image, whatever the type of the image file is.

Upvotes: 1

Related Questions