簡體   English   中英

如何在此代碼中打印圖像之間差異中心的 (x,y) 坐標?

[英]how I can print (x,y) coordinate for the center of difference between images in this code?

我有這個代碼我想打印兩個圖像中差異中心的(x,y)坐標

import cv2
import numpy as np


original = cv2.imread("images/original_1.png")
duplicate = cv2.imread("images/original_1_edit.png")


#start image process
# check if 2 images are equals
if original.shape == duplicate.shape:
    print("The images have same size and channels")
    differenc = cv2.subtract(original, duplicate)
    #check the channelas RGB
    b, g, r = cv2.split(differenc)
    cv2.imshow("differenc", differenc)
    if cv2.countNonZero(b) == 0 and cv2.countNonZero(g) == 0 and cv2.countNonZero(r) == 0:
        print("The images completely Equal")

cv2.imshow("Original", original)
cv2.imshow("Duplicate", original)
cv2.waitKey(0)
cv2.destroyAllWindows()

當您減去圖像時,結果會顯示差異。 您可以使用閾值將其轉換為掩碼。 然后,您可以找到差異的輪廓,並使用 boundingRect 計算中心。

結果:
在此處輸入圖片說明

代碼:

    import cv2
    import numpy as np

     # load images
    img = cv2.imread("image.png")
    img2 = cv2.imread("image2.png")
    # create copy for image comparison
    img2_ori = img2.copy()
    # subtract to get difference
    diff =  cv2.subtract(img, img2)
    # create grayscale of diff
    gray =  cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
    # create a mask for the non black values (above 10) 
    ret,thresh1 = cv2.threshold(gray,10,255,cv2.THRESH_BINARY)
    # find contours in mask
    contours, hierarchy = cv2.findContours(thresh1, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    # calculate the center of each contour using the boundingrect
    for cnt in contours:
            x,y,w,h = cv2.boundingRect(cnt)
            centerX = x+ int(w/2)
            centerY = y+ int(h/2)
            print(centerX)
            print(centerY)
            # draw blue dot in center
            cv2.circle(img2,(centerX, centerY),5,(255,0,0),-1)
    #show images
    cv2.imshow("img", img)
    cv2.imshow("img2", img2_ori)
    cv2.imshow("diff", thresh1)
    cv2.imshow("result", img2)

    cv2.waitKey(0)
    cv2.destroyAllWindows() 

如果您的意思是打印 2 個不同圖像的中心:

from PIL import Image

img1 = Image.open("occhio1.jpg")
img2 = Image.open("occhio2.jpg")

center1 = (img1.width / 2, img1.height / 2)
center2 = (img2.width / 2, img2.height / 2)

print(str(center1) + " " + str(center2))

我使用了代表 Pillow 的 PIL 你可以使用 pip install Pillow 下載它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM