2012-10-04 48 views
5

Tôi đã cố gắng thêm hình ảnh vào hình ảnh lớn ở vị trí mong muốn. Tôi tìm thấy một cách để thêm watermark trong opencv bằng cách làm addWeighted (src, alpha, water, -, dst, -), nhưng vấn đề là cả watermark và hình ảnh bề mặt phải có cùng kích thước mà tôi không muốn.thêm hình mờ nhỏ vào hình ảnh lớn opencv4android

Tìm thấy một cách khác (tôi đoán)

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

Nhưng tôi không hiểu làm thế nào để làm điều này ??

Cảm ơn ..

CẬP NHẬT

 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*)" 

Trả lời

5

Tôi tìm thấy một cách để làm điều đó đúng.

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

Something như thế này nên làm việc:

Mat waterMark = new Mat(width, height); //assumed as a smaller image than your source mat, with size (width, height). 

Rect ROI = new Rect(x, y, width, height); // Position and size of your watermark; 

Core.addWeighted(source.submat(ROI), alpha, waterMark, beta, gamma, source.submat(ROI)); 
+0

Tôi đã áp dụng cách của bạn và cập nhật câu hỏi của tôi, vui lòng xem. cảm ơn anyway cho phản ứng nhanh chóng. – Khawar

+0

đã giải quyết nó, cảm ơn – Khawar

Các vấn đề liên quan