[英]Divide Ipl image by a scalar in OpenCV
I am trying to divide 2 by all the matrix elements of an Ipl image using cvDiv with C API of OpenCV. 我正在尝试使用cvDiv和OpenCV的C API将Ipl图像的所有矩阵元素除以2。 My code is as follows:
我的代码如下:
IplImage* src = cvLoadImage(argv[1]);
CvMat* src1 = cvCreateMat(src->height, src->width, CV_16UC3);
cvDiv(src, src1, double scale=2);
But, I am getting the following error: 但是,我收到以下错误:
error: expected primary-expression before ‘double’
Can anybody tell why? 谁能告诉我为什么? Or is there any other way to divide all the elements of a matrix by a particular number say 2?
还是有其他方法将矩阵的所有元素除以特定的数字(例如2)?
You can do it like this. 您可以这样做。 But as I said it is quite diffucult and verbose to do this in the C-api but it is possible.
但是正如我说的那样,在C-api中做到这一点非常困难和冗长,但是有可能。 In openCV C-Api it is really important that the matrices of the images are of the same type.
在openCV C-Api中,图像矩阵的类型相同是非常重要的。 Therefore you will not be able to do this with your
因此,您将无法使用
CvMat* src1 = cvCreateMat(src->height, src->width, CV_16UC3);
but try it like this this works like a charm on my machine. 但是像这样尝试一下,这就像在我的机器上一样具有魅力。
IplImage* src = cvLoadImage(argv[1]);
/*This ensures you'll end up with an image of the same type as the source image.
*and it also will allocate the memory for you this must be done ahead.*/
IplImage* dest = cvCreateImage(
cvSize(src->width, src->height),
src->depth,
src->nChannels
);
/*we use this for the division*/
IplImage* div= cvCreateImage(
cvSize(src->width, src->height),
src->depth,
src->nChannels
);
/*this sets every scalar to 2*/
cvSet( div, cvScalar(2,2,2), NULL);
cvDiv( src, div, dest, 1 );
The above will get the job done using the C-api the same can be achieved with the following code using the C++-api and this is what I would recommend to anyone starting with opencv since I feel the C-api is much more verbose, and difficult. 上面的代码可以使用C-api来完成工作,使用下面的代码使用C ++-api可以完成相同的工作,这是我向以opencv开头的任何人的建议,因为我觉得C-api更加冗长,和困难。 C++ matrix contructors and operator overloading make the task almost trivial:
C ++矩阵构造函数和运算符重载使这项任务变得微不足道:
cv::Mat Src2 = cv::imread( argv[1] );
cv::Mat Dest2 = Src2 / 2;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.