現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
How to give output name of image matched from template matching image processing method?
1 回表示 (過去 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
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
2018 年 5 月 18 日
i have tried to run your code but it gives an error;
Error: File: sentry.m Line: 201 Column: 14 Unexpected MATLAB expression.
--> line 201 column 14 refers to 'if'
NUR SHOLIHAH RAMLEE
2018 年 5 月 18 日
I have altered some things. it gave me the folder name of the image template database. what if I want the name of the image matched that is in the database.
As inside my database there are 4 images with different name such '100V', '120V', '220V' and '240V'. after doing the template matching, I want the output of matched image to give the image name from the database. how to do that? thank you
Florian Morsch
2018 年 5 月 18 日
The code i gave you is incomplete, you have to change the statement for the if() part to your condition.
How is this match saved? You run through a loop and compare the images? If so, just give the loop a run index, if it has run for the 3rd time select the 3rd image in the folder and display the name.
If you have done the matching and found a match, you should have some information on the matching image, which will help you to select it.
I dont know your code, so all i can give you is a general idea on how to achieve what you want. The code i gave above was also a general idea because of missing information.
NUR SHOLIHAH RAMLEE
2018 年 5 月 18 日
the process to find the matched image is done by using visual words from the image indexed.
I will try to find how to do it.
Florian Morsch
2018 年 5 月 18 日
So you use a bag of visual words? Then if you have found a match just link that match with the name of the database images.
Something like:
file1 = 'H:\user4\matlab\myfile1.txt'; % example file
[filepath1,name1,ext1] = fileparts(file1)
file2 = 'H:\user4\matlab\myfile2.txt'; % example file
[filepath2,name2,ext2] = fileparts(file2)
file3 = 'H:\user4\matlab\myfile3.txt'; % example file
[filepath3,name3,ext3] = fileparts(file3)
file4 = 'H:\user4\matlab\myfile4.txt'; % example file
[filepath4,name4,ext4] = fileparts(file4)
Your code for matching is here...
match = FoundMatch3 % example, you found match 3
if(match == match1)
fprintf = ('file name is %s',name1)
elseif(match == match2)
fprintf = ('file name is %s',name2)
elseif(match == match3)
fprintf = ('file name is %s',name3)
elseif(match == match4)
fprintf = ('file name is %s',name4)
end
Again, this is just a example which can help you to find a solution, not a fully working code
NUR SHOLIHAH RAMLEE
2018 年 5 月 21 日
thank you @florian morsch. I am wondering, at the if statement, should I declare match1? sorry I don't know much about this.
NUR SHOLIHAH RAMLEE
2018 年 5 月 30 日
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/189118/image.jpeg)
this is the filepath of the dataset image
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/189119/image.jpeg)
this is the filepath of the image template
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/189121/image.jpeg)
this is the final code to give the output.
I have done many ways to give the output of the image template that is matched with the query image. but I only seen a mistake. can you tell me where is my mistake? thank you very much
Florian Morsch
2018 年 5 月 30 日
編集済み: Florian Morsch
2018 年 5 月 30 日
Whats the value behind "bestImage" in your code? You now have
if(match == 100)
which will work, as long as bestImage also has the value 100. Does your variable "bestImage" has at any time the a value of 100 / 120 / 220 / 240? Because thats what you are testing, if it has a different value you wont get any result at all.
If you have done the matching and found the best match, compare the best match with your condition.
The code you posted should work ( cant tell really, because parts are missing) if you check for the right conditions.
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
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)
%%%%%%%%%%%%%%%%
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/189148/image.jpeg)
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Feature Detection and Extraction についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)