Matlab template matching normxcorr2 error - matlab

I have an homework that i need to identify playing cards with matlab.
I decide to use TemplateMatching using normxcorr2 but i keep having this error:
Error in Proje_Deneme2 (line 9)
R = normxcorr2(T,I);
And i dont know why.
here is my code:
I = imread("2clubs.jpg");
[Ir, Ic]=size(I);
figure(1)
imshow(I);
T = imread("2clubsTemp.jpg");
figure(2)
imshow(T);
[Tr, Tc]=size(T);
R = normxcorr2(T,I);
R = imcrop(R,[Tc Tr Ic Ir]);
[r, c, v] = fin(R==(max(max(R))));
RGB = insertShape(I, "rectangle", [c r Tc Tr], "LineWidth", 3);
figure(3)
imshow(RGB);
I am new to matlab and image processing and this is my first time using normxcorr2 so if i miss something very dump please excuse me.
Thank you for your answers.

I think i solve the problem my images are colored so when i perform grayscale on them it works.But i still don't know why colored images wont work.

Related

How to denoise a noisy image in matlab using Tihkonov regularization?

I am trying to write a code for denoising a noisy image in Matlab using Tikhonov regularization. For this purpose, I have written the following code:
refimg = im2double(mat2gray((imread('image1.png'))))*255; % original image
rng(0);
Noisyimg = refimg + randn(size(refimg))*20; % noisy image
%% Accelerated Gradient Method
x = cell(size(zeros(1,Nbiter)));
x{1} = Noisyimg; x{2} = Noisyimg;
y = zeros(size(refimg));
for iter = 3:Nbiter
y = x{iter-1} + ((iter-2)/(iter+1))*(x{iter-1}-x{iter-2});
[ux,uy] = grad(y,options);
GradFy = mu.*(y-Noisyimg)-K.*div(ux,uy,options);
xn = y - t*GradFy;
x{iter} = min(max(xn,0),255);
end
in which K, mu, and t are positive parameters that must be assinged. I have implemented the code with many different values for these parameters but I can not get a denoised image. I receive an out put image which is still very noisy. For this reason, I guess there is something wrong with the code. I really appreciate it if you could please help me to find the problem with my code. In the following, I have inserted the image that I have tried to denoise and also the energy functional that I have tried to minimize and the algorithm that I have used. Here, f is the noisy image and u is the image to be reconstructed. Many thanks.

Transform a photo according to a function [MATLAB]

I'm a beginner in matlab and I'm trying to transform a photo according to a function given in the code.
My aim is to see where some points of the R^2 plan go. For example, i'd like to transform :
But I can't figure this out.
I found some good conversations on this topic:
https://www.mathworks.com/matlabcentral/answers/81975-is-it-possible-to-pass-an-image-as-an-input-to-a-function-in-matlab
and good functions like :
https://www.mathworks.com/help/images/ref/imtransform.html
https://www.mathworks.com/help/images/ref/imwarp.html
but I don't understand what to do with that because I don't have a matrix but just like the function "1/z"...
The aim is to do something better than this :
How to plot the Wolfram Alpha grid? [MATLAB]
I've tried to add colors to the mesh graph but I ve not succed in doing so... I could only find how to change uniformly the colors, like setting all in green...
If you have another solution not using an image but constructing a
grid of a range of colors and then deforming it (like in the link) or
even better, instead of a grid, creating a whole plan with an uniform
distribution of the colors... it also fixes the problem!
Thank you !
You can use the surf function to plot a grid with colored patches. If you use the same code in my answer to your previous question, you could visualize the original grid with colors as follows:
C = X.^2 + Y.^2; %change this to any function you like to get different color patterns
surf(X,Y,C);
view([0, 90]); %view the mesh from above
Now, if you want to see how the transformed mesh looks like, you can do:
surf(U,V,C);
view([0, 90]);
where U and V are computed according to my previous answer.
Edit: Added sample code for transforming an image using geometricTransform2d and imwarp.
clear
clc
A = imread('peppers.png');
figure(1)
imshow(A)
t1 = geometricTransform2d(#ftransform);
Rin = imref2d(size(A),[-1 1],[-1 1]);
Rout = imref2d(size(A),[-5 5],[-5 5]);
B = imwarp(A, Rin, t1,'OutputView',Rout);
figure(2);
imshow(B)
function Xt = ftransform(X)
Z = complex(X(:,1),X(:,2));
Zt = 1./Z;
Xt(:,1) = real(Zt);
Xt(:,2) = imag(Zt);
end

Error when using imellipse to create mask

I try to create mask on a image, however I get the following error:
Undefined function 'createMask' for input arguments of type
'matlab.graphics.primitive.Image'.
I can't figure out what's wrong. Any help? Thanks in advance!
img = imread('pout.tif');
figure, h_im = imshow(img);
h = imellipse;
position = wait(h);
BW = createMask(position,h_im);
Solution
If I understand correctly, you are trying to get an ellipse input from the user and obtaining a mask of it.
There are two changes which needs to be done:
The wait function is not needed, since imellipse function waits until the user provide an ellipse.
First argument of createMask should be the ellipse h.
Updated code
img = imread('pout.tif');
figure, h_im = imshow(img);
h = imellipse;
BW = createMask(h,h_im);
Result

Image Morphing in Matlab

I want to use mathematical morphology function in MATLAB to find the boundary of can.png image. The input image is:
I want to get a boundary such as :
I tried to use different combination and parameter using strel, imerode, imdilate , But the result is not good enough (far from the expectation)
One of my trial code is:
a = imread ('can.png');
b = im2bw(a);
SE = strel('rectangle', [10 50 ]) ;
i2 = imdilate(b,SE);
figure(1); imshow(i2);
p = ones(4);
c = b - imerode(b,p);
figure(2); imshow(c);
The output is:
Can any body help me, how to create the expected image (black background with thin boundary for the can, please? Thank you very much.
Binarize on its morphological gradient, then do a dilation with an elementary SE, fill holes and finally obtain its border (trivial given the current image). This doesn't require any magical arbitrary threshold.
im = imread('can.png');
% turn image to BW
imb = (im < 220);
% fill the holes in the image
imf = imfill(imb, 'holes');
% find the edge
ed = edge(imf);
Resulting image:

Saving output of Warp Command in MATLAB

I am using the command WARP in image processing in MATLAB.
[x,y,z] = cylinder;
I = imread('testpat1.png');
warp(x,y,z,I);
Above is the example code for using WARP given in MATLAB. But I am not able to save the output of this command. If I do imwrite, just 1 X 1 matrix is saved.
Can anyone help me this ?
Thanks in advance
You should be able to use the following line of code to get the handle to the resulting surface object:
h = warp(x, y, z, I);
You can then access properties of that surface using get(h, 'property')
A list of the available properties is here
For example if you want to get the X coordinates you would do: Xcoords = get(h, 'XData');
hope that helps!
This is based on the comments below.
fig = figure, warp(x, y, z, I);
print(fig, '-r80','-dtiff','image2.tif')