加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 00:36:44 所属栏目:Java 来源:网络整理
导读:我没有在openCV中找到任何在 java中将平面图像转换为圆柱形的示例,我希望它在2d而不是3d中渲染图像,也没有找到任何示例代码或书籍.下面是我想在杯子周围扭曲的图片的图像. 一本好书和示例代码将非常感激. 到目前为止我已经这样做了.建议我的@Amitay使图像凹
我没有在openCV中找到任何在 java中将平面图像转换为圆柱形的示例,我希望它在2d而不是3d中渲染图像,也没有找到任何示例代码或书籍.下面是我想在杯子周围扭曲的图片的图像.

一本好书和示例代码将非常感激.

到目前为止我已经这样做了.建议我的@Amitay使图像凹陷,使用这个例子Wrap image around cylinder ,但坚持转换.

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

}

解决方法

请看 Warp Image to Appear in Cylindrical Projection中的这个答案

你只需要改变两件事:

>因为你想要凸投影而不是凹面,你需要改变函数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));

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

>将所有其余代码形式c转换为java.

祝好运

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读