• 不规则四边形贴图(转)


    用OpenCv可以搞定 两个函数: 
    cvGetPerspectiveTransform  
    cvWarpPerspective 

    不多说了 给你段代码: 

    C/C++ code CvPoint2D32f srcpoints[4]; CvPoint2D32f dstpoints[4]; srcpoints[0].x=o0x; //原图像当中四个点的位置 srcpoints[0].y=o0y; srcpoints[1].x=o1x; srcpoints[1].y=o1y; srcpoints[2].x=o2x; srcpoints[2].y=o2y; srcpoints[3].x=o3x; srcpoints[3].y=o3y; dstpoints[0].x=xofst0; //变换后对应的四个位置 dstpoints[0].y=yofst0; dstpoints[1].x=xofst1; dstpoints[1].y=yofst1; dstpoints[3].x=xofst2; dstpoints[3].y=yofst2; dstpoints[2].x=xofst3; dstpoints[2].y=yofst3; cvGetPerspectiveTransform(srcpoints,dstpoints,&mat ); //取得透视变换矩阵 cvWarpPerspective(src,dst,&mat,CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS,cvScalarAll(0)); //进行透视变换

    网站转载的

    staticint ComputePixel(float x, float y, outfloat x1, outfloat y1)
    {
    double r, nn;

    if (x ==0&& y ==0)
    {
    x1
    = x;
    y1
    = y;
    return1;
    }

    nn
    = Math.Sqrt(x * x + y * y);
    r
    = (Math.Abs(x) > Math.Abs(y)) ? Math.Abs(nn / x) : Math.Abs(nn / y);

    x1
    = (float)(r * x);
    y1
    = (float)(r * y);

    return1;
    }

    static Color GetPixelColorInterpolated(ref Bitmap image, float x, float y)
    {
    int xi = (int)(x);
    if (x <0) xi--;
    int yi = (int)(y);
    if (y <0) yi--;

    if (xi <-1|| xi >= image.Width || yi <-1|| yi >= image.Height)
    {
    return GetPixelColorWithOverflow(ref image, -999, -999);
    }

    //get four neighbouring pixels
    if ((xi +1) < image.Width && xi >=0&& (yi +1) < image.Height && yi >=0)
    {
    ushort wt1 = (ushort)((x - xi) *256.0f), wt2 = (ushort)((y - yi) *256.0f);
    ushort wd = (ushort)(wt1 * wt2 >>8);
    ushort wb = (ushort)(wt1 - wd);
    ushort wc = (ushort)(wt2 - wd);
    ushort wa = (ushort)(256- wt1 - wc);
    ushort wrr, wgg, wbb;

    Color clr
    = image.GetPixel(xi, yi);
    wbb
    = (ushort)(wa * clr.B); wgg = (ushort)(wa * clr.G); wrr = (ushort)(wa * clr.R);

    clr
    = image.GetPixel(xi +1, yi);
    wbb
    += (ushort)(wb * clr.B); wgg += (ushort)(wb * clr.G); wrr += (ushort)(wb * clr.R);

    clr
    = image.GetPixel(xi, yi +1);
    wbb
    += (ushort)(wc * clr.B); wgg += (ushort)(wc * clr.G); wrr += (ushort)(wc * clr.R);

    clr
    = image.GetPixel(xi +1, yi +1);
    wbb
    += (ushort)(wd * clr.B); wgg += (ushort)(wd * clr.G); wrr += (ushort)(wd * clr.R);

    return Color.FromArgb(255, wrr >>8, wgg >>8, wbb >>8);
    }
    else
    {
    float t1 = x - xi, t2 = y - yi;
    float d = t1 * t2;
    float b = t1 - d;
    float c = t2 - d;
    float a =1- t1 - c;

    Color rgb11, rgb21, rgb12, rgb22;
    rgb11
    = GetPixelColorWithOverflow(ref image, xi, yi);
    rgb21
    = GetPixelColorWithOverflow(ref image, xi +1, yi);
    rgb12
    = GetPixelColorWithOverflow(ref image, xi, yi +1);
    rgb22
    = GetPixelColorWithOverflow(ref image, xi +1, yi +1);

    //calculate linear interpolation
    return Color.FromArgb(255,
    (
    byte)(a * rgb11.R + b * rgb21.R + c * rgb12.R + d * rgb22.R),
    (
    byte)(a * rgb11.G + b * rgb21.G + c * rgb12.G + d * rgb22.G),
    (
    byte)(a * rgb11.B + b * rgb21.B + c * rgb12.B + d * rgb22.B));
    }
    }

    static Color GetPixelColorWithOverflow(ref Bitmap image, long x, long y)
    {
    if (!IsInside(ref image, x, y))
    {
    return Color.FromArgb(255, 255, 255, 255);
    }

    return image.GetPixel((int)x, (int)y);
    }

    staticbool IsInside(ref Bitmap image, long x, long y)
    {
    return (0<= y && y < image.Height &&0<= x && x < image.Width);
    }

    privatevoid transformEllipseToolStripMenuItem_Click(object sender, EventArgs e)
    {
    Bitmap image
    =new Bitmap("D:\\PSD\\info1.jpg");

    int x, y;
    float x1, y1;
    float fx, fy, xmid, ymid, ar;
    Bitmap image2
    =new Bitmap(image);

    xmid
    = (float)(image.Width /2.0);
    ymid
    = (float)(image.Height /2.0);
    ar
    = (float)(image.Height) / (float)(image.Width);
    for (y =0; y < image.Height; y++)
    {
    for (x =0; x < image.Width; x++)
    {
    ComputePixel(ar
    * (x - xmid), y - ymid, out fx, out fy);
    x1
    = xmid + fx / ar;
    y1
    = ymid + fy;

    image2.SetPixel(x, y, GetPixelColorInterpolated(
    ref image, x1, y1));
    }
    }

    this.pictureBox1.Image = image2;
    }



    这个文章不错

    http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/imgproc/imgtrans/warp_affine/warp_affine.html#warp-affine

    http://www.opencv.org.cn/forum/viewtopic.php?f=1&t=8940&p=34421&hilit=%E9%80%8F+%E8%A7%86+%E5%8F%98+%E6%8D%A2#p34421

  • 相关阅读:
    转 | 禁忌搜索算法(Tabu Search)求解带时间窗的车辆路径规划问题详解(附Java代码)
    Branch and price and cut求解传统VRP问题以及VRPTW问题
    标号法(label-setting algorithm)求解带时间窗的最短路问题(ESPPRC)
    运筹学从何学起?如何快速入门精确式算法?
    转 | 模拟退火算法(SA)和迭代局部搜索(ILS)求解TSP的Java代码分享
    用Python画论文折线图、曲线图?几个代码模板轻松搞定!
    45. 截取“测试数据”后面的内容
    44. 更改oracle字符集编码american_america.zh16gbk 改为 SIMPLIFIED CHINESE_CHINA.ZHS16GBK
    18. 浏览器关闭页面时弹出“确定要离开此面吗?”
    6. concat_ws用法
  • 原文地址:https://www.cnblogs.com/zhixing/p/2422506.html
Copyright © 2020-2023  润新知