Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

How can I count one matrix in big another matrix ?

조회 수: 1 (최근 30일)
Hamid MF
Hamid MF 2012년 11월 8일
마감: MATLAB Answer Bot 2021년 8월 20일
Hi guys I'm trying to writing program to classified many images (hand writing alphabet for arabic font like 'ا' ) so i decided to convert them into binary images then I used one function to get Skeleton of the font. after that I figured out I can count the edge of alphabet with this algorithm for example We have
A=[ 0 0 0 0 0 ;
0 0 1 0 0 ;
0 0 1 0 0 ;
0 0 1 0 0 ;
0 0 0 0 0 ]
I have to find out how many
B=[ 0 0 0;
0 1 0;
0 1 0 ]
And
C=[ 0 1 0;
0 1 0;
0 0 0 ]
available in A matrix ?
output must return 2 for this example, so the edges are 2
Are there any functions in matlab to do this ?
Thank You for your helping ...

답변 (2개)

Matt Fig
Matt Fig 2012년 11월 8일
편집: Matt Fig 2012년 11월 8일
There may be something in the IPT that does this, or you can try this FEX file out:
A=[0 0 0 0 0;0 0 1 0 0;0 0 1 0 0;0 0 1 0 0;0 0 0 0 0];
B=[0 0 0;0 1 0;0 1 0];
C=[0 1 0;0 1 0;0 0 0];
findsubmat(A,B) % Returns index of location of B in A.
length(findsubmat(A,C)) % Returns num of Cs in A.
  댓글 수: 2
Sean de Wolski
Sean de Wolski 2012년 11월 8일
편집: Sean de Wolski 2012년 11월 8일
I think this is kind of what bwhitmiss does. Though I would probably just use conv2 and some logical conditions:
sum(conv2(A,B,'same')==sum(B(:)))
Walter Roberson
Walter Roberson 2012년 11월 9일
Hamid writes "thank's bro it worked ..."

Image Analyst
Image Analyst 2012년 11월 9일
Sean's solution will do what you asked, but you might want to look into normxcorr2() which would do a better job of finding the whole character. In fact it might even do a better job if you don't convert to binary. Okay, you're probably wondering how you can use this strange new function. Well, I have a demo for that (like dozens of other things). Here it is:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
format longg;
format compact;
fontSize = 20;
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
% Read in a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'peppers.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 2, 1);
imshow(rgbImage, []);
axis on;
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
smallSubImage = imcrop(rgbImage, [192 82 60 52]);
subplot(2, 2, 2);
imshow(smallSubImage, []);
axis on;
title('Template Image to Search For', 'FontSize', fontSize);
% Search the red channel for a match.
correlationOutput = normxcorr2(smallSubImage(:,:,1), rgbImage(:,:,1));
subplot(2, 2, 3);
imshow(correlationOutput, []);
title('Correlation Output', 'FontSize', fontSize);
[maxCorrValue, maxIndex] = max(abs(correlationOutput(:)));
[ypeak, xpeak] = ind2sub(size(correlationOutput),maxIndex(1));
corr_offset = [(xpeak-size(smallSubImage,2)) (ypeak-size(smallSubImage,1))];
subplot(2, 2, 4);
imshow(rgbImage);
hold on;
rectangle('position',[corr_offset(1) corr_offset(2) 50 50],...
'edgecolor','g','linewidth',2);
title('Template Image Found in Original Image', 'FontSize', fontSize);

이 질문은 마감되었습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by