Help with alpha gradient
HI all! I'm using opencv for Unity in c# but I think is very similar to opencv for Java
I think my problem is a simple one, but I'm new to opencv and there are a lot of things that I don't know...
This is my situation: From a picture I need to cut the face and apply some sketch effect.
PICTURE:
CROPPED IMAGE WITH SOME EFFECTS:
What I need is a gradient alpha border of the ellipse in the final image. Something like the next image but with a gradient in the border of the ellipse
This is my code:
Mat mask = new Mat(rgbaMat.rows(), rgbaMat.cols(), CvType.CV_8UC4, new Scalar(0, 0, 0, 0));
Mat face = new Mat(rgbaMat.rows(), rgbaMat.cols(), CvType.CV_8UC4, new Scalar(0, 0, 0, 0));
OpenCVForUnity.Rect rectCrop = new OpenCVForUnity.Rect((int)x, (int)y, (int)width, (int)height);
RotatedRect rRect = new RotatedRect(new Point(centerX, centerY), new Size(width, height), 0);
Imgproc.ellipse(mask, rRect, new Scalar(255, 255, 255, 255), -1);
Imgcodecs.imwrite(Application.dataPath + "/mask.png", mask);
Mat abs_grad_x = new Mat(rgbaMat.rows(), rgbaMat.cols(), CvType.CV_16SC4, new Scalar(0, 0, 0, 0));
Mat abs_grad_y = new Mat(rgbaMat.rows(), rgbaMat.cols(), CvType.CV_16SC4, new Scalar(0, 0, 0, 0));
Mat grad_x = new Mat(rgbaMat.rows(), rgbaMat.cols(), CvType.CV_16SC4, new Scalar(0, 0, 0, 0));
Mat grad_y = new Mat(rgbaMat.rows(), rgbaMat.cols(), CvType.CV_16SC4, new Scalar(0, 0, 0, 0));
int delta = 0;
int scale = 1;
int ddepth = CvType.CV_16S;
Mat final = new Mat(rgbaMat.rows(), rgbaMat.cols(), CvType.CV_16SC4, new Scalar(0, 0, 0, 0));
Imgproc.GaussianBlur(mask, mask, new Size(42, 42), 0);
rgbaMat.copyTo(face, mask);
face = face.submat(rectCrop);
Imgproc.GaussianBlur(face, face, new Size(21, 21), 0);
Imgproc.cvtColor(face, face, Imgproc.COLOR_RGBA2GRAY);
Imgproc.Scharr(face, grad_x, ddepth, 1,0,scale, delta, 0);
Core.convertScaleAbs(grad_x, abs_grad_x);
Imgproc.Scharr(face, grad_y, ddepth,0, 1, scale, delta, 0);
Core.convertScaleAbs(grad_y, abs_grad_y);
Core.addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, final);
Core.bitwise_not(final, final);
Imgproc.threshold(final, final, 230,255, Imgproc.THRESH_BINARY);
may be this can help you. We cannot help you with unity or 3rd party wrappers
Hi, thank you for the answer but I'm already doing the face detection to make the ellipse in the right place. I'm having big troubles making the alpha gradient...I undertsand the wrapper problem but I think that here is not related. The problem could be discuss without code...I would need to know the right steps to do for making a gradient. There are some knowledges that I'm missing, can You help me with that? Thank you!
i think, he wants some kind of gaussian "vignette"
(and yea, though unity itself is c#, the opencv asset is actually using java there)
I don't know what is alpha gradient in image processing. Now if you want to locate ellipse border you draw ellipse with a thickness of t. You use result as a mask
Sorry for my english....I need something like this: https://www.lifewire.com/thmb/elQL7F_...
@LBerger -- the ellipse thing is exactly, what's already happening in the code above .. (ellipse mask & copyTo())
Ok then you can use this post result you can use as a weight is maskBlur : value near boundary are small and near center are constant
Ok thank you! I'll try and I'll let you know