我该如何处理重叠的矩形?
How can I deal with overlapping rectangles?
我正在比较两个图像并使用 compare_ssim 找到差异,在这种情况下我得到了差异的轮廓,我需要通过在它周围绘制矩形来突出显示它,但我面临的问题是一些相互重叠的矩形我想删除那些重叠的。给出的是我的代码和图像。
import os
import csv
from datetime import datetime
from datetime import date
from datetime import timedelta
import tldextract
import time
import requests
import json
from urllib.parse import urlparse
import tldextract
import os
from PIL import Image
from PIL import ImageChops
from PIL import ImageDraw
from skimage.measure import compare_ssim
import numpy as np
import argparse
import imutils
import cv2
import urllib.request as req
import math
def is_contour_bad(c):
# approximate the contour
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02 * peri, True)
# the contour is 'bad' if it is not a rectangle
return not len(approx) == 4
initial_view = "first_image.jpg"
secondary_view = "seconda_image.jpg"
initial = cv2.imread(initial_view)
secondary = cv2.imread(secondary_view)
size_of_initial_image = heighta, widtha = initial.shape[:2]
size_of_secondary_image = heightb, widthb = secondary.shape[:2]
if size_of_initial_image == size_of_secondary_image:
grayA = cv2.cvtColor(initial, cv2.COLOR_BGR2GRAY)
grayB = cv2.cvtColor(secondary, cv2.COLOR_BGR2GRAY)
(score, diff) = compare_ssim(grayA, grayB, full=True)
diff = (diff * 255).astype("uint8")
if score == 1.0:
print('images are identical')
else:
thresh = cv2.threshold(diff, 0, 255,
cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
# output = secondary.copy()
# alpha = 0.3
threshold_area = 1000
for c in cnts:
if is_contour_bad(c):
pass
area = cv2.contourArea(c)
if area > threshold_area:
(x, y, w, h) = cv2.boundingRect(c)
cv2.rectangle(secondary, (x, y), (x + w , y + h), (0,255,255), 2)
else:
(x, y, w, h) = cv2.boundingRect(c)
if h >= 7 and w >= 7:
changed_w = w + 100
changed_h = h + 20
changed_x = x - 20
cv2.rectangle(secondary, (changed_x, y), (changed_x + changed_w , y + changed_h), (0,255,255), 2)
complete_path = "result_image.jpg"
cv2.imwrite( complete_path, secondary );
else:
continue
这听起来像是非最大抑制的问题。
Pyimagesearch 有一篇非常好的文章,我强烈推荐阅读。您可以像本文使用匹配算法的结果一样使用 compare_ssim 的结果。
我正在比较两个图像并使用 compare_ssim 找到差异,在这种情况下我得到了差异的轮廓,我需要通过在它周围绘制矩形来突出显示它,但我面临的问题是一些相互重叠的矩形我想删除那些重叠的。给出的是我的代码和图像。
import os
import csv
from datetime import datetime
from datetime import date
from datetime import timedelta
import tldextract
import time
import requests
import json
from urllib.parse import urlparse
import tldextract
import os
from PIL import Image
from PIL import ImageChops
from PIL import ImageDraw
from skimage.measure import compare_ssim
import numpy as np
import argparse
import imutils
import cv2
import urllib.request as req
import math
def is_contour_bad(c):
# approximate the contour
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02 * peri, True)
# the contour is 'bad' if it is not a rectangle
return not len(approx) == 4
initial_view = "first_image.jpg"
secondary_view = "seconda_image.jpg"
initial = cv2.imread(initial_view)
secondary = cv2.imread(secondary_view)
size_of_initial_image = heighta, widtha = initial.shape[:2]
size_of_secondary_image = heightb, widthb = secondary.shape[:2]
if size_of_initial_image == size_of_secondary_image:
grayA = cv2.cvtColor(initial, cv2.COLOR_BGR2GRAY)
grayB = cv2.cvtColor(secondary, cv2.COLOR_BGR2GRAY)
(score, diff) = compare_ssim(grayA, grayB, full=True)
diff = (diff * 255).astype("uint8")
if score == 1.0:
print('images are identical')
else:
thresh = cv2.threshold(diff, 0, 255,
cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
# output = secondary.copy()
# alpha = 0.3
threshold_area = 1000
for c in cnts:
if is_contour_bad(c):
pass
area = cv2.contourArea(c)
if area > threshold_area:
(x, y, w, h) = cv2.boundingRect(c)
cv2.rectangle(secondary, (x, y), (x + w , y + h), (0,255,255), 2)
else:
(x, y, w, h) = cv2.boundingRect(c)
if h >= 7 and w >= 7:
changed_w = w + 100
changed_h = h + 20
changed_x = x - 20
cv2.rectangle(secondary, (changed_x, y), (changed_x + changed_w , y + changed_h), (0,255,255), 2)
complete_path = "result_image.jpg"
cv2.imwrite( complete_path, secondary );
else:
continue
这听起来像是非最大抑制的问题。 Pyimagesearch 有一篇非常好的文章,我强烈推荐阅读。您可以像本文使用匹配算法的结果一样使用 compare_ssim 的结果。