在Java中将图像转换为圆柱形

8
我在Java中没有找到任何关于将平面图像转换为圆柱形的openCV示例,我希望它能以2D方式呈现图像而不是3D,并且也没有找到任何相关的示例代码或书籍。下面是我想要围绕杯子变形的图片。

如果有好的书籍和示例代码,将不胜感激。

enter image description here

enter image description here

enter image description here

我目前已经完成了这部分工作。建议我的 @Amitay 将图像变为凹面,使用此示例 将图像围绕圆柱体,但卡在了转换上。
import java.io.File;
import org.bytedeco.javacpp.indexer.UByteBufferIndexer;
import org.bytedeco.javacpp.opencv_core.Mat;
import org.bytedeco.javacpp.opencv_core.*;
import static org.bytedeco.javacpp.opencv_highgui.imshow;
import static org.bytedeco.javacpp.opencv_highgui.waitKey;
import static org.bytedeco.javacpp.opencv_imgcodecs.CV_LOAD_IMAGE_COLOR;
import static org.bytedeco.javacpp.opencv_imgcodecs.imread;

/**
 *
 * @author BTACTC
 */
public class CupWrapping {

    Mat image;
    Mat dstImage;

    int width;
    int height;

    public CupWrapping(File imageFile) {

        image = imread(imageFile.getAbsolutePath(), CV_LOAD_IMAGE_COLOR);

        width = image.size().width();
        height = image.size().height();

        dstImage = new Mat(width, height, image.type());

        UByteBufferIndexer sI = image.createIndexer();
        UByteBufferIndexer sD = dstImage.createIndexer();

        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                Point2f current_pos = new Point2f(x, y);
                current_pos = convert_pt(current_pos, width, height);
                Point top_left = new Point((int) current_pos.x(), (int) current_pos.y()); //top left because of integer rounding

                //make sure the point is actually inside the original image
                if (top_left.x() < 0
                        || top_left.x() > width - 2
                        || top_left.y() < 0
                        || top_left.y() > height - 2) {
                    continue;
                }

                //bilinear interpolation
                float dx = current_pos.x() - top_left.x();
                float dy = current_pos.y() - top_left.y();

                float weight_tl = (float) ((1.0 - dx) * (1.0 - dy));
                float weight_tr = (float) ((dx) * (1.0 - dy));
                float weight_bl = (float) ((1.0 - dx) * (dy));
                float weight_br = (dx) * (dy);

                byte value = (byte) (weight_tl * sI.get(top_left.y(), top_left.x())
                        + weight_tr * sI.get(top_left.y(), top_left.x() + 1)
                        + weight_bl * sI.get(top_left.y() + 1, top_left.x())
                        + weight_br * sI.get(top_left.y() + 1, top_left.x() + 1));
                sD.put(y, x,value);
            }
        }

        imshow("", dstImage);

        waitKey(0);

    }

    public Point2f convert_pt(Point2f point, int w, int h) {
        //center the point at 0,0
        Point2f pc = new Point2f(point.x() - w / 2, point.y() - h / 2);

        //these are your free parameters
        float f = w;
        float r = w;

        float omega = w / 2;
        float z0 = (float) (f - Math.sqrt(r * r - omega * omega));

        float zc = (float) ((2 * z0 - Math.sqrt(4 * z0 * z0 - 4 * (pc.x() * pc.x() / (f * f) + 1) * (z0 * z0 - r * r))) / (2 * (pc.x() * pc.x() / (f * f) + 1)));
        Point2f final_point = new Point2f(pc.x() * zc / f, pc.y() * zc / f);
        final_point.x() = final_point.x() +  w / 2;
       final_point.y() += h / 2;
        return final_point;

    }

    public static void main(String[] args) {

        File imageFile = new File("image/C13.jpg");
        CupWrapping wrap = new CupWrapping(imageFile);
    }

}

我不知道为什么会被踩,如果你要踩这个问题,你需要给出具体的原因。 - Aqeel Haider
我不知道(我不是给你点踩的人),但我猜测可能是因为有人认为你需要提供更多的信息。请查看tour 了解大家可能寻找的详细信息。 在这种情况下,我不确定您是否想将杯子呈现为使用桥梁图像纹理的三维模型,还是您想要呈现一个只包含该特定杯子和图像的静态图像,或者图像应该围绕杯子旋转多少,您使用了哪些其他库等等。请编辑以添加详细信息! :) - cxw
@cxw 感谢您的评论,我已经编辑了问题,我认为现在更加清晰明了。 - Aqeel Haider
你是想将图像渲染到一个杯子的3D模型上吗?问题仍然不太清楚。你能否包含一下你已经编写的实现这个功能的代码? - masad
我添加了代码。 - Aqeel Haider
1个回答

7

看看这个答案在将图像变形为圆柱投影

你只需要更改两个地方:

  1. 因为你想要凸投影而不是凹投影,所以你需要更改函数convert_pt中的代码行。

从:

float zc = (2*z0+sqrt(4*z0*z0-4*(pc.x*pc.x/(f*f)+1)*(z0*z0-r*r)))/(2* (pc.x*pc.x/(f*f)+1));

To

float zc = (2*z0-sqrt(4*z0*z0-4*(pc.x*pc.x/(f*f)+1)*(z0*z0-r*r)))/(2* (pc.x*pc.x/(f*f)+1));
  1. 将剩下的代码从c++转换为java。

祝你好运!


@ Amitay 谢谢,我现在正在将C++代码转换为Java,卡在了image.at<uchar>(top_left)这一部分,我不知道如何在Java中转换这部分代码。任何建议都会有帮助。 - Aqeel Haider
看起来你所说的部分只是在特定位置从原始图像中读取值并根据预先计算的权重进行加权。因此,image.at<uchar>(top_left) 只是 image.at<uchar>(top_left.y, top_left.x)。 - Binus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接