Python:如何从图像(OpenCV,Numpy)中切出具有特定颜色的区域 [英] Python: How to cut out an area with specific color from image (OpenCV, Numpy)
问题描述
所以我一直在尝试编写Python脚本,该脚本以图像作为输入,然后切出具有特定背景颜色的矩形.但是,导致我的编码技能出现问题的是,矩形并不是在每个图像中都位于固定位置上(该位置是随机的).
so I've been trying to code a Python script, which takes an image as input and then cuts out a rectangle with a specific background color. However, what causes a problem for my coding skills, is that the rectangle is not on a fixed position in every image (the position will be random).
我不太了解如何管理numpy函数.我还阅读了一些有关OpenCV的内容,但是我是一个新手.到目前为止,我只是通过".crop"功能裁剪了图像,但是随后我将不得不使用固定值.
I do not really understand how to manage the numpy functions. I also read something about OpenCV, but I'm totally new to it. So far I just cropped the images through the ".crop" function, but then I would have to use fixed values.
这是输入图像的外观,现在我想检测黄色矩形的位置,然后将图像裁剪到其大小.
This is how the input image could look and now I would like to detect the position of the yellow rectangle and then crop the image to its size.
非常感谢您的帮助.
@MarkSetchell的方法效果很好,但是发现其他测试图片存在问题.另一张图片的问题是,在图片的顶部和底部有2个小像素具有相同的颜色,这会导致错误或裁切不良.
@MarkSetchell's way works pretty good, but found a issue for a different test picture. The problem with the other picture is that there are 2 small pixels with the same color at the top and the bottom of the picture, which cause errors or a bad crop.
推荐答案
更新后的答案
我已经更新了答案,以应对与黄色框颜色相同的嘈杂离群像素的斑点.首先在图片上运行3x3的中值滤镜以去除斑点:
I have updated my answer to cope with specks of noisy outlier pixels of the same colour as the yellow box. This works by running a 3x3 median filter over the image first to remove the spots:
#!/usr/bin/env python3
import numpy as np
from PIL import Image, ImageFilter
# Open image and make into Numpy array
im = Image.open('image.png').convert('RGB')
na = np.array(im)
orig = na.copy() # Save original
# Median filter to remove outliers
im = im.filter(ImageFilter.MedianFilter(3))
# Find X,Y coordinates of all yellow pixels
yellowY, yellowX = np.where(np.all(na==[247,213,83],axis=2))
top, bottom = yellowY[0], yellowY[-1]
left, right = yellowX[0], yellowX[-1]
print(top,bottom,left,right)
# Extract Region of Interest from unblurred original
ROI = orig[top:bottom, left:right]
Image.fromarray(ROI).save('result.png')
原始答案
好,您的黄色是 rgb(247,213,83)
,所以我们要查找所有黄色像素的X,Y坐标:
Ok, your yellow colour is rgb(247,213,83)
, so we want to find the X,Y coordinates of all yellow pixels:
#!/usr/bin/env python3
from PIL import Image
import numpy as np
# Open image and make into Numpy array
im = Image.open('image.png').convert('RGB')
na = np.array(im)
# Find X,Y coordinates of all yellow pixels
yellowY, yellowX = np.where(np.all(na==[247,213,83],axis=2))
# Find first and last row containing yellow pixels
top, bottom = yellowY[0], yellowY[-1]
# Find first and last column containing yellow pixels
left, right = yellowX[0], yellowX[-1]
# Extract Region of Interest
ROI=na[top:bottom, left:right]
Image.fromarray(ROI).save('result.png')
您可以使用 ImageMagick 在Terminal中执行完全相同的操作:
You can do the exact same thing in Terminal with ImageMagick:
# Get trim box of yellow pixels
trim=$(magick image.png -fill black +opaque "rgb(247,213,83)" -format %@ info:)
# Check how it looks
echo $trim
251x109+101+220
# Crop image to trim box and save as "ROI.png"
magick image.png -crop "$trim" ROI.png
如果仍使用 ImageMagick v6而不是v7,则将 magick
替换为 convert
.
If still using ImageMagick v6 rather than v7, replace magick
with convert
.
这篇关于Python:如何从图像(OpenCV,Numpy)中切出具有特定颜色的区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!