How can I reject an image when the given template is not being matched (depending on any threshold value)?
2 次查看(过去 30 天)
显示 更早的评论
I have a set of 198 different currencies of different nations. I want to segment them on the basis of some templates. Suppose for the Indian currency I am using the Ashok Stambh template to segment the banknote as an Indian note. But if I put a Bangladesh currency, and check it against the Ashok Stambh template, it is giving me certain region where the template is present, where as in reality the template is not present there. I want to discard the Bangladesh Currency to be said as Indian Currency on certain parameter/threshold. How can I achieve that ?
I am attaching one Indian Banknote as well as a Bangladesh Banlnote and the Indian Ashok Stambh Template. I am also attaching the codes I tried to use but failed to achieve the purpose. I am also attaching the results which I am getting.
Code 1 :
I = rgb2gray(imread('./new3/Indian 10₹ Front.jpg'));
[Ir Ic] = size(I);
T = rgb2gray(imread('India_template.jpg'));
[Tr Tc] = size(T);
R = normxcorr2(T,I);
imshow(R);
figure,surf(R),shading flat
[y x] = find(R>=1*max(R(:)));
ynew = y - size(T,1);
xnew = x - size(T,2);
figure, imshow(I);
for i = 1:length(xnew)
imrect(gca,[xnew(i)+1, ynew(i)+1, size(T,2), size(T,1)]);
hold on;
end
Result with Indian Banknote :
Result with Bangladesh Banknote :
Other codes I have tried but with same result :
I = rgb2gray(imread('./new3/Bangladesh 20 Taka Front.jpg'));
[Ir Ic] = size(I);
T = rgb2gray(imread('India_template.jpg'));
[Tr Tc] = size(T);
R = normxcorr2(T,I);
imshow(R);
figure,surf(R),shading flat
[y x] = find(R>=1*max(R(:)));
ynew = y - size(T,1);
xnew = x - size(T,2);
figure, imshow(I);
R = imcrop(R, [Tc Tr Ic Ir]);
[r c v] = find(R==(max(max(R))));
RGB = insertShape(I, 'rectangle', [c r Tc Tr], 'LineWidth', 3);
imshow(RGB);
Another approach :
% Demo to use normxcorr2 to find a template (a white onion)
% in a larger image (pile of vegetables)
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;
rgbImage = imread('./new3/Indian 10₹ Front.jpg');
% 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 = imread('India_template.jpg');
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(:,:,3), rgbImage(:,:,3));
subplot(2, 2, 3);
imshow(correlationOutput, []);
title('Normalized Cross 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) 85 125],...
'edgecolor','g','linewidth',2);
title('Template Image Found in Original Image', 'FontSize', fontSize);
0 个评论
采纳的回答
yanqi liu
2022-5-23
yes,sir,may be set some thresh to make accept or reject rules,such as
% Demo to use normxcorr2 to find a template (a white onion)
% in a larger image (pile of vegetables)
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;
rgbImages{1} = imread('https://ww2.mathworks.cn/matlabcentral/answers/uploaded_files/1007020/Indian%2010%E2%82%B9%20Front.jpg');
rgbImages{2} = imread('https://ww2.mathworks.cn/matlabcentral/answers/uploaded_files/1007015/Bangladesh%2020%20Taka%20Front.jpg');
smallSubImage = imread('https://ww2.mathworks.cn/matlabcentral/answers/uploaded_files/1007025/India_template.jpg');
for ik = 1 : length(rgbImages)
rgbImage = rgbImages{ik};
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
figure;
% 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]);
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(double(rgb2gray(smallSubImage)), double(rgb2gray(rgbImage)));
subplot(2, 2, 3);
imshow(correlationOutput, []);
title('Normalized Cross Correlation Output', 'FontSize', fontSize);
[maxCorrValue, maxIndex] = max(abs(correlationOutput(:)));
if maxCorrValue > 0.6
[ypeak, xpeak] = ind2sub(size(correlationOutput),maxIndex(1));
corr_offset = [(xpeak-size(smallSubImage,2)) (ypeak-size(smallSubImage,1))];
end
subplot(2, 2, 4);
imshow(rgbImage);
if maxCorrValue > 0.6
hold on;
rectangle('position',[corr_offset(1) corr_offset(2) size(smallSubImage,2) size(smallSubImage,1)],...
'edgecolor','g','linewidth',2);
title('Template Image Found in Original Image', 'FontSize', fontSize);
else
title('Reject template match');
end
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Processing and Computer Vision 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!