【发布时间】:2021-06-15 15:48:42
【问题描述】:
所以我试图从这个paper 实现一个方法。我被困在必须找到病变最佳拟合椭圆的长轴与坐标系的 x 轴之间的角度的部分。
这是示例图片:
这是我目前得到的:
有可能找到那个角度吗?找到角度后,我必须将 RoI 沿 x 轴翻转该角度。
更新 ----------
到 Roi 图片的 Google 驱动器链接:RoI image
基于paper分步实现方法。
首先,我应该将 RoI 重新定位到图像坐标的中心。在论文中,他们使用其质心将 RoI 居中。我设法根据在answer 中找到的这段代码来做到这一点。如果我的 RoI 很小并且不触及图像边框,结果很好。但如果我有大图像,结果真的很糟糕。所以我最终使用 boundingRect 使 RoI 居中。这是居中的结果:
将 RoI 居中的代码:
import math
import cv2
import numpy as np
import matplotlib.pyplot as plt
# read image
cont_img = cv2.imread(r"C:\Users\Pandu\Desktop\IMD064_lesion.bmp", 0)
cont_rgb = cv2.cvtColor(cont_img, cv2.COLOR_GRAY2RGB)
# fit ellipse and find ellipse properties
hh, ww = cont_img.shape
contours, hierarchy = cv2.findContours(cont_img, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
ellipse = cv2.fitEllipse(contours[0])
(xc, yc), (d1, d2), angle = ellipse
# centering by centroid
half_width = int(ww/2)
half_height = int(hh/2)
offset_x = (half_width-xc)
offset_y = (half_height-yc)
T = np.float32([[1, 0, offset_x], [0, 1, offset_y]])
centered_by_centroid = cv2.warpAffine(cont_img.copy(), T, (ww, hh))
plt.imshow(centered_by_centroid, cmap=plt.cm.gray)
# centering by boundingRect
# This centered RoI is (L)
x, y, w, h = cv2.boundingRect(contours[0])
startx = (ww - w)//2
starty = (hh - h)//2
centered_by_boundingRect = np.zeros_like(cont_img)
centered_by_boundingRect[starty:starty+h, startx:startx+w] = cont_img[y:y+h, x:x+w]
plt.imshow(centered_by_boundingRect, cmap=plt.cm.gray)
第二,在将RoI居中后,我应该找到方向角度并根据该角度旋转RoI,然后翻转。使用来自 answer 的代码。 (这是旋转 RoI 的正确方法吗?):
# find ellipse properties of centered RoI
contours, hierarchy = cv2.findContours(centered_by_boundingRect, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
ellipse = cv2.fitEllipse(contours[0])
(xc, yc), (d1, d2), angle = ellipse
roi_centroid = (xc, yc)
rot_angle = 90 - angle
if rot_angle < 0:
rot_angle += 180
# This rotated RoI is (Lx)
M = cv2.getRotationMatrix2D(roi_centroid, -rot_angle, 1.0)
rot_im = cv2.warpAffine(centered_by_boundingRect, M, (ww, hh))
plt.imshow(rot_im, cmap=plt.cm.gray)
# (Ly)
# by passing 0 to flip() should flip image around x-axis, but I get the same result as the paper
res_flip_y = cv2.flip(rot_im.copy(), 0)
plt.imshow(res_flip_y , cmap=plt.cm.gray)
# (L) (xor) (Lx)
res_x_xor = cv2.bitwise_xor(centered_by_boundingRect, rot_im)
plt.imshow(res_x_xor, cmap=plt.cm.gray)
# (L) (xor) (Ly)
res_y_xor = cv2.bitwise_xor(centered_by_boundingRect, res_flip_x)
plt.imshow(res_y_xor, cmap=plt.cm.gray)
我仍然无法得到与论文相同的结果,旋转操作在大 RoI 上也会产生不好的结果。帮助...
更新 ---------- 20/03/2021
Small RoI:旋转结果很好,看起来与论文相似,但在 L (xor) Lx 或 L (xor) Ly 上仍然没有得到相同的最终结果
大投资回报率:随着投资回报率超出边界/图像,旋转结果不佳
【问题讨论】:
-
旋转操作产生不好的结果是什么意思?你能张贴一张你期望看到的照片吗?对我来说,它似乎旋转得很好。
-
@Ian 非常感谢您的回复...问题描述已更新...
-
如果问题是白色斑点从图像边缘伸出,您可以在图像上添加黑色边框,使其在旋转时足以容纳斑点。您可以使用 cv2.copyMakeBorder() 添加边框
标签: python opencv image-processing