# How can I reject an image when the given template is not being matched (depending on any threshold value)?

1 Ansicht (letzte 30 Tage)
ARNABJIT CHOUDHURY am 22 Mai 2022
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 :
[Ir Ic] = size(I);
[Tr Tc] = size(T);
R = normxcorr2(T,I);
imshow(R);
[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 :
Other codes I have tried but with same result :
[Ir Ic] = size(I);
[Tr Tc] = size(T);
R = normxcorr2(T,I);
imshow(R);
[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;
% 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]);
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 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

yanqi liu am 23 Mai 2022
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;
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
##### 2 KommentareKeine anzeigenKeine ausblenden
ARNABJIT CHOUDHURY am 23 Mai 2022
Thank you very much Sir. It worked smoothly.
ARNABJIT CHOUDHURY am 23 Mai 2022
I am implementing a paper and got stuck in a step, may I ask you? @yanqi liu

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Explore and Edit Images with Image Viewer App finden Sie in Help Center und File Exchange

R2021a

### Community Treasure Hunt

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

Start Hunting!

Translated by