How can I deal with overlapping rectangles? [closed]

2024/9/22 10:30:29

I am comparing two images and find the difference using compare_ssim, in that case I got the contours of differences, which i need to highlight by drawing rectangle around it, but I am facing the issue that some of the rectangles overlapping each other I want to remove those overlapping. Given is my code and the image.

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 mathdef is_contour_bad(c):# approximate the contourperi = cv2.arcLength(c, True)approx = cv2.approxPolyDP(c, 0.02 * peri, True)# the contour is 'bad' if it is not a rectanglereturn not len(approx) == 4initial_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.3threshold_area = 1000for c in cnts:if is_contour_bad(c):passarea = 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 + 100changed_h = h + 20changed_x = x - 20cv2.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

enter image description here

Answer

This sounds like a problem for Non Maximum Suppression. Pyimagesearch has a pretty good article on it which I highly recommend reading. You can use the result of compare_ssim similar to how the article uses the result of the matching algorithm.

https://en.xdnf.cn/q/119145.html

Related Q&A

panda df not showing all rows after loading from MS SQL

Im using Pandas with latest sqlalchemy (1.4.36) to query a MS SQL DB, using the following Python 3.10.3 [Win] snippet: import pandas as pd # from sqlalchemy…

How to extract multiple grandchildren/children from XML where one child is a specific value?

Im working with an XML file that stores all "versions" of the chatbot we create. Currently we have 18 versions and I only care about the most recent one. Im trying to find a way to extract a…

Is there are a way to replace python import with actual sources?

I am having python files with the import statements which I would like to replace into the actual code placed in the foo.py.For instance, with the in file:from foo import Barbar = Bar() print barI woul…

How to use supported numpy and math functions with CUDA in Python?

According to numba 0.51.2 documentation, CUDA Python supports several math functions. However, it doesnt work in the following kernel function: @cuda.jit def find_angle(angles):i, j = cuda.grid(2)if i …

PYTHON - Remove tuple from list if contained in another list

I have a list of tuples:list_of_tuples = [(4, 35.26), (1, 48.19), (5, 90.0), (3, 90.0)]tuple[0] is an item_IDtuple[1] is an angleI have a list of item_IDs I want to remove/ignore from the list:ignore_I…

How to run a ij loop in Python, and not repeat (j,i) if (i,j) has already been done?

I am trying to implement an "i not equal to j" (i<j) loop, which skips cases where i = j, but I would further like to make the additional requirement that the loop does not repeat the perm…

Split a string with multiple delimiters

I have the string "open this and close that" and I want to obtain "open this and" and "close that". This is my best attempt:>>>print( re.split(r[ ](?=(open|clos…

Extracting a string between 2 chracters using python [duplicate]

This question already has answers here:Python-get string between to characters(4 answers)Closed 7 years ago.I need a Python regex to give me all the strings between ~ and ^ from a string like this:~~~~…

remove empty line printed from hive query output using python

i am performing a hive query and storing the output in a tsv file in the local FS. I am running a for loop for the hive query and passing different parameters. If the hive query returns no output once …

.exceptions.WebDriverException: Message: Can not connect to the Service

struggling to find a solution all over, have latest chrome 117 and also downloaded chromedriver and used the path accordingly in script also tried with chrome browser Although it opens the browser but …