Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用OpenCV和Python提取地板布局和阈值_Python_Image_Opencv_Image Thresholding_Ssim - Fatal编程技术网

使用OpenCV和Python提取地板布局和阈值

使用OpenCV和Python提取地板布局和阈值,python,image,opencv,image-thresholding,ssim,Python,Image,Opencv,Image Thresholding,Ssim,我尝试使用提取两幅图像之间的差异,以仅获得地板面积(image_a是原始图像,image_b已绘制地板) 预期的输出是阈值掩码 我遇到的问题是,ssim差异的阈值设置在我的案例中不起作用(示例如下所示) 有人能提供更好的阈值技术或理论吗 from skimage.measure import compare_ssim import cv2 ... image_a = cv2.imread(first) image_b = cv2.imread(second) gray_a = cv2.cvt

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

预期的输出是阈值掩码

我遇到的问题是,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)
检测到最大轮廓的图像 检测到最大轮廓的图像\u b 脱粒

通过对给定图像之间差值的平均值进行阈值化,可以获得更好的结果

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
def get_掩码(img1、img2、thresh):
如果img1.shape!=img2.shape:
返回
diff=cv2.absdiff(img1,img2)
差值=np.平均值(差值,轴=2)
差异[差异阈值]=255
掩码=np.dstack([diff]*3)
返回掩码


伪影可能会出现在生成的遮罩中,可以通过涂抹来减少。

尝试腐蚀和扩张。也许能给你一些想法。简单地说,调整侵蚀和膨胀参数,直到您对结果满意,然后使用
findContours
方法获得占地面积@陳翰群 谢谢,这是一篇很有帮助的文章,但它并没有删除我拥有的所有工件。那么更好的差异提取算法呢?此外,它可以帮助您过滤掉不需要的部分。例如,您可能正在寻找面积较大的轮廓。在您的例子中,您希望找到底部和右下角。这两个部分的面积比其他工件要大得多。嘿@arturkuchynski,我正在从事一个类似的项目,你能告诉我这个过程吗,或者至少,指出一个方向,以实现地板的遮罩效果,就像第一张图中的第二张图一样。谢谢我对此做了很多研究,但找不到该往哪个方向走。PyImageSearch的OpenCV博客内容丰富,但我觉得这不是我需要的解决方案。