我正在使用opencl处理图像处理方面的更复杂方面。现在,我尝试的是模板匹配,但我得到了一些结果,但不确定是否正确。我认为模板仍然是匹配的..这是我使用的脚本和图像...
python文件:
import pyopencl as cl
import numpy as np
from PIL import Image
from time import time
import cv2
def getAssets(platform,device):
plat = cl.get_platforms()[platform]
dev = plat.get_devices()[device]
ctx = cl.Context([dev])
q = cl.CommandQueue(ctx)
return ctx,q
def getKernel():
string = "F:\Image processing\Template\matchTemplate.c"
krnl = open(string).read()
return krnl
def execution(image,template):
cntx,queue = getAssets(0,0)
img = np.asarray(image).astype(np.int32)
temp = np.asarray(template).astype(np.int32)
mf = cl.mem_flags
inp = cl.Buffer(cntx,mf.READ_ONLY | mf.COPY_HOST_PTR,hostbuf=img)
tmp = cl.Buffer(cntx,mf.READ_ONLY | mf.COPY_HOST_PTR,hostbuf=temp)
out = cl.Buffer(cntx,mf.WRITE_ONLY,img.nbytes)
task = cl.Program(cntx,getKernel()%(x,y)).build()
val = task.matchTmp(queue,img.shape,None,inp,tmp,out)
output = np.empty_like(img)
cl.enqueue_copy(queue,output,out)
output = output.astype(np.uint8)
print(output,output.min(),output.max())
return np.array(output)
def showResult(image,template):
result = execution(image,template)
result = Image.fromarray(result)
result.show()
if __name__ == '__main__':
image = cv2.imread("F:\Image processing\Template\cam.jpg",0)
template = cv2.imread("F:\Image processing\Template\Template.jpg",0)
global x,y
x = image.shape[1]
y = image.shape[0]
# print(image.shape)
# cv2.imshow("image",template)
# cv2.waitKey(0)
showResult(image,template)
内核文件:
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
__kernel void matchTmp(__global int *inp,__global int *tmp,__global int *out){
int j = get_global_id(1);
int i = get_global_id(0);
int x = get_global_id(3);
int y = get_global_id(2);
int Iwidth = %d;
int rown = %d;
int w = 48;
int r= 48;
int value = 0,value1,value2,value3;
value1 = value1 + half_powr((tmp[x*48 + y] - inp[(i+x)*Iwidth + (j+y)]),2);
value2 = value2 + half_powr(tmp[x*48 + y],2);
value3 = value3 + half_powr(inp[(i+x)*Iwidth + (j+y)],2);
value = (value1/half_sqrt(value2*value3))*50;
value = (value < 0 ? 0 : value);
value = (value > 255 ? 255 : value);
out[i*Iwidth + j] = value;
}
我使用了method=CV_TM_SQDIFF_NORMED
,如此站点OpenCV doc所示。我确实在物体附近获得了很高的价值,但是,为什么其他所有东西都是黑色的?实际结果应该类似于here所示。任何人都可以帮我这个忙!
[这是在Python / OpenCV中为您的图像进行模板匹配的演示,该图像显示了图像中最匹配的图像以及由matchTemplate()得到的相关表面图像中的最佳匹配的对应关系。
模板一次在图像上移动一个像素,然后逐个像素地将模板与图像中的当前位置进行比较,然后计算度量得分并将其记录在新图像中。因此,当输出图像在输入图像中移动时,其位置对应于模板的左上角。
输入:
模板:
import cv2
import numpy as np
# read image
img = cv2.imread('airplane.jpg')
# read template
tmplt = cv2.imread('airplane_tail.jpg')
hh, ww = tmplt.shape[:2]
# do template matching
# move template across image 1 pixe shift at a time and compute the correlation metric at each shift position as an image
corrimg = cv2.matchTemplate(img,tmplt,cv2.TM_CCOEFF_NORMED)
# get location of best match corresponding to the upper left corner of template from the brightest spot in corrimg
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(corrimg)
max_val_ncc = '{:.3f}'.format(max_val)
print("normalize_cross_correlation: " + max_val_ncc)
xx = max_loc[0]
yy = max_loc[1]
print('x_match_loc =',xx,'y_match_loc =',yy)
# draw template bounds in red onto img and corrimg scaled to 8-bit range
result1 = img.copy()
result2 = (255*corrimg).clip(0,255).astype(np.uint8)
result2 = cv2.merge([result2,result2,result2])
cv2.rectangle(result1, (xx, yy), (xx+ww, yy+hh), (0, 0, 255), 1)
cv2.rectangle(result2, (xx, yy), (xx+ww, yy+hh), (0, 0, 255), 1)
# save results
cv2.imwrite('airplane_template_match_location.jpg', result1)
cv2.imwrite('airplane_correlation_image_location.jpg', result2)
# show images
cv2.imshow('image', img)
cv2.imshow('template', tmplt)
cv2.imshow('result1', result1)
cv2.imshow('result2', result2)
cv2.waitKey(0)
cv2.destroyAllWindows()
在输入图像中匹配位置:
相关图像中的匹配位置:
文本输出:
normalize_cross_correlation: 0.996
x_match_loc = 216 y_match_loc = 190