android - Error when adding two Mats in OpenCV, Java -
i writing opencv based image processing algorythm thresholding. algorythm written here in c++ language , rewriting in on java, android studio. in 1 line, have add 2 mat (opencv matrix) objects. in c++ res=img+res;
, in java core.add(imgmat, res, res);
. @ line error, cannot solve:
cvexception: /volumes/./././././././arithm.cpp:639: error: (-209) operation neither 'array op array' (where arrays have same size , same number of channels), nor 'array op scalar', nor 'scalar op array' in function void cv::arithm_op(...)
in code below see, both mat objects have same size , has same format (cvtype) too. again, how code looks in c++, can see in this question.
my code (java):
public bitmap thresholding(bitmap bitmap) { mat imgmat = new mat(); utils.bitmaptomat(bitmap, imgmat); imgmat.convertto(imgmat, cvtype.cv_32fc1, 1.0 / 255.0); mat res = calcblockmeanvariance(imgmat, 21); core.subtract(new matofdouble(1.0), res, res); core.add(imgmat, res, res); imgproc.threshold(res, res, 0.85, 1, imgproc.thresh_binary); //imgproc.resize(res, res, new org.opencv.core.size(res.cols() / 2, res.rows() / 2)); res.convertto(res, cvtype.cv_8uc1, 255.0); utils.mattobitmap(res, bitmap); return bitmap; } public mat calcblockmeanvariance (mat img, int blockside) { mat = new mat(); mat resmat; mat inpaintmask = new mat(); mat patch; mat smallimg = new mat(); matofdouble mean = new matofdouble(); matofdouble stddev = new matofdouble(); img.convertto(i, cvtype.cv_32fc1); resmat = mat.zeros(img.rows() / blockside, img.cols() / blockside, cvtype.cv_32fc1); (int = 0; < img.rows() - blockside; += blockside) { (int j = 0; j < img.cols() - blockside; j += blockside) { patch = new mat(i,new rect(j,i, blockside, blockside)); core.meanstddev(patch, mean, stddev); if (stddev.get(0,0)[0] > 0.01) resmat.put(i / blockside, j / blockside, mean.get(0,0)[0]); else resmat.put(i / blockside, j / blockside, 0); } } imgproc.resize(i, smallimg, resmat.size()); imgproc.threshold(resmat, inpaintmask, 0.02, 1.0, imgproc.thresh_binary); mat inpainted = new mat(); imgproc.cvtcolor(smallimg, smallimg, imgproc.color_rgba2bgr); smallimg.convertto(smallimg, cvtype.cv_8uc1, 255.0); inpaintmask.convertto(inpaintmask, cvtype.cv_8uc1); photo.inpaint(smallimg, inpaintmask, inpainted, 5, photo.inpaint_telea); imgproc.resize(inpainted, resmat, img.size()); resmat.convertto(resmat, cvtype.cv_32fc1, 1.0 / 255.0); return resmat; }
thank in advance.
while imgmat
, res
have same size, have different number of channels: imgmat
has 4 channels , res
has 3 channels.
since can add
2 matrices if have same size , number of channels, can convert imgmat
3 channel image before calling add
like:
imgproc.cvtcolor( imgmat, imgmat, imgproc.color_bgra2bgr);
Comments
Post a Comment