how to draw a rectangle to identify character

3 次查看(过去 30 天)
This code to draw a rectangle to identify character that I had found (overlay the template in a target).
[name,path] = uigetfile('*.jpg');
image1 = imread([path,name]);
[name,path] = uigetfile('*.jpg');
image2 = imread([path,name]);
if size(image1,3)==3
image1=rgb2gray(image1);
end
if size(image2,3)==3
image2=rgb2gray(image2);
end
% check which one is target and which one is template using their size
if size(image1)>size(image2)
Target=image1;
Template=image2;
else
Target=image2;
Template=image1;
end
% find both images sizes
[r1,c1]=size(Target);
[r2,c2]=size(Template);
% mean of the template
image22=Template-mean(mean(Template));
%corrolate both images
corrMat=[];
for i=1:(r1-r2+1)
for j=1:(c1-c2+1)
Nimage=Target(i:i+r2-1,j:j+c2-1);
Nimage=Nimage-mean(mean(Nimage)); % mean of image part under mask
corr=sum(sum(Nimage.*image22));
corrMat(i,j)=corr;
end
end

回答(3 个)

Image Analyst
Image Analyst 2014-3-29
That's a dangerous way to use size(). Looks like someone needs to read Steve's blog on the use of the size function.
Anyway, I'm not sure what your question is. Do you want to draw a rectangle in the overlay above the image? If so, use plot() or rectangle().
  2 个评论
Image Analyst
Image Analyst 2014-3-29
编辑:Image Analyst 2014-3-29
Anyway, you can't find a template in a target by correlation, at least not robustly. Think about it and if you don't know why, ask. You'll need to use normalized cross correlation, as shown in my attached demo.

请先登录,再进行评论。


ahmed
ahmed 2014-3-29
编辑:ahmed 2014-3-29
i want to draw a rectangle in a overlay the full image illuster to identify the character
  1 个评论
Image Analyst
Image Analyst 2014-3-30
Invert your image so that the letters are white and then run the normalized cross correlation code I gave you.

请先登录,再进行评论。


raha boolat
raha boolat 2021-7-14
编辑:Rik 2021-7-14
function obj_det(img_name)
f=imread (img_name);
if (size(f,3) >1)
f=rgb2gray(f);
f_bw=~im2bw(f,graythresh(f));
else
f_bw=~im2bw(f,graythresh(f));
end
f_bw=imfill(f_bw,'holes');
s=bwconncomp(f_bw);
imshow(f);
for i=1:s.NumObjects
ind=s.PixelIdxList{i};
[r,c]=ind2sub(size(f),ind);
min_r=min(r,[],1);
max_r=max(r,[],1);
min_c=min(c,[],1);
max_c=max(c,[],1);
hy=max_r - min_r;
hx=max_c - min_c;
rectangle('position',[min_c,min_r,hx,hy],'EdgeColor','r');
end
end
  1 个评论
Rik
Rik 2021-7-14
@raha boolat This time I edited your answer for you. Next time, please use the tools explained on this page to make your posts more readable.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by