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
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
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
2014년 3월 29일
편집: ahmed
2014년 3월 29일
댓글 수: 1
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
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
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 Center 및 File Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!