Cut out a piece from image the piece is not rectangular (eg trapeze), and turn it into a rectangle that fits...












0















I will bring an example I have a picture of a swimming pool with some tracks I want to take only the three middle tracks Now what is the best way to cut the image in a trapeze shape then how to take this trapeze and try to fit it to the size of the window that will have a relatively similar ratio between the two sides (upper and lower)



image for the example










share|improve this question



























    0















    I will bring an example I have a picture of a swimming pool with some tracks I want to take only the three middle tracks Now what is the best way to cut the image in a trapeze shape then how to take this trapeze and try to fit it to the size of the window that will have a relatively similar ratio between the two sides (upper and lower)



    image for the example










    share|improve this question

























      0












      0








      0








      I will bring an example I have a picture of a swimming pool with some tracks I want to take only the three middle tracks Now what is the best way to cut the image in a trapeze shape then how to take this trapeze and try to fit it to the size of the window that will have a relatively similar ratio between the two sides (upper and lower)



      image for the example










      share|improve this question














      I will bring an example I have a picture of a swimming pool with some tracks I want to take only the three middle tracks Now what is the best way to cut the image in a trapeze shape then how to take this trapeze and try to fit it to the size of the window that will have a relatively similar ratio between the two sides (upper and lower)



      image for the example







      python opencv image-processing imutils






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 2 at 13:51









      yoelyoel

      11




      11
























          2 Answers
          2






          active

          oldest

          votes


















          0














          I modified this example



          Result:



          enter image description here



          import numpy as np 
          import cv2

          # load image
          img = cv2.imread('pool.jpg')
          # resize to easily fit on screen
          img = cv2.resize(img,None,fx=0.5, fy=0.5, interpolation = cv2.INTER_CUBIC)
          # determine cornerpoints of the region of interest
          pts1 = np.float32([[400,30],[620,30],[50,700],[1000,700]])
          # provide new coordinates of cornerpoints
          pts2 = np.float32([[0,0],[300,0],[0,600],[300,600]])

          # determine transformationmatrix
          M = cv2.getPerspectiveTransform(pts1,pts2)
          # apply transformationmatrix
          dst = cv2.warpPerspective(img,M,(300,600))

          # display image
          cv2.imshow("img", dst)
          cv2.waitKey(0)
          cv2.destroyAllWindows()


          Note the rezise function, you may wish to delete that line, but you will have to change the coordinates of the cornerpoints accordingly.
          I used about the height and width of the base of the trapezoid for the new image (300,600).



          You can tweak the cornerpoints and final image size as you see fit.






          share|improve this answer
























          • Thank you very much for the answer but the quality of the image is very low, do you have any other idea ?

            – yoel
            Jan 3 at 8:13











          • The problem is the image itself. If you zoom in on the top of the image, it is already quite blurred. Those pixels are at the top of the trapezoid and get stretched to make the new image a rectangle. The way to improve the final result is to take a sharp picture with more pixels.

            – J.D.
            Jan 3 at 10:09



















          0














          You can use imutils.four_point_transform function. You can read more about it here.



          Basic usage is finding the document contours on a canny edge detected image (again, you can use imutils package that I linked), find the contours on that image and then apply four_point_transform on that contour.



          EDIT: How to use canny edge detection and four_point_transform



          For finding contours you can use openCV and imutils like this:



          cnts = cv2.findContours(edged_image.copy(), cv2.RETR_EXTERNAL,
          cv2.CHAIN_APPROX_SIMPLE)
          cnts = imutils.grab_contours(cnts)


          Now, when you have the contours just iterate through and see which one is the biggest and has four points (4 vertices). Then just pass the image and the contour to the four_point_transform function like this:



          image_2 = four_point_transform(image, biggest_contour)


          That's it.






          share|improve this answer


























          • I would be happy if you could explain a little more in detail how to find the contours by canny detection , because if i use only the four_point_transform without the contours the quality of the image is very low

            – yoel
            Jan 3 at 8:12











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54007548%2fcut-out-a-piece-from-image-the-piece-is-not-rectangular-eg-trapeze-and-turn-i%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          I modified this example



          Result:



          enter image description here



          import numpy as np 
          import cv2

          # load image
          img = cv2.imread('pool.jpg')
          # resize to easily fit on screen
          img = cv2.resize(img,None,fx=0.5, fy=0.5, interpolation = cv2.INTER_CUBIC)
          # determine cornerpoints of the region of interest
          pts1 = np.float32([[400,30],[620,30],[50,700],[1000,700]])
          # provide new coordinates of cornerpoints
          pts2 = np.float32([[0,0],[300,0],[0,600],[300,600]])

          # determine transformationmatrix
          M = cv2.getPerspectiveTransform(pts1,pts2)
          # apply transformationmatrix
          dst = cv2.warpPerspective(img,M,(300,600))

          # display image
          cv2.imshow("img", dst)
          cv2.waitKey(0)
          cv2.destroyAllWindows()


          Note the rezise function, you may wish to delete that line, but you will have to change the coordinates of the cornerpoints accordingly.
          I used about the height and width of the base of the trapezoid for the new image (300,600).



          You can tweak the cornerpoints and final image size as you see fit.






          share|improve this answer
























          • Thank you very much for the answer but the quality of the image is very low, do you have any other idea ?

            – yoel
            Jan 3 at 8:13











          • The problem is the image itself. If you zoom in on the top of the image, it is already quite blurred. Those pixels are at the top of the trapezoid and get stretched to make the new image a rectangle. The way to improve the final result is to take a sharp picture with more pixels.

            – J.D.
            Jan 3 at 10:09
















          0














          I modified this example



          Result:



          enter image description here



          import numpy as np 
          import cv2

          # load image
          img = cv2.imread('pool.jpg')
          # resize to easily fit on screen
          img = cv2.resize(img,None,fx=0.5, fy=0.5, interpolation = cv2.INTER_CUBIC)
          # determine cornerpoints of the region of interest
          pts1 = np.float32([[400,30],[620,30],[50,700],[1000,700]])
          # provide new coordinates of cornerpoints
          pts2 = np.float32([[0,0],[300,0],[0,600],[300,600]])

          # determine transformationmatrix
          M = cv2.getPerspectiveTransform(pts1,pts2)
          # apply transformationmatrix
          dst = cv2.warpPerspective(img,M,(300,600))

          # display image
          cv2.imshow("img", dst)
          cv2.waitKey(0)
          cv2.destroyAllWindows()


          Note the rezise function, you may wish to delete that line, but you will have to change the coordinates of the cornerpoints accordingly.
          I used about the height and width of the base of the trapezoid for the new image (300,600).



          You can tweak the cornerpoints and final image size as you see fit.






          share|improve this answer
























          • Thank you very much for the answer but the quality of the image is very low, do you have any other idea ?

            – yoel
            Jan 3 at 8:13











          • The problem is the image itself. If you zoom in on the top of the image, it is already quite blurred. Those pixels are at the top of the trapezoid and get stretched to make the new image a rectangle. The way to improve the final result is to take a sharp picture with more pixels.

            – J.D.
            Jan 3 at 10:09














          0












          0








          0







          I modified this example



          Result:



          enter image description here



          import numpy as np 
          import cv2

          # load image
          img = cv2.imread('pool.jpg')
          # resize to easily fit on screen
          img = cv2.resize(img,None,fx=0.5, fy=0.5, interpolation = cv2.INTER_CUBIC)
          # determine cornerpoints of the region of interest
          pts1 = np.float32([[400,30],[620,30],[50,700],[1000,700]])
          # provide new coordinates of cornerpoints
          pts2 = np.float32([[0,0],[300,0],[0,600],[300,600]])

          # determine transformationmatrix
          M = cv2.getPerspectiveTransform(pts1,pts2)
          # apply transformationmatrix
          dst = cv2.warpPerspective(img,M,(300,600))

          # display image
          cv2.imshow("img", dst)
          cv2.waitKey(0)
          cv2.destroyAllWindows()


          Note the rezise function, you may wish to delete that line, but you will have to change the coordinates of the cornerpoints accordingly.
          I used about the height and width of the base of the trapezoid for the new image (300,600).



          You can tweak the cornerpoints and final image size as you see fit.






          share|improve this answer













          I modified this example



          Result:



          enter image description here



          import numpy as np 
          import cv2

          # load image
          img = cv2.imread('pool.jpg')
          # resize to easily fit on screen
          img = cv2.resize(img,None,fx=0.5, fy=0.5, interpolation = cv2.INTER_CUBIC)
          # determine cornerpoints of the region of interest
          pts1 = np.float32([[400,30],[620,30],[50,700],[1000,700]])
          # provide new coordinates of cornerpoints
          pts2 = np.float32([[0,0],[300,0],[0,600],[300,600]])

          # determine transformationmatrix
          M = cv2.getPerspectiveTransform(pts1,pts2)
          # apply transformationmatrix
          dst = cv2.warpPerspective(img,M,(300,600))

          # display image
          cv2.imshow("img", dst)
          cv2.waitKey(0)
          cv2.destroyAllWindows()


          Note the rezise function, you may wish to delete that line, but you will have to change the coordinates of the cornerpoints accordingly.
          I used about the height and width of the base of the trapezoid for the new image (300,600).



          You can tweak the cornerpoints and final image size as you see fit.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 2 at 14:27









          J.D.J.D.

          1,282229




          1,282229













          • Thank you very much for the answer but the quality of the image is very low, do you have any other idea ?

            – yoel
            Jan 3 at 8:13











          • The problem is the image itself. If you zoom in on the top of the image, it is already quite blurred. Those pixels are at the top of the trapezoid and get stretched to make the new image a rectangle. The way to improve the final result is to take a sharp picture with more pixels.

            – J.D.
            Jan 3 at 10:09



















          • Thank you very much for the answer but the quality of the image is very low, do you have any other idea ?

            – yoel
            Jan 3 at 8:13











          • The problem is the image itself. If you zoom in on the top of the image, it is already quite blurred. Those pixels are at the top of the trapezoid and get stretched to make the new image a rectangle. The way to improve the final result is to take a sharp picture with more pixels.

            – J.D.
            Jan 3 at 10:09

















          Thank you very much for the answer but the quality of the image is very low, do you have any other idea ?

          – yoel
          Jan 3 at 8:13





          Thank you very much for the answer but the quality of the image is very low, do you have any other idea ?

          – yoel
          Jan 3 at 8:13













          The problem is the image itself. If you zoom in on the top of the image, it is already quite blurred. Those pixels are at the top of the trapezoid and get stretched to make the new image a rectangle. The way to improve the final result is to take a sharp picture with more pixels.

          – J.D.
          Jan 3 at 10:09





          The problem is the image itself. If you zoom in on the top of the image, it is already quite blurred. Those pixels are at the top of the trapezoid and get stretched to make the new image a rectangle. The way to improve the final result is to take a sharp picture with more pixels.

          – J.D.
          Jan 3 at 10:09













          0














          You can use imutils.four_point_transform function. You can read more about it here.



          Basic usage is finding the document contours on a canny edge detected image (again, you can use imutils package that I linked), find the contours on that image and then apply four_point_transform on that contour.



          EDIT: How to use canny edge detection and four_point_transform



          For finding contours you can use openCV and imutils like this:



          cnts = cv2.findContours(edged_image.copy(), cv2.RETR_EXTERNAL,
          cv2.CHAIN_APPROX_SIMPLE)
          cnts = imutils.grab_contours(cnts)


          Now, when you have the contours just iterate through and see which one is the biggest and has four points (4 vertices). Then just pass the image and the contour to the four_point_transform function like this:



          image_2 = four_point_transform(image, biggest_contour)


          That's it.






          share|improve this answer


























          • I would be happy if you could explain a little more in detail how to find the contours by canny detection , because if i use only the four_point_transform without the contours the quality of the image is very low

            – yoel
            Jan 3 at 8:12
















          0














          You can use imutils.four_point_transform function. You can read more about it here.



          Basic usage is finding the document contours on a canny edge detected image (again, you can use imutils package that I linked), find the contours on that image and then apply four_point_transform on that contour.



          EDIT: How to use canny edge detection and four_point_transform



          For finding contours you can use openCV and imutils like this:



          cnts = cv2.findContours(edged_image.copy(), cv2.RETR_EXTERNAL,
          cv2.CHAIN_APPROX_SIMPLE)
          cnts = imutils.grab_contours(cnts)


          Now, when you have the contours just iterate through and see which one is the biggest and has four points (4 vertices). Then just pass the image and the contour to the four_point_transform function like this:



          image_2 = four_point_transform(image, biggest_contour)


          That's it.






          share|improve this answer


























          • I would be happy if you could explain a little more in detail how to find the contours by canny detection , because if i use only the four_point_transform without the contours the quality of the image is very low

            – yoel
            Jan 3 at 8:12














          0












          0








          0







          You can use imutils.four_point_transform function. You can read more about it here.



          Basic usage is finding the document contours on a canny edge detected image (again, you can use imutils package that I linked), find the contours on that image and then apply four_point_transform on that contour.



          EDIT: How to use canny edge detection and four_point_transform



          For finding contours you can use openCV and imutils like this:



          cnts = cv2.findContours(edged_image.copy(), cv2.RETR_EXTERNAL,
          cv2.CHAIN_APPROX_SIMPLE)
          cnts = imutils.grab_contours(cnts)


          Now, when you have the contours just iterate through and see which one is the biggest and has four points (4 vertices). Then just pass the image and the contour to the four_point_transform function like this:



          image_2 = four_point_transform(image, biggest_contour)


          That's it.






          share|improve this answer















          You can use imutils.four_point_transform function. You can read more about it here.



          Basic usage is finding the document contours on a canny edge detected image (again, you can use imutils package that I linked), find the contours on that image and then apply four_point_transform on that contour.



          EDIT: How to use canny edge detection and four_point_transform



          For finding contours you can use openCV and imutils like this:



          cnts = cv2.findContours(edged_image.copy(), cv2.RETR_EXTERNAL,
          cv2.CHAIN_APPROX_SIMPLE)
          cnts = imutils.grab_contours(cnts)


          Now, when you have the contours just iterate through and see which one is the biggest and has four points (4 vertices). Then just pass the image and the contour to the four_point_transform function like this:



          image_2 = four_point_transform(image, biggest_contour)


          That's it.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 3 at 8:37

























          answered Jan 2 at 14:01









          NovakNovak

          1,4941615




          1,4941615













          • I would be happy if you could explain a little more in detail how to find the contours by canny detection , because if i use only the four_point_transform without the contours the quality of the image is very low

            – yoel
            Jan 3 at 8:12



















          • I would be happy if you could explain a little more in detail how to find the contours by canny detection , because if i use only the four_point_transform without the contours the quality of the image is very low

            – yoel
            Jan 3 at 8:12

















          I would be happy if you could explain a little more in detail how to find the contours by canny detection , because if i use only the four_point_transform without the contours the quality of the image is very low

          – yoel
          Jan 3 at 8:12





          I would be happy if you could explain a little more in detail how to find the contours by canny detection , because if i use only the four_point_transform without the contours the quality of the image is very low

          – yoel
          Jan 3 at 8:12


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54007548%2fcut-out-a-piece-from-image-the-piece-is-not-rectangular-eg-trapeze-and-turn-i%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith