Cut out a piece from image the piece is not rectangular (eg trapeze), and turn it into a rectangle that fits...
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
add a comment |
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
add a comment |
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
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
python opencv image-processing imutils
asked Jan 2 at 13:51
yoelyoel
11
11
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
I modified this example
Result:
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.
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
add a comment |
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.
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
I modified this example
Result:
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.
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
add a comment |
I modified this example
Result:
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.
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
add a comment |
I modified this example
Result:
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.
I modified this example
Result:
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.
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
add a comment |
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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