Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
545 views
in Technique[技术] by (71.8m points)

android - add watermark small image to large image opencv4android

I've been trying to add an image to large image at desired position. I found a way to add watermark in opencv by doing addWeighted(src, alpha, water,--,dst,--), but the problem is that both watermark and surface image should be of same sizes which I don't want.

Found another way (I guess)

Mat srcMat = cvCanvasImage.submat(top/2, (top + height)/2, left/2, (left + width)/2);
Imgproc.cvtColor(mat, srcMat, Imgproc.COLOR_GRAY2BGR,4);

But I don't understand how to do this??

Thanks..

UPDATE

     Mat cvCanvasImage = Highgui.imread(Environment.getExternalStorageDirectory() + "/wallpapers/castle.jpg");

 // Small watermark image
Mat cvWaterImage = Highgui.imread(Environment.getExternalStorageDirectory() +"/square.png");

Size canvasSize = cvWaterImage.size();

   int rows = (int) canvasSize.height;
   int cols = (int) canvasSize.width;       
   int left = 0;
   int top = 0;         
   int width = rows;
   int height = cols;

   Rect ROI = new Rect(left, top, width, height);
   Core.addWeighted(cvCanvasImage.submat(ROI), alpha, cvWaterImage, beta, 0, cvCanvasImage.submat(ROI)); 

  //now it throws me this error   

                             "error: (-209) The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array' in function void cv::arithm_op(const cv::_InputArray&, const cv::_InputArray&, const cv::_OutputArray&, const cv::_InputArray&, int, void (**)(const uchar*, size_t, const uchar*, size_t, uchar*, size_t, cv::Size, void*), bool, void*)"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I found a way to do it right.

Mat b = Highgui.imread(Environment.getExternalStorageDirectory() + "/castle.jpg");

// Small watermark image
Mat a = Highgui.imread(Environment.getExternalStorageDirectory() +"/square.png");

Mat bSubmat = b.submat(a.rows(), a.rows()*2, a.cols(), a.cols()*2);        
a.copyTo(bSubmat);

Highgui.imwrite("mnt/sdcard/SubmatCopyToTest.png", b);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...