3

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();
}
888
  • 3,246
  • 8
  • 41
  • 60
Abhinav S Rai
  • 33
  • 1
  • 4

3 Answers3

3
  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.]

Barshan Das
  • 3,677
  • 4
  • 32
  • 46
1

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.

sansuiso
  • 9,259
  • 1
  • 40
  • 58
1

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);
Niko
  • 26,516
  • 9
  • 93
  • 110
  • Dear Niko thank you for the help and i would like to add that apart from this until i renoved getchar() and used waitKEy instead i couldnt get the image the window would come no errors shown couts written after the imshow would also get executed but only on removing getchar() it went away completely any hints y is it so ? – Abhinav S Rai Apr 12 '13 at 13:53
  • @AbhinavSRai The `waitKey()` is required because OpenCV processes GUI events (such as creating / updating windows) only when `waitKey()` is called. See http://stackoverflow.com/questions/5217519/opencv-cvwaitkey – Niko Apr 14 '13 at 20:52