The use of OpenCV's Mat format and IplImage format

Recently, OpenCV's Mat image format and IplImage image format have been frequently used, so record some of their differences.

First post the method of mutual conversion:

Mat to IplImage as follows:

	//浅拷贝
	Mat mat_Image = imshow("1.bmp");
	IplImage image = cvIplImage(mat_Image);
    //深拷贝
    IplImage* temp;
    //temp = cvCreateImage(cvSize(mat_Image.rows, mat_Image.cols), 8, 1);//灰度图
    temp = cvCloneImage(&image);

IplImage to Mat is as follows:

    IplImage* src=cvLoadImage("temp.jpg");
    Mat ImgTemp;
    ImgTemp=cvarrToMat(src);//浅拷贝
    Mat Img = ImgTemp.clone();//利用clone()再次深拷贝

The difference in the method of loading

	//Mat的加载方法
	//第一个参数代表图片路径和名称,第二个参数代表0-》灰度加载、1-》彩色加载
	Mat mat = imread("1.bmp", 0);

	//IplImage的加载方法
	IPlImage *Image = cvLoadImage("1.bmp",0);

Set window size and display

	//Mat的图片只能用imshow()来显示
	Mat mat = imread("1.bmp", 0);
    namedWindow("mat",0);  //第二个参数 0-》窗口可以自定义大小、1-》自适应图片大小
    resizeWindow("mat",800,600);
    imshow("mat", mat);

	//IplImage的图片只能用cvShowImage()来显示
	IPlImage *Image = cvLoadImage("1.bmp",0);
	cvNamedWindow("Image", 0);
    cvResizeWindow("Image", cvSize(1200, 900));
    cvShowImage("Image", Image);
	
	/*
	备注:无论是namedWindow和cvNamedWindow,还是resizeWindow和cvResizeWindow,
	用法和效果是一样的,对于窗口的操作不分图片格式。
	*/

There are pit records

Problems with cvNamedWindow, cvShowImage and multithreading

When calling cvShowImage in a child thread, there will be no response and no display of the window, regardless of whether cvNamedWindow is called in advance in the child thread.

However, calling cvNamedWindow in the main thread in advance, and then calling cvShowImage in the sub-thread will display normally.

If the window generated by cvNamedWindow is closed before the child thread calls cvShowImage, the window will also be unresponsive and undisplayed.
Reference link: https://blog.csdn.net/qwertyuj/article/details/7406456

Guess you like

Origin blog.csdn.net/weixin_44650358/article/details/125780003