CvMat和Imread与IpImage和CvLoadImage [英] CvMat and Imread Vs IpImage and CvLoadImage
问题描述
使用OpenCv 2.4
我有两种选择来加载图像:
I have two options to load images:
1- CvMat and Imread
2- IpImage and CvLoadImage
哪个更好用?我尝试将两者混合并最终导致段错误.
Which one is better to use? I tried mixing the two and end up in seg fault.
推荐答案
imread
返回Mat
,而不是CvMat
.它们是两个不同的接口(对于C ++和Ipl ...,为Mat
/imread
;对于C接口,为Cv ..).
imread
returns a Mat
, not CvMat
. They are the two different interfaces (Mat
/imread
for C++ and Ipl... and Cv.. for C interface).
C ++接口更好,更安全,更易于使用.它会自动为您处理内存,并允许您为同一任务编写更少的代码. OpenCV的人提倡使用C ++,除非某些非常具体的项目要求迫使您使用C.
The C++ interface is nicer, safer and easier to use. It automatically handles memory for you, and allows you to write less code for the same task. The OpenCV guys advocate for the usage of C++, unless some very specific project requirements force you to C.
示例(C ++)
cv::Mat image = imread("path/to/myimage.jpg")
if(image.empty())
return;
cv::imshow("Image", image);
cv::Mat bw = image > 128; // threshold image
cv::Mat crop = image(cv::Rect(0, 0, 100, 100)); // a 100px x 100px crop
crop= 0; // set image to 0
cv::waitKey();
// end code here
请注意,除非另有说明,否则所有矩阵分配都引用相同的数据.在上面的示例中,crop
矩阵指向image
,并将其设置为零会将image
的特定部分设置为0.
Note that if not stated otherwise, all matrix assignments reference the same data. In the example above, the crop
matrix points to image
, and setting it to zero will set that specific part of the image
to 0.
要创建新的数据副本,请使用Mat :: copyTo或Mat :: clone();
To create a new copy of data, use Mat::copyTo, or Mat::clone();
还有C接口
IplImage* pImg = CvLoadImage("path/to/myimage.jpg");
if(pImg == NULL)
return;
// ... big bloat to do the same operations with IplImage
CvShowImage("Image", pImg);
cvWaitKey();
CvReleaseImage(&pImg); // Do not forget to release memory.
// end code here
这篇关于CvMat和Imread与IpImage和CvLoadImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!