gpt4 book ai didi

python - 使用 OpenCV 和 Python 提取楼层布局和阈值

转载 作者:太空宇宙 更新时间:2023-11-03 21:23:10 24 4
gpt4 key购买 nike

我试过使用 SSIM提取两个图像之间的差异以仅获取地板面积(image_a 是原始图像,image_b 已绘制(paint)地板)。

预期的输出是阈值掩码。

我遇到的问题是 ssim 差异的阈值在我的情况下不起作用(示例如下所示)。

有人可以提供更好的阈值技术或理论吗?

from skimage.measure import compare_ssim
import cv2
...

image_a = cv2.imread(first)
image_b = cv2.imread(second)

gray_a = cv2.cvtColor(image_a, cv2.COLOR_BGR2GRAY)
gray_b = cv2.cvtColor(image_b, cv2.COLOR_BGR2GRAY)

_, diff = compare_ssim(gray_a, gray_b, full=True, gaussian_weights=True)
diff = (diff * 255).astype("uint8")

thresh = cv2.threshold(diff, 0, 255,
cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]

contours = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]

contour_sizes = [(cv2.contourArea(contour), contour) for contour in contours]


if len(contour_sizes) > 0:
largest_contour = max(contour_sizes, key=lambda x: x[0])[1]
x, y, w, h = cv2.boundingRect(largest_contour)
cv2.rectangle(image_a, (x, y), (x + w, y + h), (36, 255, 12), 2)
cv2.rectangle(image_b, (x, y), (x + w, y + h), (36, 255, 12), 2)

cv2.imwrite('image_a.jpg', image_a)
cv2.imwrite('image_b.jpg',image_b)
cv2.imwrite('thresh.jpg', thresh)

检测到最大轮廓的图像_a enter image description here检测到最大轮廓的 image_b enter image description here打谷 enter image description here

最佳答案

通过对给定图像之间的差异的均值进行阈值处理可以获得更好的结果。

def get_mask(img1, img2, thresh):
if img1.shape != img2.shape:
return
diff = cv2.absdiff(img1, img2)
diff = np.mean(diff, axis=2)
diff[diff <= thresh] = 0
diff[diff > thresh] = 255
mask = np.dstack([diff] * 3)
return mask

thresh_morph

伪影可能会出现在生成的蒙版中,可以通过应用 Morphological Transformations 来减少.

关于python - 使用 OpenCV 和 Python 提取楼层布局和阈值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58230751/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com