How to give output name of image matched from template matching image processing method?

조회 수: 4 (최근 30일)
hi, i have a code that will take the query image from webcam and will do some template matching process with image templates in database. after doing the template matching, i want the program to show the name of the template image matched from the database. how am i going to do that?

채택된 답변

Florian Morsch
Florian Morsch 2018년 5월 18일
Create a if- statememt.
file = 'H:\user4\matlab\myfile.txt'; % example file
[filepath,name,ext] = fileparts(file)
If(condition to fullfill your if, like "ObjectFound == 1")
fprintf = ('file name is %s',name) % this will display the text in the command window.
end
  댓글 수: 10
NUR SHOLIHAH RAMLEE
NUR SHOLIHAH RAMLEE 2018년 5월 30일
file1 = 'C:\Users\nuramlee\Desktop\MATTrain\sent\voltread1\100.txt'; % example file [filepath1,name1,ext1] = fileparts(file1 ) file2 = 'C:\Users\nuramlee\Desktop\MATTrain\sent\voltread1\120.txt'; % example file [filepath2,name2,ext2] = fileparts(file2 ) file3 = 'C:\Users\nuramlee\Desktop\MATTrain\sent\voltread1\220.txt'; % example file [filepath3,name3,ext3] = fileparts(file3 ) file4 = 'C:\Users\nuramlee\Desktop\MATTrain\sent\voltread1\240.txt'; % example file [filepath4,name4,ext4] = fileparts(file4 )
%specify the location of image dataDir = 'C:\Users\nuramlee\Desktop\MATTrain\sent\voltread1'; voltread = imageDatastore(dataDir);
%% %Index the image set. This process can take a few minutes. imageIndex = indexImages(voltread); % % figure; % % imshow(imageIndex); %% %select and display the query image %image must be in the 256x156 pixel size!% img = imread('100AA.bmp'); %Input image %Show input image figure, imshow(img); img = rgb2gray(img); img = double (img);
%%Canny Edge%% %Value for Thresholding T_Low = 0.075; T_High = 0.175;
%Gaussian Filter Coefficient B = [2, 4, 5, 4, 2; 4, 9, 12, 9, 4;5, 12, 15, 12, 5;4, 9, 12, 9, 4;2, 4, 5, 4, 2 ]; B = 1/159.* B;
%Convolution of image by Gaussian Coefficient A=conv2(img, B, 'same');
%Filter for horizontal and vertical direction KGx = [-1, 0, 1; -2, 0, 2; -1, 0, 1]; KGy = [1, 2, 1; 0, 0, 0; -1, -2, -1];
%Convolution by image by horizontal and vertical filter Filtered_X = conv2(A, KGx, 'same'); Filtered_Y = conv2(A, KGy, 'same');
%Calculate directions/orientations arah = atan2 (Filtered_Y, Filtered_X); arah = arah*180/pi;
pan=size(A,1); leb=size(A,2);
%Adjustment for negative directions, making all directions positive for i=1:pan for j=1:leb if (arah(i,j)<0) arah(i,j)=360+arah(i,j); end; end; end;
arah2=zeros(pan, leb);
%Adjusting directions to nearest 0, 45, 90, or 135 degree for i = 1 : pan for j = 1 : leb if ((arah(i, j) >= 0 ) && (arah(i, j) < 22.5) (arah(i, j) >= 157.5) && (arah(i, j) < 202.5) (arah(i, j) >= 337.5) && (arah(i, j) <= 360)) arah2(i, j) = 0; elseif ((arah(i, j) >= 22.5) && (arah(i, j) < 67.5) (arah(i, j) >= 202.5) && (arah(i, j) < 247.5)) arah2(i, j) = 45; elseif ((arah(i, j) >= 67.5 && arah(i, j) < 112.5) (arah(i, j) >= 247.5 && arah(i, j) < 292.5)) arah2(i, j) = 90; elseif ((arah(i, j) >= 112.5 && arah(i, j) < 157.5) (arah(i, j) >= 292.5 && arah(i, j) < 337.5)) arah2(i, j) = 135; end; end; end;
figure, imagesc(arah2); colorbar;
%Calculate magnitude magnitude = (Filtered_X.^2) + (Filtered_Y.^2); magnitude2 = sqrt(magnitude);
BW = zeros (pan, leb);
%Non-Maximum Supression for i=2:pan-1 for j=2:leb-1 if (arah2(i,j)==0) BW(i,j) = (magnitude2(i,j) == max([magnitude2(i,j), magnitude2(i,j+1), magnitude2(i,j-1)])); elseif (arah2(i,j)==45) BW(i,j) = (magnitude2(i,j) == max([magnitude2(i,j), magnitude2(i+1,j-1), magnitude2(i-1,j+1)])); elseif (arah2(i,j)==90) BW(i,j) = (magnitude2(i,j) == max([magnitude2(i,j), magnitude2(i+1,j), magnitude2(i-1,j)])); elseif (arah2(i,j)==135) BW(i,j) = (magnitude2(i,j) == max([magnitude2(i,j), magnitude2(i+1,j+1), magnitude2(i-1,j-1)])); end; end; end;
BW = BW.*magnitude2; figure, imshow(BW);
%Hysteresis Thresholding T_Low = T_Low * max(max(BW)); T_High = T_High * max(max(BW));
T_res = zeros (pan, leb);
for i = 1 : pan for j = 1 : leb if (BW(i, j) < T_Low) T_res(i, j) = 0; elseif (BW(i, j) > T_High) T_res(i, j) = 1; %Using 8-connected components elseif ( BW(i+1,j)>T_High BW(i-1,j)>T_High BW(i,j+1)>T_High BW(i,j-1)>T_High BW(i-1, j-1)>T_High BW(i-1, j+1)>T_High BW(i+1, j+1)>T_High BW(i+1, j-1)>T_High) T_res(i,j) = 1; end; end; end;
edge_final = uint8(T_res.*255); %Show final edge detection result figure, imshow(edge_final); queryImage = edge_final; % % figure % % imshow(queryImage) % % axes(handles.axes2) %% %retrieve the best matches. the [queryWords] output contains visual word %locations information for the query image. Use this information to %verify the search result. [imageIDs, ~, queryWords] = retrieveImages(queryImage,imageIndex);
%% %find the best match for the query image by extracting the visual words %from image index. the image index contains the visual word information %for all the images in index
bestMatch = imageIDs(1); bestImage = imread(imageIndex.ImageLocation{bestMatch}); bestMatchWords = imageIndex.ImageWords(bestMatch);
%% %Generate a set of tentative matches based on visual word assignments. %Each visual word in the query can have multiple matches %due to the hard quantization used to assign visual words.
queryWordsIndex = queryWords.WordIndex; bestMatchWordIndex = bestMatchWords.WordIndex;
tentativeMatches = []; for i = 1:numel(queryWords.WordIndex)
idx = find(queryWordsIndex(i) == bestMatchWordIndex);
matches = [repmat(i, numel(idx), 1) idx];
tentativeMatches = [tentativeMatches; matches];
end %% %Show the point locations for the tentative matches. %There are many poor matches.
points1 = queryWords.Location(tentativeMatches(:,1),:); points2 = bestMatchWords.Location(tentativeMatches(:,2),:); % % % % figure % % showMatchedFeatures(queryImage,bestImage,points1,points2,'montage') % % %% %Remove poor visual word assignments using estimateGeometricTransform function. %Keep the assignments that fit a valid geometric transform
[tform,inlierPoints1,inlierPoints2] = ... estimateGeometricTransform(points1,points2,'affine',... 'MaxNumTrials',20000);
%Rerank the search results by the percentage of inliers.
%Do this when the geometric verification procedure is applied to the top N search results.
%Those images with a higher percentage of inliers are more likely to be relevant.
percentageOfInliers = size(inlierPoints1,1)./size(points1,1);
% % figure % % showMatchedFeatures(queryImage,bestImage,inlierPoints1,... % % inlierPoints2,'montage')
%% %% %Apply the estimated transform. outputView = imref2d(size(bestImage)); Ir = imwarp(queryImage, tform, 'OutputView', outputView);
figure imshowpair(Ir,bestImage,'montage');
% example, you found match 3 match = bestImage; match1='100.txt';
if(match == 'string' name1); fprintf('file name is %s',name1 ) elseif(match == 120 ) fprintf('file name is %s',name2 ) elseif(match == 220 ) fprintf('file name is %s',name3 ) elseif(match == 240 ) fprintf('file name is %s',name4 ) end
the code is as follows.
NUR SHOLIHAH RAMLEE
NUR SHOLIHAH RAMLEE 2018년 5월 31일
hi florian, thank you for your suggestion yesterday. I already can give the name of the matched image as I want but, the answer will only printed inside the command prompt. as I changed the fprintf to msgbox, an error occurred as in the picture
%%%%%%%%%%%%%%
elseif(match == 2 ) fprintf('file name is %s',name2)
%%%%%%%%%%%%%%%%

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Geometric Transformation and Image Registration에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by