Ask Your Question

Revision history [back]

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:

image description

CROPPED IMAGE WITH SOME EFFECTS:

image description

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

image description

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);