- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试从此图像中裁剪一个居中(或不居中)的圆:
我从关于堆栈溢出的这个主题的现有问题中窃取了这段代码,但出了点问题:
import cv2
file = 'dog.png'
img = cv2.imread(file)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
circle = cv2.HoughCircles(img,
3,
dp=1.5,
minDist=10,
minRadius=1,
maxRadius=10)
x = circle[0][0][0]
y = circle[0][0][1]
r = circle[0][0][2]
rectX = (x - r)
rectY = (y - r)
crop_img = img[rectY:(rectY+2*r), rectX:(rectX+2*r)]
cv2.imwrite('dog_circle.png', crop_img)
输出:
Traceback (most recent call last):
File "C:\Users\Artur\Desktop\crop_circle - Kopie\crop_circle.py", line 14, in <module>
x = circle[0][0][0]
TypeError: 'NoneType' object is not subscriptable
cv2.HoughCircles()
似乎生成 None
而不是裁剪的圆形数组。我该如何解决这个问题?
最佳答案
首先:HoughCircles
用于检测图像上的圆圈,而不是裁剪它。
你不能有圆形图片。图像始终是矩形,但某些像素可以是透明的(RGBA
中的 alpha channel )并且程序不会显示它们。
因此,您可以先裁剪图像以使其具有正方形,然后添加包含哪些像素应该可见的信息的 alpha channel 。在这里,您可以使用黑色背景上带有白色圆圈的蒙版。最后,您必须将其保存为 png
或 tiff
,因为 jpg
无法保留 alpha channel 。
我为此使用模块 PIL
/pillow
。
我在图像中心裁剪正方形区域,但您可以为此使用不同的坐标。
接下来我创建具有相同大小和黑色背景的灰度图像并绘制白色圆/椭圆。
最后,我将这张图片作为 alpha channel 添加到裁剪后的图片中,并将其保存为 png
。
from PIL import Image, ImageDraw
filename = 'dog.jpg'
# load image
img = Image.open(filename)
# crop image
width, height = img.size
x = (width - height)//2
img_cropped = img.crop((x, 0, x+height, height))
# create grayscale image with white circle (255) on black background (0)
mask = Image.new('L', img_cropped.size)
mask_draw = ImageDraw.Draw(mask)
width, height = img_cropped.size
mask_draw.ellipse((0, 0, width, height), fill=255)
#mask.show()
# add mask as alpha channel
img_cropped.putalpha(mask)
# save as png which keeps alpha channel
img_cropped.save('dog_circle.png')
img_cropped.show()
结果
顺便说一句:
在蒙版中,您可以使用 0 到 255 之间的值,不同的像素可能具有不同的透明度 - 其中一些可以是半透明的,以形成平滑的边框。
如果您想在自己页面的 HTML 中使用它,则不必创建圆形图像,因为 Web 浏览器可以将图像的角圆化并将其显示为圆形。为此,您必须使用 CSS。
编辑:蒙版上有更多圆圈的示例。
from PIL import Image, ImageDraw
filename = 'dog.jpg'
# load image
img = Image.open(filename)
# crop image
width, height = img.size
x = (width - height)//2
img_cropped = img.crop((x, 0, x+height, height))
# create grayscale image with white circle (255) on black background (0)
mask = Image.new('L', img_cropped.size)
mask_draw = ImageDraw.Draw(mask)
width, height = img_cropped.size
mask_draw.ellipse((50, 50, width-50, height-50), fill=255)
mask_draw.ellipse((0, 0, 250, 250), fill=255)
mask_draw.ellipse((width-250, 0, width, 250), fill=255)
# add mask as alpha channel
img_cropped.putalpha(mask)
# save as png which keeps alpha channel
img_cropped.save('dog_2.png')
img_cropped.show()
关于python - 从图像中裁剪圆形缩略图的最简单方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58543750/
我制作手机游戏,但我想为我的社交游戏制作一些简单的基于浏览器的客户端,以便我们可以更轻松地调试。最简单的引擎是什么? 我研究了一些 HTML5 引擎和 GWT,但我想听听社区的意见。我正在寻找一种能够
有一个有趣的例子,有人设法为一个只占用 1kb 的论坛创建了一个 PHP 脚本:http://www.nerdparadise.com/blogs/blake/6034/ 我想知道是否有类似的小脚本可
首先我要吐槽一下,看程序的过程中遇见了yield这个关键字,然后百度的时候,发现没有一个能简单的让我懂的,讲起来真tm的都是头头是道,什么参数,什么传递的,还口口声声说自己的教程是最简单的,最浅显易
我的proguard配置太糟糕了,我的游戏崩溃了,而且似乎不正常。在我弄清楚到底哪里出了问题之前,最简单,最安全的配置用于全部安装是什么?有没有办法使它仅更改变量名?或者只是混淆代码以使其更难阅读而不
我正在重构一些 C# 代码,其中一部分是重做一些引用,因为我们正在完全重做文件夹结构。我想做的就是进入 .csproj 或 .sln 文件并修改路径。 然而,一些引用文献有类似的路径 "../../.
免责声明:这是一个理论问题,目的是增加我的理解。我知道我总是可以使用像 JSON 库这样的工具来解决问题。 假设我想创建一个逗号分隔的值列表,这些值本身可能包含逗号。这些逗号需要先转义。假设我使用 .
我对编程完全陌生,我选择 Delphi 作为我想学习的编程语言。 我基本上想构建使用套接字填写和提交 Web 表单的工具,并且我希望它们也是多线程的。 我希望它们功能丰富且性能正确。 我并不急于这样做
我正在构建一个协作创作工具,该工具允许用户共同编辑信息空间,该信息空间是节点和链接的可视化。一个客户端应用程序中所做的更改需要反映到所有其他客户端中。由于它是可视化的,因此可能需要经常更新潜在的大数据
作为一家专门开发自定义 CMS 的公司,我们被要求在下一个项目中部署开源 CMS。 我们可以自由选择系统。对于熟悉 PHP5 中的 MVC 模型和 OOP 的团队,您会推荐什么? 有人告诉我Drupa
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
我完全是 Ember.js 菜鸟,需要了解后端注意事项,目前似乎很少有教程涵盖。对于快速原型(prototype)设计,最简单/最简单的后端设置是什么?我看到了一些 ember-rails 教程,但是
我正在寻找这种最简单、最简单的方法来启动 Java Web 服务。我曾经使用 Axis-1-on-Tomcat,但是对于 Axis 2,它变得太厚了。我正在寻找的一些偏好: 低内存占用 - 一个包含最
我多年来一直使用旧版 openGL 和 cocoa,但现在我正在努力过渡到 openGL 3.2。互联网上有几个例子,但它们都太复杂了(许多甚至在 XCode 5.1 下不再编译)。有人可以编写一个最
我正在构建一个简单的应用程序,它应该将开关/支票簿的状态存储 7 天。我遇到的唯一问题是我用来构建所述应用程序的网站不适合手机上的本地存储。没有通过本地存储或链接到在线数据库来存储开关/支票簿状态的良
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 7 个月前。 Improv
有时我需要 1 个用户类型元素的集合(或任何其他容器)并以这种方式创建它们: boost::assign::list_of(typeVariable).convert_to_container >()
我的页面上有一个 DropDownList 和一个 TextBox。当用户在 DropDownList 中选择“其他”选项时,我想在其右侧显示一个文本框。我不想使用传统的回发技术。我希望这种交互是在客
说到编码,我还很年轻,而且我听说过很多关于组织的事情。有些使用部分,有些使用 div,有些使用 div 作为按钮,其他使用 css 中的输入来更改它。作为一个喜欢让他的代码干净、简单易懂但又正确的人,
我需要将带有变量项的 ContentValues 转换为 JSON 字符串,我可以将其保存到数据库中,并在以后用作 HTTP 请求的正文。网络上的所有内容都只会以相反的方向进行转换。 最佳答案 每当您
我的电脑上安装了 Python 3.7。想用tensorflow,发现基本不支持3.7,所以想(也)安装Python 3.6。 关于如何做到这一点有什么建议吗?我是否必须卸载 3.7 并将其替换为 3
我是一名优秀的程序员,十分优秀!