简体   繁体   English

如何使用 OpenCV 从屏幕截图中裁剪图像?

[英]How to crop an image from screenshot with the use of OpenCV?

I recently began studying image processing and took a task where I need to crop an image from mobile Instagram screenshot via use of OpenCV.我最近开始研究图像处理并完成了一项任务,我需要使用 OpenCV 从移动 Instagram 屏幕截图中裁剪图像。 I need to find edges of the image with contours and crop, but I'm not sure how to do this correctly.我需要找到具有轮廓和裁剪的图像边缘,但我不确定如何正确执行此操作。

I've tried to look up some examples like these:我试图查找一些这样的例子:

How to crop biggest rectangle out of an image 如何从图像中裁剪出最大的矩形

https://www.quora.com/How-can-I-detect-an-object-from-static-image-and-crop-it-from-the-image-using-openCV https://www.quora.com/How-can-I-detect-an-object-from-static-image-and-crop-it-from-the-image-using-openCV

How to detect edge and crop an image in Python 如何在 Python 中检测边缘并裁剪图像

How to crop rectangular shapes in an image using Python 如何使用 Python 在图像中裁剪矩形形状

But I'm still don't understand how to do it in my case.但我仍然不明白如何在我的情况下做到这一点。

Basically I have images like these:基本上我有这样的图像:

https://imgur.com/a/VbwCdkO and https://imgur.com/a/Mm69i35 https://imgur.com/a/VbwCdkOhttps://imgur.com/a/Mm69i35

And the result should be like this:结果应该是这样的:

https://imgur.com/a/Bq6Zjw0 https://imgur.com/a/Bq6Zjw0

https://imgur.com/a/AhzOkWS https://imgur.com/a/AhzOkWS

Screenshots used need to be only from mobile version of Instagram and it can be assumed that they are always of rectangular shape使用的截图只需要来自 Instagram 的移动版本,并且可以假设它们总是矩形的

And if there are more than one image like here:如果有不止一张像这里的图像:

https://imgur.com/a/avv8Wvv https://imgur.com/a/avv8Wvv

Then only one of the two is cropped (which one doesn't matter).然后只裁剪两者中的一个(哪个无关紧要)。 For example:例如:

https://imgur.com/a/a4KnRKC https://imgur.com/a/a4KnRKC

Thanks!谢谢!

One of the prominent feature in your snapshot images is the white background color.快照图像中的突出特征之一是白色背景色。 Everything appears on top of it, even that user image.一切都出现在它上面,甚至是那个用户图像。 So we will try to segment out the background which would leave us with smaller components such as Instagram icon, likes, etc. Then we will pick the largest element assuming that the user image is the largest element present on the screen.因此,我们将尝试分割出背景,这会给我们留下较小的组件,例如 Instagram 图标、喜欢等。然后我们将选择最大的元素,假设用户图像是屏幕上存在的最大元素。 Then we will simply find the cv2.boundingRect() of the largest contour and crop the snapshot accordingly as:然后我们将简单地找到最大轮廓的cv2.boundingRect()并相应地裁剪快照:

import cv2
import numpy as np

img = cv2.imread("/path/to/img.jpg")

white_lower = np.asarray([230, 230, 230])
white_upper = np.asarray([255, 255, 255])

mask = cv2.inRange(img, white_lower, white_upper)
mask = cv2.bitwise_not(mask)

在此处输入图片说明

Now we fill find contours in this mask and select the largest one.现在我们在这个掩码中填充查找轮廓并选择最大的一个。

im, cnt, hierarchy = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
largest_contour = max(cnt, key=lambda x:cv2.contourArea(x))
bounding_rect = cv2.boundingRect(largest_contour)

cropped_image = img[bounding_rect[1]: bounding_rect[1]+bounding_rect[3],
                bounding_rect[0]:bounding_rect[0]+bounding_rect[2]]

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM