使用np.array_equal(img1, img2)函数来检查两个图像的像素数组是否完全相同

在加载图像之前,我们需要先确保img1img2分别是NumPy数组或者灰度图像文件的像素数组。

以下是两种加载图像的方法,分别用于加载NumPy数组和灰度图像文件:

1、加载NumPy数组:

import cv2
import numpy as np

# 假设你已经有了两个NumPy数组,分别是img1和img2

# 比较两幅图像是否相等
are_equal = np.array_equal(img1, img2)

# 输出结果
print(are_equal)

 2、加载灰度图像文件:

 

from PIL import Image
import numpy as np

# 假设图像文件路径分别为img1_path和img2_path

# 加载图像1
img1_path = "path/to/image1.png"
pil_img1 = Image.open(img1_path)
img1 = np.array(pil_img1)

# 加载图像2
img2_path = "path/to/image2.png"
pil_img2 = Image.open(img2_path)
img2 = np.array(pil_img2)

# 比较两幅图像是否相等
are_equal = np.array_equal(img1, img2)

# 输出结果
print(are_equal)

在以上代码示例中,img1img2可以是NumPy数组或者灰度图像文件的像素数组。如果这两个数组完全相等,则are_equal为True,否则为False。

解释如下代码:for pic_id1 in range(1,N_pic+1): print('matching ' + set_name +': ' +str(pic_id1).zfill(5)) N_CHANGE = 0 for T_id in range(1,16,3): for H_id in range(2,5): FAIL_CORNER = 0 data_mat1 = read_data(input_file,pic_id1,T_id,H_id) search_list = range( max((pic_id1-10),1),pic_id1)+ range(pic_id1+1, min((pic_id1 + 16),N_pic + 1 ) ) for cor_ind in range(0,N_cor): row_cent1 = cor_row_center[cor_ind] col_cent1 = cor_col_center[cor_ind] img_corner = data_mat1[(row_cent1-N_pad): (row_cent1+N_pad+1), (col_cent1-N_pad): (col_cent1+N_pad+1) ] if ((len(np.unique(img_corner))) >2)&(np.sum(img_corner ==1)< 0.8*(N_pad2+1)**2) : for pic_id2 in search_list: data_mat2 = read_data(input_file,pic_id2,T_id,H_id) match_result = cv2_based(data_mat2,img_corner) if len(match_result[0]) ==1: row_cent2 = match_result[0][0]+ N_pad col_cent2 = match_result[1][0]+ N_pad N_LEF = min( row_cent1 , row_cent2) N_TOP = min( col_cent1, col_cent2 ) N_RIG = min( L_img-1-row_cent1 , L_img-1-row_cent2) N_BOT = min( L_img-1-col_cent1 , L_img-1-col_cent2) IMG_CHECK1 = data_mat1[(row_cent1-N_LEF): (row_cent1+N_RIG+1), (col_cent1-N_TOP): (col_cent1+N_BOT+1) ] IMG_CHECK2 = data_mat2[(row_cent2-N_LEF): (row_cent2+N_RIG+1), (col_cent2-N_TOP): (col_cent2+N_BOT+1) ] if np.array_equal(IMG_CHECK1,IMG_CHECK2) : check_row_N = IMG_CHECK1.shape[0] check_col_N = IMG_CHECK1.shape[1] if (check_col_Ncheck_row_N>=25): match_all.append( (pic_id1, row_cent1, col_cent1, pic_id2 , row_cent2, col_cent2) ) search_list.remove(pic_id2) else: FAIL_CORNER = FAIL_CORNER +1 N_CHANGE = N_CHANGE + 1 #%% break if less than 1 useless corners, or have detected more than 10 images from 60 if(FAIL_CORNER <= 1): break
06-13
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值