- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为学校做一个文本分割项目。我需要对二进制图像进行水平图像投影。我想要的结果是这样的:
.
我在 Python 中使用 OpenCV。我使用 x_sum = cv2.reduce(img, 0, cv2.REDUCE_SUM, dtype=cv2.CV_32S)
来获取总和数组,正如这个问题所建议的:horizontal and vertical projection of an image这个问题:Horizontal Histogram in OpenCV .
我尝试使用cv2.calcHist
获取水平投影图像,但我得到的只是一条水平线。我的代码如下:
image = cv2.imread(file_name)
x_sum = cv2.reduce(image, 0, cv2.REDUCE_SUM, dtype=cv2.CV_32S)
horizontal_projection=cv2.calcHist(x_sum,[0],None,[256],[0,256])
cv2.imwrite("image2.png", horizontal_projection)
请帮助并告诉我我做错了什么。我需要我的水平投影结果就像 Quora 示例一样。
最佳答案
在计算投影时,您基本上想要对图像每一行的像素求和。但是,您的文本是黑色的,它被编码为零,因此您会在一行中有很多文本的地方得到小数字,而在一行中文本很少的地方得到大数字 - 这与您想要的相反 - 所以你需要反转:
import cv2
import numpy as np
# Load as greyscale
im = cv2.imread('text.png', cv2.IMREAD_GRAYSCALE)
# Invert
im = 255 - im
# Calculate horizontal projection
proj = np.sum(im,1)
数组 proj
现在有 141 行高,每行对应于图像该行中的文本量:
array([ 0, 0, 0, 0, 40, 44, 144, 182, 264,
326, 425, 1193, 2718, 5396, 9272, 11880, 13266, 13597,
12906, 11962, 10791, 9647, 8554, 20469, 45426, 65714, 81397,
81675, 66590, 58714, 58046, 60516, 66136, 71794, 77552, 78555,
74868, 72083, 70139, 70160, 72174, 76409, 82854, 88962, 94721,
88105, 69126, 47753, 23966, 13845, 17406, 19145, 19079, 16548,
11524, 8511, 7465, 7042, 7197, 6577, 5022, 3476, 1797,
809, 450, 309, 348, 351, 250, 232, 271, 279,
251, 628, 1419, 3259, 6187, 8272, 9551, 9825, 9119,
7984, 6444, 5305, 4596, 13385, 31647, 46330, 57459, 56139,
42402, 34928, 33729, 35055, 38874, 41649, 43394, 43265, 41291,
40126, 39767, 40515, 42390, 44478, 46793, 47881, 47743, 43983,
36644, 28054, 18242, 15583, 20047, 22038, 21569, 17751, 10571,
6830, 6580, 6231, 5681, 4595, 2879, 1642, 771, 365,
320, 282, 105, 88, 76, 76, 28, 28, 28,
28, 0, 0, 0, 0, 0], dtype=uint64)
我将您的图片裁剪为 819x141 像素,如下所示:
有很多方法可以进行可视化。这是一个:
#!/usr/bin/env python3
import cv2
import numpy as np
# Load as greyscale
im = cv2.imread('text.png', cv2.IMREAD_GRAYSCALE)
# Invert
im = 255 - im
# Calculate horizontal projection
proj = np.sum(im,1)
# Create output image same height as text, 500 px wide
m = np.max(proj)
w = 500
result = np.zeros((proj.shape[0],500))
# Draw a line for each row
for row in range(im.shape[0]):
cv2.line(result, (0,row), (int(proj[row]*w/m),row), (255,255,255), 1)
# Save result
cv2.imwrite('result.png', result)
关于python - 如何在OpenCV中构造二值图像的水平投影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54285839/
每当我设置 Border.Effect属性的投影效果 控件中包含的每个控件都有一个投影。 有没有办法将阴影设置到边框而不是边框中包含的每个控件? 这是我的代码的一个简短示例:
我正在尝试创建一个带有阴影的对象。我相信您需要 CSS3 来执行此操作,到目前为止我已经有了类似的东西。 div { width:300px; height:100px; background
我希望能够为我的 drawables 文件夹中的矢量添加阴影。目前,当我将具有阴影的 svg 导入 Android Studio 时,转换为 xml 会删除阴影。 我将如何在 Android Stud
使用 NHibernate 2.1,我试图将一个实体及其子集合投影到 DTO 中。我的实体看起来像这样.. public class Application { public int Id {ge
我有问题还是好,我不知道如何将x,y,z值的3d点转换为2d点, 我必须绘制投影,其中的点确实有x,y,z值,但是我不知道如何将它们转换为2d,所以我可以将它们移动到我的轴上。 我一直在寻找Wiki和
我有域类位置 public abstract class BaseEntity where T: struct { public virtual T Id { get; set
我有一个使用 Android Material 图标作为背景的 ImageView 。我尝试添加标高以创建投影效果,但仅显示图标。是否可以将标高添加到矢量资源可绘制对象中? 矢量代码:
我正在尝试连接并以逗号分隔(或空格)列表并将其投影。我在下面有一些示例代码。 public class Friend { public string Name { get; set; } }
是否有任何库可以轻松地允许 Java bean 投影? 我有一个按照 Javabean 约定用 getter 和 setter 编写的 bean,并且在运行时在不同的地方我想要获取一个完全填充的 be
我可以成功地做到: point.transform(new OpenLayers.Projection("EPSG:900913"), new OpenLayers.Projection("EPSG:
我对 OpenLayers 3 有点问题。我有以下脚本: var map = new ol.Map({ view : new ol.View({ center : [5.611
我正在尝试向 ImageView 添加阴影。 Stackoverflow 的另一个答案似乎是使用 Canvas 和位图等,比需要的复杂得多。 在 iOS 上我会做这样的事情: myImageView.
我有一个 JPanel 元素,我想给它添加一个阴影,我怎样才能给元素添加一个漂亮的褪色阴影?我需要使用外部库还是可以使用内置的东西? 例子: 最佳答案 所以我查看了 swingx它扩展了 JPanel
如何使用 MongoDB 查询预测作者的名字 { name: "Wings Of Fire", author: { first: "Abdul", last: "Kalam" } }
我有一个集合“帐户”,其中包含类似于此结构的文档: { "email" : "john.doe@acme.com", "groups" : [ {
我试图弄清楚如何使用枚举列表(@ElementCollection)对实体进行 DTO 投影。不幸的是,缺少 QueryDsl 文档,在这里我只能找到版本 3 的结果 不是 适用于版本 4。 @Ent
我想要悬停 div 时出现的箭头 here也投下影子。箭头是从 CSS 绘制的: .arrow { position:absolute; margin-top:-50px; left:80px; bo
如何使用 QueryOver 和 AliasToBean 将枚举值转换为字符串值?我有以下但在尝试转换 Enum 时出错: SomeDTO someDTO = null;
我有这个外部 GeoJSON 文件: {"type": "FeatureCollection", "features": [ {"type":"Feature", "id":382, "propert
我对 Hibernate 的预测和标准有点困惑。何时使用预测以及何时使用标准? 最佳答案 它们并不相互排斥,您可以同时使用两者。预测通常在某些标准的背景下使用。 简单地说,Hibernate Proj
我是一名优秀的程序员,十分优秀!