how to draw a rectangle to identify character
3 views (last 30 days)
Show older comments
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
Answers (3)
Image Analyst
on 29 Mar 2014
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 Comments
Image Analyst
on 29 Mar 2014
Edited: Image Analyst
on 29 Mar 2014
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
on 29 Mar 2014
Edited: ahmed
on 29 Mar 2014
1 Comment
Image Analyst
on 30 Mar 2014
Invert your image so that the letters are white and then run the normalized cross correlation code I gave you.
raha boolat
on 14 Jul 2021
Edited: Rik
on 14 Jul 2021
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 Comment
Rik
on 14 Jul 2021
@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.
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!