-1

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 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

enter image description here

Alpha Romeo
  • 84
  • 10
  • I'm not sure what you would consider a good solution. If you want exactly what you stated, then the solution is straightforward: detect all overlapping rectangles (solutions available in many places on this site and elsewhere on line), and remove those from the list of rectangles. If you want to remove any rectangle that overlaps an earlier one, then perform the detection as you create each new rectangle -- and don't add the new one. If you have some other resolution in mind, then post that description and your coding attempt. – Prune Feb 08 '19 at 17:15
  • Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. Your posted code has many undefined symbols and no output: you didn't format the image properly. – Prune Feb 08 '19 at 17:17
  • Look at the threshold image you use to find the contours. Maybe you can clean it up a bit and that might solve your issue. – J.D. Feb 08 '19 at 19:30
  • Thanks @Prune I have now posted full code of mine you just need to add to random images into it and you will then able to reproduce the problem – Alpha Romeo Feb 11 '19 at 10:41

1 Answers1

1

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.

Shawn Mathew
  • 2,198
  • 1
  • 14
  • 31