- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我的应用程序:我正在尝试旋转图像(使用 OpenCV 和 Python)
目前我开发了以下代码,它旋转输入图像,用黑色边框填充它,给我 A。我想要的是 B - 旋转图像中最大可能的区域裁剪窗口。我称之为轴对齐boundED box。
这与 Rotate and crop 基本相同,但是我无法得到该问题的答案。此外,该答案显然仅对方形图像有效。我的图片是矩形的。
给出A的代码:
import cv2
import numpy as np
def getTranslationMatrix2d(dx, dy):
"""
Returns a numpy affine transformation matrix for a 2D translation of
(dx, dy)
"""
return np.matrix([[1, 0, dx], [0, 1, dy], [0, 0, 1]])
def rotateImage(image, angle):
"""
Rotates the given image about it's centre
"""
image_size = (image.shape[1], image.shape[0])
image_center = tuple(np.array(image_size) / 2)
rot_mat = np.vstack([cv2.getRotationMatrix2D(image_center, angle, 1.0), [0, 0, 1]])
trans_mat = np.identity(3)
w2 = image_size[0] * 0.5
h2 = image_size[1] * 0.5
rot_mat_notranslate = np.matrix(rot_mat[0:2, 0:2])
tl = (np.array([-w2, h2]) * rot_mat_notranslate).A[0]
tr = (np.array([w2, h2]) * rot_mat_notranslate).A[0]
bl = (np.array([-w2, -h2]) * rot_mat_notranslate).A[0]
br = (np.array([w2, -h2]) * rot_mat_notranslate).A[0]
x_coords = [pt[0] for pt in [tl, tr, bl, br]]
x_pos = [x for x in x_coords if x > 0]
x_neg = [x for x in x_coords if x < 0]
y_coords = [pt[1] for pt in [tl, tr, bl, br]]
y_pos = [y for y in y_coords if y > 0]
y_neg = [y for y in y_coords if y < 0]
right_bound = max(x_pos)
left_bound = min(x_neg)
top_bound = max(y_pos)
bot_bound = min(y_neg)
new_w = int(abs(right_bound - left_bound))
new_h = int(abs(top_bound - bot_bound))
new_image_size = (new_w, new_h)
new_midx = new_w * 0.5
new_midy = new_h * 0.5
dx = int(new_midx - w2)
dy = int(new_midy - h2)
trans_mat = getTranslationMatrix2d(dx, dy)
affine_mat = (np.matrix(trans_mat) * np.matrix(rot_mat))[0:2, :]
result = cv2.warpAffine(image, affine_mat, new_image_size, flags=cv2.INTER_LINEAR)
return result
最佳答案
这个解决方案/实现背后的数学相当于 this solution of an analagous question ,但公式被简化并避免了奇异性。这是与其他解决方案中的 largest_rotated_rect
具有相同接口(interface)的 python 代码,但在几乎所有情况下都提供更大的区域(始终被证明是最佳的):
def rotatedRectWithMaxArea(w, h, angle):
"""
Given a rectangle of size wxh that has been rotated by 'angle' (in
radians), computes the width and height of the largest possible
axis-aligned rectangle (maximal area) within the rotated rectangle.
"""
if w <= 0 or h <= 0:
return 0,0
width_is_longer = w >= h
side_long, side_short = (w,h) if width_is_longer else (h,w)
# since the solutions for angle, -angle and 180-angle are all the same,
# if suffices to look at the first quadrant and the absolute values of sin,cos:
sin_a, cos_a = abs(math.sin(angle)), abs(math.cos(angle))
if side_short <= 2.*sin_a*cos_a*side_long or abs(sin_a-cos_a) < 1e-10:
# half constrained case: two crop corners touch the longer side,
# the other two corners are on the mid-line parallel to the longer line
x = 0.5*side_short
wr,hr = (x/sin_a,x/cos_a) if width_is_longer else (x/cos_a,x/sin_a)
else:
# fully constrained case: crop touches all 4 sides
cos_2a = cos_a*cos_a - sin_a*sin_a
wr,hr = (w*cos_a - h*sin_a)/cos_2a, (h*cos_a - w*sin_a)/cos_2a
return wr,hr
以下是该功能与其他解决方案的比较:
>>> wl,hl = largest_rotated_rect(1500,500,math.radians(20))
>>> print (wl,hl),', area=',wl*hl
(828.2888697391496, 230.61639227890998) , area= 191016.990904
>>> wm,hm = rotatedRectWithMaxArea(1500,500,math.radians(20))
>>> print (wm,hm),', area=',wm*hm
(730.9511000407718, 266.044443118978) , area= 194465.478358
与[0,pi/2[
中的角度angle
旋转图像的边界框(宽度w
,高度 h
) 具有以下维度:
w_bb = w*cos_a + h*sin_a
h_bb = w*sin_a + h*cos_a
如果 w_r
, h_r
是计算出的裁剪图像的最佳宽度和高度,则边界框的插入为:
(w_bb-w_r)/2
(h_bb-h_r)/2
证明:
寻找具有最大面积的两条平行线之间的轴对齐矩形是一个具有一个参数的优化问题,例如x
如下图:
让 s
表示两条平行线之间的距离(它将变成旋转矩形的较短边)。那么求知矩形的边a
、b
分别与x
、s-x
、s-x
的比例恒定.,即 x = a sin α 和 (s-x) = b cos α:
所以最大化面积a*b
意味着最大化x*(s-x)
。由于直角三角形的“高度定理”,我们知道 x*(s-x) = p*q = h*h
。因此在 x = s-x = s/2
处达到最大面积,即平行线之间的两个角 E、G 在中线上:
此解决方案仅在此最大矩形适合旋转矩形时才有效。因此,对角线 EG
不得长于旋转矩形的另一边 l
。自从
EG = AF + DH = s/2*(cot α + tan α) = s/(2sin αcos α) = s/sin 2*α
我们有条件 s ≤ lsin 2α,其中 s 和 l 是旋转矩形的短边和长边。
在 s > lsin 2α 的情况下,参数 x
必须小于(小于 s/2)并且 s.t.广受欢迎的矩形的所有角都位于旋转矩形的一侧。这导致方程
x*cot α + (s-x)*tan α = l
给出 x = sin α*(lcos α - ssin α)/cos 2*α。从 a = x/sin α 和 b = (s-x)/cos α 我们得到上面用到的公式。
关于python - 旋转图像并裁剪黑色边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16702966/
我的文本字段需要一个边框,如下图所示。我该怎么做? 最佳答案 试试这些..我希望它能帮助... UITextField *txt=[[UITextField alloc]initWithFrame:C
我尝试通过“GradientDrawable”改变颜色“描边”,但不起作用。 另外,我不知道如何获取 id stroke,并且只更改笔划(我看到 google,所有示例都失败了) 我的 XML 项目
有没有办法如何使用css(理想)来绘制元素边框但只是线条的一部分(在左右边框下方的图像中)? 最佳答案 是的,你可以,像这样,甚至 IE8 也可以这样做: div { position: re
我一直致力于自定义 GUI 框架,因为我无法处理需要通过标记 (XAML) 开发 UI 的托管废话或 native 代码。我正在尝试创建一个使用该 GUI 框架的应用程序原型(prototype),但
以下Microsoft example code包含以下内容: ... ... ... 但是,在运行时,此代码会生成以下数据绑定(bind)
所以基本上我在tex文件的顶部有这样的内容: \setbeamertemplate{footline}{Number \insertframenumber} 这会将“Number ”应用于所有帧的页脚
我不明白为什么我的 HBox 周围没有边框?现在什么也没有发生,除了 eclipse 抛出 IllegalArgumentException 之外,因为我猜是 this.setCenter(hbox)
我正在尝试使用 Colorbox,以便它在加载时打开并提醒访问者 session 注册已开放。我在 Javascript Coder 找到了我正在使用的代码。 我无法让“X”关闭Colorbox来显示
我制作了一个自定义面板,类似于 WrapPanel 但带有列(或类似于 Grid 但项目自动定位在网格中)。 这是它的样子: 我想在我的面板上有一个属性,它在每一列之间划一条线。是否可以在自定义面板上
我正在尝试为我的页面制作边框,但我没有这样做..我的代码是 @drawable/custom_border 我的问题是,黑色设置为整个 View (作为背景),我想要黑色边框而不是黑色背景
我目前正在开发一个小游戏,但我无法让圆圈正确击中左侧和顶部 Canvas 边框。它正确地击中了右侧和底部。 圆可以用 W A S D 移动,并且必须正确地碰到 Canvas 的所有边界 这是代码:ht
我有一个像这样的图像 slider :
如何绘制具有透明度的png图像轮廓(边框)。 就像我们有这张图片: 我们想要这个: 我正在通过 HTML5 创建图像阴影,但无法获得我想要的。我想使用 HTML5 或 jquery 或两者来绘制它,如
在我们的主页上,我们有几个元素作为菜单点。(图片+标题+小说明)。问题是当鼠标悬停时,菜单元素总是有边框。 (即使我隐藏边框)。 你可以看看:www.scf-software.com 图片问题: im
我对 HTML 和 CSS 完全陌生,所以我希望有人能给我指出正确的方向。我试图理解本教程以制作视差滚动网站: http://ihatetomatoes.net/simple-parallax-scr
如何使底部边框正好位于文本框下方? div { border-bottom: solid 2px #354458; } p { color: #ffffff; background-col
本质上,我想应用一个 bottom-border,但我不希望它位于单元格的底部,而是位于死 Angular 。 如何仅使用 CSS 和 HTML(并且不使用图形)做到这一点? 假设单元格/行的高度为
我有一个带有线性渐变的 block (div)。有没有可能让右上角切出一个三 Angular 形? 例如,你有 border-radius 5px 来制作一个圆 Angular 的 block 。但是
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 6 年前。 Improve this qu
我想按照下面的设计图实现下拉按钮。查看下拉菜单在按钮中间后开始。我的问题是按钮具有透明背景以利用根父 div 中的背景图像。 到目前为止,我已经实现了下图。正如我上面所说,我想在 border-rad
我是一名优秀的程序员,十分优秀!