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.
I want to rectify an image with perspectival distorsion. I have points of the corners and I have also have an algorithm that perfoms what I need but it executes really slow. It has 'imtransform' and 'maketform' functions which matlab has faster functions for these actions. So I tried to replace them but I couldn't make it right. Any helps will be appreciated.
Here is the Images to make this question clearer:
Input Image with known Coordinates(x,y):
and Desired Output:
This process executed with the interval of 2 seconds, I need to replace this process via new matlab functions but I couldn't make it.
Old algorihm was:
%X has the clockwise X coordinates %Y has the clockwise Y coordinates
A=zeros(8,8);
A(1,:)=[X(1),Y(1),1,0,0,0,-1*X(1)*x(1),-1*Y(1)*x(1)];
A(2,:)=[0,0,0,X(1),Y(1),1,-1*X(1)*y(1),-1*Y(1)*y(1)];
A(3,:)=[X(2),Y(2),1,0,0,0,-1*X(2)*x(2),-1*Y(2)*x(2)];
A(4,:)=[0,0,0,X(2),Y(2),1,-1*X(2)*y(2),-1*Y(2)*y(2)];
A(5,:)=[X(3),Y(3),1,0,0,0,-1*X(3)*x(3),-1*Y(3)*x(3)];
A(6,:)=[0,0,0,X(3),Y(3),1,-1*X(3)*y(3),-1*Y(3)*y(3)];
A(7,:)=[X(4),Y(4),1,0,0,0,-1*X(4)*x(4),-1*Y(4)*x(4)];
A(8,:)=[0,0,0,X(4),Y(4),1,-1*X(4)*y(4),-1*Y(4)*y(4)];
v=[x(1);y(1);x(2);y(2);x(3);y(3);x(4);y(4)];
u=A\v;
%transfer fonksiyonumuz
U=reshape([u;1],3,3)';
w=U*[X';Y';ones(1,4)];
w=w./(ones(3,1)*w(3,:));
T=maketform('projective',U');
%transform uygulayıp resmi düzleştiriyoruz
P2=imtransform(I,T,'XData',[1 n],'YData',[1 m]);
if it helps, here is how I generated "A" matrix and U matrix:
Out Link
using the builtin MATLAB functions (fitgeotrans, imref2d, and imwarp) the following code runs in 0.06 seconds on my laptop:
% read the image
im = imread('paper.jpg');
tic
% set the moving points := the original image control points
x = [1380;2183;1282;422];
y = [727;1166;2351;1678];
movingPoints = [x,y];
% set the fixed points := the desired image control points
xfix = [1;1000;1000;1];
yfix = [1;1;1000;1000];
fixedPoints = [xfix,yfix];
% generate geometric transform
tform = fitgeotrans(movingPoints,fixedPoints,'projective');
% generate reference object (full desired image size)
R = imref2d([1000 1000]);
% warp image
outputImage = imwarp(im,tform,'OutputView',R);
toc
% show image
imshow(outputImage);
Hello I have a work on this image:
My objective is to count all the sperm in this image .for this I'm thinking to detect just the lines so it make my work easy. because I am beginner, in this step I am completely lost there are any algorithms can help me to detect lines?? ( I have seen that there are hough transformation and scan line algorithm) I don't which algorithm can help me and if there are others
Here's a piece of code that might help you getting started.
By looking at the image it seems very difficult to label sperms by looking at straight lines and hence using Hough transform won't help a lot.
In the example below I focused on filtering the image and counting the number of blobs. The code is commented and should be easy to understand.
img = imread('d9S3Z.png');
figure, imshow(img)
% convert to binary image
[X,map] = rgb2ind(img,0.0);
img = ind2gray(X,map); % Convert indexed to grayscale
level = graythresh(img); % Compute an appropriate threshold
% or use your own, e.g. level = 0.46
img_bw = im2bw(img,level);% Convert grayscale to binary
% create mask to remove edge interference
mask = zeros(size(img_bw));
mask(2:end-2,2:end-2) = 1;
img_bw(mask<1) = 1;
%invert image
img_inv =1-img_bw;
% find blobs
img_blobs = bwmorph(img_inv,'majority',10);
figure, imshow(img_blobs);
% count blobs
CC = bwconncomp(img_blobs);
num_sperm = CC.NumObjects # sperm count
I have a enrgy equation such as
The algorithm is implement by Lankton and you can download the code and image at code. I want to base on that code to draw the energy function. Note that the F is computed in that code. The my goal energy figure such as
I tried to implement it by that code. But it is not correct answer
Energy=[];
%--main loop
for its = 1:max_its % Note: no automatic convergence test
%-- get the curve's narrow band
idx = find(phi <= 1.2 & phi >= -1.2)';
[y x] = ind2sub(size(phi),idx);
%-- get windows for localized statistics
xneg = x-rad; xpos = x+rad; %get subscripts for local regions
yneg = y-rad; ypos = y+rad;
xneg(xneg<1)=1; yneg(yneg<1)=1; %check bounds
xpos(xpos>dimx)=dimx; ypos(ypos>dimy)=dimy;
%-- re-initialize u,v,Ain,Aout
u=zeros(size(idx)); v=zeros(size(idx));
Ain=zeros(size(idx)); Aout=zeros(size(idx));
F_energy=zeros(size(idx));
%-- compute local stats
for i = 1:numel(idx) % for every point in the narrow band
img = I(yneg(i):ypos(i),xneg(i):xpos(i)); %sub image
P = phi(yneg(i):ypos(i),xneg(i):xpos(i)); %sub phi
upts = find(P<=0); %local interior
Ain(i) = length(upts)+eps;
u(i) = sum(img(upts))/Ain(i);
vpts = find(P>0); %local exterior
Aout(i) = length(vpts)+eps;
v(i) = sum(img(vpts))/Aout(i);
F_energy(i)=sum((img(upts)-u(i)).^2)+sum((img(vpts)-v(i)).^2); %% Compute the first term in (5) without integrate
end
%-- get image-based forces
F = -(u-v).*(2.*I(idx)-u-v);
% Compute the second term in (5)
u=phi<=0;
bw2=bwperim(u);
Length_phi=sum(sum(bw2));
Energy=[Energy (sum(F_energy(:))+alpha.*Length_phi)];
end
Maybe it is so difficult task because the energy function is so complex. However, all thing are implemented by above code, except enrgy term. Hope you can understand and help me draw the enrgy function. Thank in advance
This is my figure result. However, it is not similar the paper result. My result is minimal energy at near zero points. But the paper result is not. What is happen in my code.
Are you sure, that your parameters are similar to these used in original paper? I've observed that energy in each iteration depends on at least two things:
radius
initialization mask
The paper indeed confirms that relationship:
The radius of the ball selected by the B(x,y) function is an
important parameter to be considered when using localized energies.
and
One limitation of the proposed method is that it has a greater
sensitivity to initialization than global region-based methods.
Following picture shows what I've managed to achieve using your code:
Please notice, that in original paper the X-axis unit is second. The code gives us energy in each iteration. Without knowing the duration of one iteration in the original computations (described in the paper) we can't really compare these plots. However, mine is much more similar to the original one.
Here is code for the initialization mask (corresponding to the plot):
I = imread('Mushroom.png'); %-- load the image
m = false(size(I,1),size(I,2)); %-- create initial mask
m(60:100,15:80) = true; %-- initial mask coordinates
Max iterations: 400
Radius: 20
Hope I've helped.
I am new to optical flow and computer vision in general and I started to work with a simple demo example from Matlab.
The objective of it is to use a video and plot the motion vectors onto the screen. I am using the following code:
%% initialization
close all
clear all
% Create reader
reader = vision.VideoFileReader;
reader.Filename = 'viptraffic.avi';
% Create viewer
viewer = vision.DeployableVideoPlayer;
%%viewer.FrameRate = 10;
%Create Optical Flow
optical = vision.OpticalFlow; %how pixels are moving from one frame to the next
optical.OutputValue = 'Horizontal and vertical components in complex form'; %will allow us to draw a vector
%%%on the vision so that we see how the pixels are moving from one frame to the next
%%We pass the horizontal and vertical components to the shape inserter
%%below
% Display vector fields
shapes = vision.ShapeInserter;
shapes.Shape = 'Lines';
shapes.BorderColor = 'white';
R = 1:4:120;%%downsample the optical flow field
C = 1:4:160;%%downsample the optical flow field
[Cv, Rv] = meshgrid (C, R); %%% display a grid on the image and take every fourth value
Rv = Rv(:)';
Cv = Cv(:)';
%% Execution
reset(reader)
%Set up for stream
while ~isDone(reader)
I = step(reader);
of = step(optical,rgb2gray(I));
size(of)
ofd = of(R,C);
size(ofd)
H = imag(ofd)*20;
V = real(ofd)*20;
%Draw lines on top of image
lines = [Rv;Cv; Rv+H(:)'; Cv+V(:)']; %%start and a finish , start+movement, end+movement
% lines = [Cv;Rv;Cv;Rv];
Ishp = step(shapes,I,lines);
step(viewer,Ishp);
end
release(viewer);
I do not why the vector lines are not plotting properly.
Can anyone help me?
Thanks
PS: here is the result:
Try using
lines = [Rv(:); Cv(:); Rv(:)+H(:); Cv(:)+V(:)];
instead of
lines = [Rv;Cv; Rv+H(:)'; Cv+V(:)'];
Better yet, if you have a recent version of Matlab, try using the insertShape function instead of vision.ShapeInserter.
Edit:
If you have a recent version of the Computer Vision System Toolbox, try the new optical flow functions: opticalFlowHS, opticalFlowLK, opticalFlowLKDoG, and opticalFlowFarneback.