Smoothing out edges - Converting an 8UC4 Mat to a 32FC4 Mat

asked 2018-02-26 16:12:38 -0500

GregF458XVM gravatar image

I want to smooth out the edges of an object I'm extracting from by using the following lines of code from a StackOverflow question:

cv::namedWindow("result");
Mat img=imread("TestImg.png");
Mat whole_image=imread("D:\\ImagesForTest\\lena.jpg");
whole_image.convertTo(whole_image,CV_32FC3,1.0/255.0);
cv::resize(whole_image,whole_image,img.size());
img.convertTo(img,CV_32FC3,1.0/255.0);

Mat bg=Mat(img.size(),CV_32FC3);
bg=Scalar(1.0,1.0,1.0);

// Prepare mask
Mat mask;
Mat img_gray;
cv::cvtColor(img,img_gray,cv::COLOR_BGR2GRAY);
img_gray.convertTo(mask,CV_32FC1);
threshold(1.0-mask,mask,0.9,1.0,cv::THRESH_BINARY_INV);

cv::GaussianBlur(mask,mask,Size(21,21),11.0);
imshow("result",mask);
cv::waitKey(0);

However, I am running into this assertion failure here:

OpenCV Error: Assertion failed (((((sizeof(size_t)<<28)|0x8442211) >> ((traits::Depth<_Tp>::value) & ((1 << 3) - 1))*4) & 15) == elemSize1()) in cv::Mat::at

I traced the problem down to something similar to this line:

whole_image.convertTo(whole_image,CV_32FC3,1.0/255.0);

From using the code from a StackOverflow.com answer, I noticed that the type of image I loaded is 8UC4.

How can I convert an 8UC4 Mat to 32FC4?

edit retag flag offensive close merge delete

Comments

If you load a 8UC4 then it means you are not using the code above, because imread standardly using IMREAD_COLOR which is 8UC3 ... so how did you define that you have 8UC4? Are you reading an RGB-D image or am image with an IR channel?

StevenPuttemans gravatar imageStevenPuttemans ( 2018-02-27 04:59:48 -0500 )edit

I am using the following line to convert the Mat from 8UC3 to 8UC4: cvtColor(img, img, COLOR_BGR2BGRA);

If cvtColor is used instead, what's the third argument I need to specify to make the resulting Mat type 32FC4?

GregF458XVM gravatar imageGregF458XVM ( 2018-02-27 07:51:11 -0500 )edit