Equivalent OpenCV Java Code to this C++ Code

asked 2018-03-08 09:32:45 -0500

Udaib khan gravatar image

updated 2018-03-08 09:37:09 -0500

I need java equivalent code to this c++ code :

void overlayImage(const cv::Mat &background, const cv::Mat &foreground, cv::Mat &output, cv::Point2i location)
{
    background.copyTo(output);

    // start at the row indicated by location, or at row 0 if location.y is negative.
    for (int y = std::max(location.y , 0); y < background.rows; ++y) {
        int fY = y - location.y; // because of the translation

        // we are done of we have processed all rows of the foreground image.
        if (fY >= foreground.rows)
            break;

        // start at the column indicated by location, or at column 0 if location.x is negative.
        for (int x = std::max(location.x, 0); x < background.cols; ++x) {
            int fX = x - location.x; // because of the translation.

            // we are done with this row if the column is outside of the foreground image.
            if (fX >= foreground.cols)
                break;

            // determine the opacity of the foregrond pixel, using its fourth (alpha) channel.
            double opacity = ((double)foreground.data[fY * foreground.step + fX * foreground.channels() + 3]) / 255.;

            // and now combine the background and foreground pixel, using the opacity, but only if opacity > 0.
            for (int c = 0; opacity > 0 && c < output.channels(); ++c) {
                unsigned char foregroundPx = foreground.data[fY * foreground.step + fX * foreground.channels() + c];
                unsigned char backgroundPx = background.data[y * background.step + x * background.channels() + c];
                output.data[y*output.step + output.channels()*x + c] = backgroundPx * (1.-opacity) + foregroundPx * opacity;
            }
        }
    }
}

i have tried but it's not properly displacing pixels at output matrix:

public void overlayImage(final Mat background,final Mat foreground,Mat output, Point location) {
        try {
            background.copyTo(output);

            Mat output2=new Mat();
            Imgproc.cvtColor(output,output2,Imgproc.COLOR_RGBA2BGR);

            // start at the row indicated by location, or at row 0 if location.y is negative.
            for (int y = (location.y >0)?(int)location.y:0; y < background.rows(); ++y)
            {
                int fY = y - (int) location.y; // because of the translation

                // we are done of we have processed all rows of the foreground image.
                if (fY >= foreground.rows())
                    break;

                // start at the column indicated by location, or at column 0 if location.x is negative.
                for (int x = (location.x > 0) ? (int) location.x : 0; x < background.cols(); ++x) {
                    int fX = x - (int) location.x; // because of the translation.

                    // we are done with this row if the column is outside of the foreground image.
                    if (fX >= foreground.cols())
                        break;

                    // determine the opacity of the foregrond pixel, using its fourth (alpha) channel.
                    // double opacity = ((double)foreground.data[fY * foreground.step + fX * foreground.channels() + 3]) / 255.;
                    double opacity = foreground.get(fY, fX)[2]/ 255.;



                    // and now combine the background and foreground pixel, using the opacity, but only if opacity > 0.
                    for (int c = 0; opacity > 0 && c < output.channels(); ++c) {
                        double backgroundPx= background.get(fY, fX)[2] / 255.+c;
                        double foregroundPx = foreground.get(fY, fX)[2] / 255.+ c;
                        output2.put(y, x, ((backgroundPx * (1. - opacity)) + (foregroundPx * opacity)));
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();

Its giving error at this ... (more)

edit retag flag offensive close merge delete

Comments

putText(y, x, ((backgroundPx * (1. - opacity)) + (foregroundPx * opacity)));

supra56 gravatar imagesupra56 ( 2018-03-09 21:33:48 -0500 )edit

@supra56 putText is expecting others params

Udaib khan gravatar imageUdaib khan ( 2018-03-12 05:58:12 -0500 )edit