Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

The help required is to show the offset difference value between base image and reference image when there is a position change.

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

The help required is to How can we show the offset difference value between base image and reference image when there is a position change.

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

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

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

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

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

How can we show the offset difference value between base image and reference image when there is a position change.image description(Image1)

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

How can we show the offset difference value between base image and reference image when there is a position change.image description(Image1)(Image1)

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

How can we show the offset difference value between base image and reference image when there is a position change.image description(Image1)image description

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

How can we show the offset difference value between base image and reference image when there is a position change.image description(Image1)image description

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

How can we show the offset difference value between base image and reference image when there is a position change.image description(Image1)image descriptionchange?

Here are the Images attached.

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

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.attached. ( i resized your ~7 MB images and uploaded ~700 kb @sturkmen )

image1

image2

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

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)

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

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

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