乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      Opencv 圖像邊緣拉伸

       mediatv 2020-03-12

      需求如下圖,需要將綠色點圍成的區(qū)域(記做inside)到紅色點圍成的區(qū)域(記做outside)拉伸到藍色點(圖片中有兩圈藍點,本文以內(nèi)圈藍點為準)圍成的區(qū)域(記做affine),并且綠色區(qū)域內(nèi)部的圖像保持不變,僅拉伸邊緣區(qū)域,原圖中紅色區(qū)域以外的圖像信息忽略。

      原理:將紅點與綠點間的區(qū)域切分為多個三角形,記住srcTriangles, 將藍點與綠點間的區(qū)域也切分為多個三角形,記住dstTriangles。使用仿射變換,將srcTriangles變換為dstTriangles即可實現(xiàn)邊緣拉伸的效果。

      注意三角形切分時,srcTriangles與dstTriangles一一對應(yīng)

      涉及Opencv API:getAffineTransform(仿射變換)

      仿射變換的用法:(三點確定仿射矩陣)

      1. src_tri[0] = Point2f(srcTriangles[i].p1.x, srcTriangles[i].p1.y);
      2. src_tri[1] = Point2f(srcTriangles[i].p2.x, srcTriangles[i].p2.y);
      3. src_tri[2] = Point2f(srcTriangles[i].p3.x, srcTriangles[i].p3.y);
      4. dst_tri[0] = Point2f(dstTriangles[i].p1.x, dstTriangles[i].p1.y);
      5. dst_tri[1] = Point2f(dstTriangles[i].p2.x, dstTriangles[i].p2.y);
      6. dst_tri[2] = Point2f(dstTriangles[i].p3.x, dstTriangles[i].p3.y);
      7. Mat warp_mat_org_inv = cv::getAffineTransform(dst_tri, src_tri);

      實現(xiàn)需求的核心代碼:

      1. Mat deform(std::vector<Triangle> srcTriangles, std::vector<Triangle> dstTriangles, Mat srcImage) {
      2. int nIsoSize = 512;
      3. Mat result = Mat::zeros(nIsoSize, nIsoSize, CV_8UC3);
      4. for (int i = 0; i < srcTriangles.size(); i++)
      5. {
      6. cv::Point2f src_tri[3];
      7. cv::Point2f dst_tri[3];
      8. src_tri[0] = Point2f(srcTriangles[i].p1.x, srcTriangles[i].p1.y);
      9. src_tri[1] = Point2f(srcTriangles[i].p2.x, srcTriangles[i].p2.y);
      10. src_tri[2] = Point2f(srcTriangles[i].p3.x, srcTriangles[i].p3.y);
      11. dst_tri[0] = Point2f(dstTriangles[i].p1.x, dstTriangles[i].p1.y);
      12. dst_tri[1] = Point2f(dstTriangles[i].p2.x, dstTriangles[i].p2.y);
      13. dst_tri[2] = Point2f(dstTriangles[i].p3.x, dstTriangles[i].p3.y);
      14. Mat warp_mat_org_inv = cv::getAffineTransform(dst_tri, src_tri);
      15. warp_mat_org_inv.convertTo(warp_mat_org_inv, CV_32FC1);
      16. for (int x = min(dst_tri[0].x, min(dst_tri[1].x, dst_tri[2].x)); x < max(dst_tri[0].x, max(dst_tri[1].x, dst_tri[2].x)); ++x) {
      17. for (int y = min(dst_tri[0].y, min(dst_tri[1].y, dst_tri[2].y)); y < max(dst_tri[0].y, max(dst_tri[1].y, dst_tri[2].y)); ++y) {
      18. if (is_point_in_triangle(cv::Point2f(x, y), dst_tri[0], dst_tri[1], dst_tri[2])) {
      19. // calculate corresponding position of dst_coord pixel center in image (src)
      20. const Mat homogenous_dst_coord(Vec3f(x, y, 1.0f));
      21. const Vec2f src_texel = Mat(warp_mat_org_inv * homogenous_dst_coord);
      22. if ((round(src_texel[1]) < srcImage.rows) && (round(src_texel[0]) < srcImage.cols) && round(src_texel[0]) > 0 && round(src_texel[1]) > 0)
      23. {
      24. int a1 = round(src_texel[1]);
      25. int a2 = round(src_texel[0]);
      26. result.at<cv::Vec3b>(y, x)[0] = srcImage.at<cv::Vec3b>(round(src_texel[1]), round(src_texel[0]))[0];
      27. result.at<cv::Vec3b>(y, x)[1] = srcImage.at<cv::Vec3b>(round(src_texel[1]), round(src_texel[0]))[1];
      28. result.at<cv::Vec3b>(y, x)[2] = srcImage.at<cv::Vec3b>(round(src_texel[1]), round(src_texel[0]))[2];
      29. }
      30. }
      31. }
      32. }
      33. }
      34. return result;
      35. }

      最終效果:(第一張為原圖,第二張為邊緣拉伸,中間不拉伸的效果)



        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多