Reputation:
I am trying to perform an erosion of a JPEG image after converting the image to YCrCb image space. Using the following code
YCrCbMin.val[0] = 0;
YCrCbMin.val[1] = 131;
YCrCbMin.val[2] = 80;
YCrCbMax.val[0] = 255;
YCrCbMax.val[1] = 185;
YCrCbMax.val[2] = 135;
imshow("img",Img);// Img is the JPEG image I load off the disk.
waitKey(0);
Mat YCrCbImg;
cvtColor(Img, YCrCbImg, CV_BGR2YCrCb);
Mat erodedImg;
inRange(YCrCbImg, YCrCbMin, YCrCbMax, erodedImg);
Mat InterMediateImg = YCrCbImg;
IplConvKernel* element = new IplConvKernel();
element->nCols = 12; element->nRows = 12; element->anchorX = 6; element->anchorY = 6;
cvErode(&erodedImg, &InterMediateImg, element, 1);
element->nCols = 6; element->nRows = 6; element->anchorX = 3; element->anchorY = 3;
cvDilate(&InterMediateImg, &erodedImg, element, 2);
On the first erosion, I get the following error as shown in the screenshot.
I have used the following image as input
What am I doing wrong here??
Upvotes: 0
Views: 2758
Reputation: 3334
The error appears probably when you call cvErode
and cvDilate
. You are mixing the C and C++ interface. So, for example, in
cvErode(&erodedImg, &InterMediateImg, element, 1);
erodedImg
and InterMediateImg
are of type cv::Mat
when they should be of type cvMat
. The same thing for cvDilate
.
You can either use the new interface:
void erode(const Mat& src, Mat& dst, const Mat& element, Point anchor=Point(-1, -1), int iterations=1, int borderType=BORDER_CONSTANT, const Scalar& borderValue=morphologyDefaultBorderValue())
and
void dilate(const Mat& src, Mat& dst, const Mat& element, Point anchor=Point(-1, -1), int iterations=1, int borderType=BORDER_CONSTANT, const Scalar& borderValue=morphologyDefaultBorderValue())
or convert from cv::Mat
to cvMat
(link to cheatsheet):
CvMat cvmat = img; // convert cv::Mat -> CvMat
Also notice that when you call those functions, the destination matrix is not empty and probably does not have the same type as the source matrix so there is a chance you will run in an error there too if I am not mistaken.
Nice erosion and dilation tutorial in OpenCV 2.4.2
Upvotes: 2