Dispalcement map filter using opencv java

asked 2018-03-06 07:10:11 -0500

Udaib khan gravatar image

updated 2018-03-07 12:59:52 -0500

i want to place on logo image on shirt image such that logo conform to curvature of shirt image using displacement map filter. But i am facing some issues kindly correct me where i am doing mistakes. Problem: Provided data element number (0) should be multiple of the Mat channels count (3)

Here is my code snippet:

public void displacementMapFilter(final Mat map, final Mat target,int componentX, int componentY, int scaleX, int scaleY,Mat output) 
{

        output.create(target.rows(), target.cols(), target.type());
// work with pixels
//
        for (int x = 0; x < output.rows(); x++)
            for (int y = 0; y < output.cols(); y++) {
            /* Formula:
            *  dstPixel[x, y] = srcPixel[x + ((componentX(x, y) - 128) * scaleX) / 256,
            *                            y + ((componentY(x, y) - 128) * scaleY) / 256)]
            */
                int dx, dy;

                int numChannels = map.channels();//is 3 for 8UC3 (e.g. RGB)
                int frameSize = map.rows() * map.cols();
                int numChannels2 = map.channels();//is 3 for 8UC3 (e.g. RGB)
                int frameSize2 = output.rows() * map.cols();
                byte[] mapByteBuffer = new byte[frameSize * numChannels];
                byte[] byteBuffer = new byte[frameSize * numChannels];
                byte[] outputByteBuffer = new byte[frameSize * numChannels];

                dx = x + (getComponent(map.get(x, y), componentX) - 128) * scaleX / 256;
                if (dx < 0) dx = 0;
                if (dx >= output.rows()) dx = output.rows();

                dy = y + (getComponent(map.get(x, y), componentY) - 128) * scaleY / 256;
                if (dy < 0) dy = 0;
                if (dy >= output.cols()) dy = output.cols();

                //output.at<cv::Vec4b>(x, y) = target.at<cv::Vec4b>(dx, dy);
                output.put(x, y, target.get(dx, dy));

            }

        }
    }



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

                for (int y = (location.y >0)?(int)location.y:0; y < background.rows(); ++y)
                {
                    int fY = y - (int) location.y; // because of the translation

                    if (fY >= foreground.rows())
                        break;

                    for (int x = (location.x > 0) ? (int) location.x : 0; x < background.cols(); ++x) {
                        int fX = x - (int) location.x; 
                        if (fX >= foreground.cols())
                            break;
                        double opacity = foreground.get(fY, fX)[2]/ 255.;
                        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;
                            output.put(y, x, ((backgroundPx * (1. - opacity)) + (foregroundPx * opacity)));
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
edit retag flag offensive close merge delete

Comments

@berak can you please correct me here where i'm doing mistake

Udaib khan gravatar imageUdaib khan ( 2018-03-06 10:08:21 -0500 )edit
  1. get a debugger, and find out, where exactly it happens
  2. build a small test case, that reproduces your problem
  3. come back with that, and we talk.
  4. "where i'm doing mistake" -- abusing opencv for what you should do in opengl, in the 1st place. displacement mapping in 2d ? in software ? in java ? in opencv ?, --seriously ?
berak gravatar imageberak ( 2018-03-06 11:47:29 -0500 )edit

@berak yes i'm using opencv because i don't want to go for 3D as i have all my images in 2D and it's possible in opencv with c++ but i am converting it in java. here is link where it's possible in opencv c++ Displacement Map Filter using OpenCV

Udaib khan gravatar imageUdaib khan ( 2018-03-07 02:53:27 -0500 )edit

@berak here the error basically occur at this point:

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

i have c++ version of this code :

output.data[y*output.step + output.channels()*x + c] = backgroundPx * (1.-opacity) + foregroundPx * opacity;

Is above java equivalent for this c++ code is right or not

Udaib khan gravatar imageUdaib khan ( 2018-03-07 12:52:43 -0500 )edit

i've given up on you, when i saw: int x = 0; x < output.rows();

berak gravatar imageberak ( 2018-03-07 12:53:48 -0500 )edit

@berak please tell me correct way how to resolve this

Udaib khan gravatar imageUdaib khan ( 2018-03-08 07:55:47 -0500 )edit