Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Dispalcement map filter using opencv java

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.

Here is my code snippet:

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

{ if (componentX < 0 || componentX > 2 || componentY < 0 || componentY > 2) { System.out.println("!!! displacementMapFilter: componentX and componentY values must be in range [0,2]"); return; } if (target.size().width != map.size().width || target.size().height != map.size().height || target.type() != CvType.CV_8UC4) { System.out.println("!!! displacementMapFilter: map and target need to have the same dimensions, and target type must be CV_8UC4"); //return; } 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);
            target.put(dx, dy, mapByteBuffer);
            output.put(x, y, target.get(dx, dy));

        }

    }
}

Dispalcement map filter using opencv java

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.

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) { if (componentX < 0 || componentX > 2 || componentY < 0 || componentY > 2) { System.out.println("!!! displacementMapFilter: componentX and componentY values must be in range [0,2]"); return; } if (target.size().width != map.size().width || target.size().height != map.size().height || target.type() != CvType.CV_8UC4) { System.out.println("!!! displacementMapFilter: map and target need to have the same dimensions, and target type must be CV_8UC4"); //return; } 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;

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);
             target.put(dx, dy, mapByteBuffer);
             output.put(x, y, target.get(dx, dy));

         }

     }
 }

Dispalcement map filter using opencv java

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.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) 
{
        if (componentX < 0 || componentX > 2 || componentY < 0 || componentY > 2) {
            System.out.println("!!! displacementMapFilter: componentX and componentY values must be in range [0,2]");
            return;
        }
        if (target.size().width != map.size().width || target.size().height != map.size().height || target.type() != CvType.CV_8UC4) {
            System.out.println("!!! displacementMapFilter: map and target need to have the same dimensions, and target type must be CV_8UC4");
            //return;
        }
        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);
                target.put(dx, dy, mapByteBuffer);
                output.put(x, y, target.get(dx, dy));

            }

        }
    }

Dispalcement map filter using opencv java

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) 
{
        if (componentX < 0 || componentX > 2 || componentY < 0 || componentY > 2) {
            System.out.println("!!! displacementMapFilter: componentX and componentY values must be in range [0,2]");
            return;
        }
        if (target.size().width != map.size().width || target.size().height != map.size().height || target.type() != CvType.CV_8UC4) {
            System.out.println("!!! displacementMapFilter: map and target need to have the same dimensions, and target type must be CV_8UC4");
            //return;
        }
        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);
                target.put(dx, dy, mapByteBuffer);
                output.put(x, y, target.get(dx, dy));

            }

        }
    }

Dispalcement map filter using opencv java

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) 
{
        if (componentX < 0 || componentX > 2 || componentY < 0 || componentY > 2) {
            System.out.println("!!! displacementMapFilter: componentX and componentY values must be in range [0,2]");
            return;
        }
        if (target.size().width != map.size().width || target.size().height != map.size().height || target.type() != CvType.CV_8UC4) {
            System.out.println("!!! displacementMapFilter: map and target need to have the same dimensions, and target type must be CV_8UC4");
            //return;
        }
        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);

            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();
        }
    }

Dispalcement map filter using opencv java

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) 
{
        if (componentX < 0 || componentX > 2 || componentY < 0 || componentY > 2) {
            System.out.println("!!! displacementMapFilter: componentX and componentY values must be in range [0,2]");
            return;
        }
        if (target.size().width != map.size().width || target.size().height != map.size().height || target.type() != CvType.CV_8UC4) {
            System.out.println("!!! displacementMapFilter: map and target need to have the same dimensions, and target type must be CV_8UC4");
            //return;
        }
        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);

            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, output.put(y, x, ((backgroundPx * (1. - opacity)) + (foregroundPx * opacity)));
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Dispalcement map filter using opencv java

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) 
{
        if (componentX < 0 || componentX > 2 || componentY < 0 || componentY > 2) {
            System.out.println("!!! displacementMapFilter: componentX and componentY values must be in range [0,2]");
            return;
        }
        if (target.size().width != map.size().width || target.size().height != map.size().height || target.type() != CvType.CV_8UC4) {
            System.out.println("!!! displacementMapFilter: map and target need to have the same dimensions, and target type must be CV_8UC4");
            //return;
        }
         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

                // 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;


                    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();
        }
    }

Dispalcement map filter using opencv java

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; // because of the translation.

                    // we are done with this row if the column is outside of the foreground image.
                     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();
         }
     }