Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image 利用matlab实现目标在图像中的位置检测_Image_Matlab_Optimization_Image Processing_Computer Vision - Fatal编程技术网

Image 利用matlab实现目标在图像中的位置检测

Image 利用matlab实现目标在图像中的位置检测,image,matlab,optimization,image-processing,computer-vision,Image,Matlab,Optimization,Image Processing,Computer Vision,我试图实现2D相关算法来检测图像中对象的位置,我不想使用任何内置函数来估计2D相关 这是我的密码: I=imread('image.tif'); % image is a black image contains white letters. h=imread('template.tif'); %template is a small image taken from the original image, it contains one white letter. I=doubl

我试图实现2D相关算法来检测图像中对象的位置,我不想使用任何内置函数来估计2D相关

这是我的密码:

I=imread('image.tif');      % image is a black image contains white letters.
h=imread('template.tif');   %template is a small image taken from the original image, it contains one white letter.
I=double(I);
h=double(h);
[nrows ncolumns]=size(I);
[nrows2 ncolumns2]=size(h);
C=zeros(nrows,ncolumns);

for u=1:(nrows-nrows2+1)
   for v=1:(ncolumns-ncolumns2+1)
       for x=1:nrows2
           for y=1:ncolumns2
               C(u,v)=C(u,v)+(h(x,y)*I(u+x-1,v+y-1));
           end
       end
  end
end

[maxC,ind] = max(C(:));
[m,n] = ind2sub(size(C),ind)   % the index represents the position of the letter.

output_image=(3.55/4).*C./100000;
imshow(uint8(output_image));
我认为它是有效的!但是它非常慢

如何用更好的代码替换以下代码以加速算法

   for x=1:nrows2
       for y=1:ncolumns2
           C(u,v)=C(u,v)+(h(x,y)*I(u+x-1,v+y-1));
       end
   end
我想每次我都有以下两个矩阵

h(1:nrows2,1:ncolumns2)
I(u:u+nrows2-1,v:v+ncolumns2-1)

还有一个问题,有什么改进吗


谢谢。

是的,有很多改进。你根本不需要for循环。由于您不想使用matlab的
xcorr2
函数,因此可以使用
conv2
。请参阅我给出的答案。

尽可能使用矩阵运算。因此,请尝试以下方法:

rowInds = (1:nrows2)-1;
colInds = (1:ncolumns2)-1;

temp = h.*I(u+rowInds,v+colInds);
C(u,v) = sum(temp(:));
而不是:

for x=1:nrows2
    for y=1:ncolumns2
        C(u,v)=C(u,v)+(h(x,y)*I(u+x-1,v+y-1));
    end
end

在傅里叶域中确定互相关如何?这将保证速度大幅提高

你说,一个物体。你能在同一张图像中用不同的物体,然后用不同质量/类型的图像来试一下吗?你说,一个物体。你能在同一幅图像中用不同的对象,然后用不同的质量/类型的图像来尝试吗?我现在不在机器前面。但是下面的链接可以给你一些优化的想法。PS:这不是一个很好的手机发帖。我真的想删除第一条评论。图像是一个简单的黑色图像,包含白色字母,模板是一个黑色小图像,包含一个白色字母,在输出图像中,我在模板中的字母处找到最大值(我尝试了多个模板)。我的回答假设您只想使用矩阵运算和for循环。如果你可以使用FFT,或者卷积函数,那么就使用它们,正如其他人在下面所说的。谢谢你,我已经在频域中应用了它(速度更快),但我想在空域中应用它。