파이썬 OpenCV의이 같은 이미지에 동일한 출력을 제공하지 않습니다

아론 :

나는 점검이 경우 이미지가 동일한 지 프로그램을 만들려고 해요. 둘 다 이미지와 실행됩니다 다음과 같은 코드가 있습니다 :

img = cv2.imread('canvas.png')
mask = np.zeros(img.shape[:2],np.uint8)
bgdModel = np.zeros((1,65),np.float64)
fgdModel = np.zeros((1,65),np.float64)
rect = (70,70,300,250)
cv2.grabCut(img,mask,rect,bgdModel,fgdModel,5,cv2.GC_INIT_WITH_RECT)
mask2 = np.where((mask==2)|(mask==0),0,1).astype('uint8')
img = img*mask2[:,:,np.newaxis]
img = img[84:191, 84:203]
count = 1
cv2.imwrite("tmp/"+str(count)+".png", img)

:이 실행 처음, 나는 다음과 같은 출력을 얻을 첫 번째 이미지를 . 시간이 7 ~ 초 후, 나는 그것을 할 정확히 같은 이미지, 나는 다음과 같은 출력을 얻을 : 두 번째 이미지를 . 나는 다시 시도하고, 다시 다른 출력을 가지고 : 세 번째 시도?

나는 이미지 (내용처럼) 동일 여부를 확인하기 위해 노력하고있어,하지만 난이 작업을 얻을 수 없습니다. 나는 유사성을 확인 유래에서 발견 다음 코드 조각을 사용하고 있습니다 :

def is_similar(image1, image2):
    return image1.shape == image2.shape and not(np.bitwise_xor(image1,image2).any())

그리고 두 번째 이미지로 먼저 확인 때 false를 반환합니다. 어떻게이 일을 할 수 있습니까?

시간 내 줘서 고마워,

==== 편집 ====

다음은 canvas.png은

==== EDIT 2 ====

@Rotem에서 자신의 답을 찾고 후, 나는 그것을 밖으로 시도했다, 그러나 그것은 여전히 보여줍니다 약간의 차이, 반환 위의 기능 False에를 : 사진 (1)Picutre 2

빨간색 :

cv2.grabCutGrabCut 알고리즘의 사용이 내장 때문에 결정 결과를 제공하지 않습니다 임의성 .

에 따르면 위키 백과 :

이것은 마르코프 구성하는 데 사용되는 임의의 픽셀 라벨을 통해 필드를 ...

당신은 실행하기 전에 OpenCV의의 랜덤 생성기의 씨앗을 재설정하여 난수를 방지 할 수 있습니다 cv2.grabCut:

cv2.setRNGSeed(0)

다음 코드 샘플입니다 :

for count in range(10):
    cv2.setRNGSeed(0)
    img = cv2.imread('canvas.png')
    mask = np.zeros(img.shape[:2],np.uint8)
    bgdModel = np.zeros((1,65),np.float64)
    fgdModel = np.zeros((1,65),np.float64)
    rect = (70,70,300,250)
    cv2.grabCut(img,mask,rect,bgdModel,fgdModel,5,cv2.GC_INIT_WITH_RECT)
    mask2 = np.where((mask==2)|(mask==0),0,1).astype('uint8')
    img = img*mask2[:,:,np.newaxis]
    img = img[84:191, 84:203]
    cv2.imwrite(str(count)+".png", img)

최신 정보:

당신은 이미지를 비교하기 위해 다음과 같은 루프를 사용할 수 있습니다 :

# Verify that all images are the same
for count in range(10):
    im = cv2.imread(str(count)+".png")
    is_same = is_similar(im, img)
    if not is_same:
        print('Images are not the same, and it is strange!')

내 컴퓨터에서 그들은 모두 동일합니다.


전체 코드 :

import cv2
import numpy as np

# Disable OpenCL and disable multi-threading.
cv2.ocl.setUseOpenCL(False)
cv2.setNumThreads(1)


def is_similar(image1, image2):
    return image1.shape == image2.shape and not(np.bitwise_xor(image1,image2).any())

for count in range(10):
    cv2.setRNGSeed(0)
    img = cv2.imread('canvas.png')
    mask = np.zeros(img.shape[:2],np.uint8)
    bgdModel = np.zeros((1,65),np.float64)
    fgdModel = np.zeros((1,65),np.float64)
    rect = (70,70,300,250)
    cv2.grabCut(img,mask,rect,bgdModel,fgdModel,5,cv2.GC_INIT_WITH_RECT)
    mask2 = np.where((mask==2)|(mask==0),0,1).astype('uint8')
    img = img*mask2[:,:,np.newaxis]
    img = img[84:191, 84:203]
    cv2.imwrite(str(count)+".png", img)


# Verify that all images are the same
for count in range(10):
    im = cv2.imread(str(count)+".png")
    is_same = is_similar(im, img)
    if not is_same:
        print('Images are not the same, and it is strange!')

추천

출처http://10.200.1.11:23101/article/api/json?id=388012&siteId=1