如何根据Python OpenCV中的坐标来裁剪图像

发布于 2025-01-20 19:49:39 字数 374 浏览 5 评论 0原文

我正在使用以下行绘制CV2矩形

cv2.rectangle(rightImg, (x, y), (x + w, y + h), (0, 0, 255), 2)

,现在

x = 93
y = 62
w = 6
h = 3

我想裁剪矩形的那一部分。在下面的代码线上确实有意义:

cropImg = rightImg[y:x, y+h:x+w]

或者

cropImg = rightImg[y+h:x+w, y:x]

我尝试了以上两者,但这并没有裁剪确切的区域。接下来我可以尝试什么?

I am drawing a cv2 rectangle by using below line

cv2.rectangle(rightImg, (x, y), (x + w, y + h), (0, 0, 255), 2)

Now values are

x = 93
y = 62
w = 6
h = 3

Now I want to crop that part of the rectangle. Does below line of code make sense:

cropImg = rightImg[y:x, y+h:x+w]

or

cropImg = rightImg[y+h:x+w, y:x]

I have tried both of the above but it's not cropping the exact area. What can I try next?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泛滥成性 2025-01-27 19:49:39

你应该尝试

cropImg=rightImg[y:y+h,x:x+w]. 

you should try

cropImg=rightImg[y:y+h,x:x+w]. 
江南月 2025-01-27 19:49:39

这已经在这里得到回答:
如何使用 Python 在 OpenCV 中裁剪图像

crop_img = rightImg[y:y+h, x:x+w]
cv2.imshow("cropped", crop_img)
cv2.waitKey(0)

小心标记为重复项。

This is already answered here:
How to crop an image in OpenCV using Python

crop_img = rightImg[y:y+h, x:x+w]
cv2.imshow("cropped", crop_img)
cv2.waitKey(0)

Be careful of marked as duplicates.

岁月打碎记忆 2025-01-27 19:49:39

它可能看起来有点奇怪,但你必须先写出 y 坐标。

cropImg = rightImg[y:y+h, x:x+w]

它将在矩形所在的位置裁剪图像。

It may look a bit weird but you have to write the y coordinates first.

cropImg = rightImg[y:y+h, x:x+w]

It will crop the image at where the rectangle is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文