How to show the offset difference value between base image and reference image in OpenCV?

asked 2020-10-01 02:56:34 -0600

sayom4u gravatar image

updated 2020-10-07 07:10:32 -0600

supra56 gravatar image

How can we show the offset difference value between base image and reference image when there is a position change?

Here are the Images attached. ( i resized your ~7 MB images and uploaded ~700 kb @sturkmen )

image1 image2

code :

import cv2
import numpy as np

def grab_contours(cnts):
    # if the length the contours tuple returned by cv2.findContours
    # is '2' then we are using either OpenCV v2.4, v4-beta, or
    # v4-official
    if len(cnts) == 2:
        cnts = cnts[0]

    # if the length of the contours tuple is '3' then we are using
    # either OpenCV v3, v4-pre, or v4-alpha
    elif len(cnts) == 3:
        cnts = cnts[1]

    # otherwise OpenCV has changed their cv2.findContours return
    # signature yet again and I have no idea WTH is going on
    else:
        raise Exception(("Contours tuple must have length 2 or 3, "
            "otherwise OpenCV changed their cv2.findContours return "
            "signature yet again. Refer to OpenCV's documentation "
            "in that case"))

    # return the actual contours array
    return cnts

image1 = cv2.imread("Img_Position1.png")
image2 = cv2.imread("Img_Position2.png")


ht = int(image1.shape[0] )  
wd = int(image1.shape[1])  
bgr = int(image1.shape[2])


print('Image1 Original Dimensions : ',image1.shape)
print('Image2 Original Dimensions : ',image2.shape)
print (ht)
print (wd)
print (bgr)

#difference = cv2.subtract(image1, image2)

if image1.shape == image2.shape:
    print("1")  
    difference = cv2.absdiff(image1,image2)
    b , g , r = cv2.split(difference)
    if cv2.countNonZero(b) == 0 and cv2.countNonZero(g) == 0 and cv2.countNonZero(r) == 0:
      print ("the images are same")
    else:
      print ("the images are different")      

 #image1 =cv2.resize(image1,(1000,600))
 #image2 =cv2.resize(image2,(1000,600))
else:
   print("2")
   image2 =cv2.resize(image2,(ht,wd))
   difference = cv2.subtract(image1,image2)
   b , g , r = cv2.split(difference)    

image1 =cv2.resize(image1,(1000,600))
image2 =cv2.resize(image2,(1000,600))

print('Image1 Original Dimensions : ',image1.shape)
print('Image2 Original Dimensions : ',image2.shape)

difference = cv2.resize(difference,(1000,600))


gray = cv2.cvtColor(difference, cv2.COLOR_BGR2GRAY)

for i in range(0, 3):
    dilated = cv2.dilate(gray.copy(), None, iterations= i+ 1)

(T, thresh) = cv2.threshold(dilated, 3, 255, cv2.THRESH_BINARY)

cnts = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = grab_contours(cnts)

for c in cnts:
# fit a bounding box to the contour
    (x, y, w, h) = cv2.boundingRect(c)
    cv2.rectangle(image2, (x, y), (x + w, y + h), (0, 255, 0), 2)



cv2.imshow("image1",image1)
cv2.imshow("image2",image2)
cv2.imwrite("changes.png", image2)

cv2.waitKey(0)
edit retag flag offensive close merge delete

Comments

1

what is a "offset difference value" ?

position change of what ?

berak gravatar imageberak ( 2020-10-01 03:16:06 -0600 )edit

By offset difference value i mean this.....suppose there is a position change between a base and a reference image.....now via OpenCV,I would want to show how much is the difference value in terms of the position change? If you share the code snippet of how to do that, then that will be much helpful.

sayom4u gravatar imagesayom4u ( 2020-10-01 03:27:41 -0600 )edit
1

suppose there is a position change between a base and a reference image.

how so ? please try to explain. do you have example images ?

take a look at the faq please, and try to improve your question

berak gravatar imageberak ( 2020-10-01 10:08:16 -0600 )edit
1

what i meant is that a component within the base image gets changed(by position) in the reference image. Is there a way to share the 2 images then I can do for your reference purpose if that helps.

sayom4u gravatar imagesayom4u ( 2020-10-02 01:39:46 -0600 )edit
1

edit your question, the "upload image" button is 6th from the left

berak gravatar imageberak ( 2020-10-02 01:57:46 -0600 )edit

I have uploaded the images. Can you please check and let me know the solution please. Tnx

sayom4u gravatar imagesayom4u ( 2020-10-03 04:30:44 -0600 )edit

yet nothing got uploaded, please try again ;)

berak gravatar imageberak ( 2020-10-03 07:02:56 -0600 )edit
  • I would want to show how much is the difference value in terms of the position change?

Used cv2.puttext

supra56 gravatar imagesupra56 ( 2020-10-03 09:16:38 -0600 )edit

@berak: I have attached once again. Please let me know if you can see it. If you still can't may I request you to please share your mail id where I can sent the 2 images please.....tnx

sayom4u gravatar imagesayom4u ( 2020-10-04 06:15:11 -0600 )edit

@supra56: Can you kindly share the exact code snippet please.

sayom4u gravatar imagesayom4u ( 2020-10-04 06:15:42 -0600 )edit