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;
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();
}
}
@berak can you please correct me here where i'm doing mistake
@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
@berak here the error basically occur at this point:
i have c++ version of this code :
Is above java equivalent for this c++ code is right or not
i've given up on you, when i saw:
int x = 0; x < output.rows();
@berak please tell me correct way how to resolve this