Python:从图像(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
.
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcfggak
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20