user2873372
user2873372

Reputation: 35

OpenCV Java inpaint image format requirement

Been trying to get inpainting to work on Android,

int height = (int) viewMat.size().height;
int width = (int) viewMat.size().width;

Mat maskMat = new Mat();
maskMat.create(viewMat.size(), CvType.CV_8U);
maskMat.setTo(bColor);

Point r1 = new Point(width/2-width/10, height/2-height/10);
Point r2 = new Point(width/2+width/10, height/2+height/10);
Scalar color = new Scalar(1);

Core.rectangle(maskMat, r1, r2, color, Core.FILLED);

outMat.create(viewMat.size(), CvType.CV_8UC3);

viewMat.convertTo(outMat, CvType.CV_8UC3);

Photo.inpaint(outMat, maskMat, outMat, 1, Photo.INPAINT_TELEA);

Was greeted with,

Caused by: CvException [org.opencv.core.CvException: /home/reports/ci/slave_desktop/50-SDK/opencv/modules/photo/src/inpaint.cpp:744: 
error: (-210) Only 8-bit 1-channel and 3-channel input/output images are supported in function void cvInpaint(const CvArr*, const CvArr*, CvArr*, double, int)

in logcat.

Been trying for hours creating Mats in various ways but to no valid.

CV_8U = 8 bit per channel, 1 channel. Right?

CV_8UC3 = 8 bit per channel, 3 channels. Right?

So what am I missing? I'm totally stumped.

Upvotes: 1

Views: 2380

Answers (2)

uzairshahid901
uzairshahid901

Reputation: 39

Image Inpaint Using OpenCv Android Studio

 ImgMat = Mat()
 Maskmat = Mat()
 destmat=Mat()

 Utils.bitmapToMat(BitmapImage, ImgMat)
 Utils.bitmapToMat(BitmapMask, Maskmat)

 Imgproc.cvtColor(ImgMat, ImgMat, Imgproc.COLOR_RGB2XYZ)
 Imgproc.cvtColor(Maskmat, Maskmat, Imgproc.COLOR_BGR2GRAY)

 Photo.inpaint(ImgMat,Maskmat,destmat, 15.0, INPAINT_NS)

 InpaintBitmap= Bitmap.createBitmap(BitmapImage.getWidth(), 
 BitmapImage.getHeight(), 
 Bitmap.Config.ARGB_8888)

 Imgproc.cvtColor(destmat, destmat, Imgproc.COLOR_XYZ2RGB);

 Utils.matToBitmap(destmat, InpaintBitmap)

Description: InpaintBitmap is an Inpaint color Bitmap src image is a 3 channel image but mask bitmap is a 1 channel image


Upvotes: 0

user2873372
user2873372

Reputation: 35

...
Point r2 = new Point(width/2+width/10, height/2+height/10);
Scalar color = new Scalar(1);

Core.rectangle(maskMat, r1, r2, color, Core.FILLED);

Imgproc.cvtColor(viewMat, outMat, Imgproc.COLOR_RGBA2RGB);

Photo.inpaint(outMat, maskMat, outMat, 1, Photo.INPAINT_TELEA);
...

Turned out it was simply a matter of getting rid of the alpha channel via color conversion.

Upvotes: 1

Related Questions