In this code I extract the outline of an object. Is there a way to convert it to a fill holes binary image?
Code:
clc;
clear;
close all;
url='https://vemaps.com/uploads/img/it-07.png';
I = imread(url);
imshow(I);
hold on;
BW = imbinarize(I);
[B,L] = bwboundaries(BW,'noholes');
k=1;
stat = regionprops(I,'Centroid');
b = B{k};
c = stat(k).Centroid;
yBoundary = b(:,2);
xBoundary = b(:,1);
cy = c(:,2);
cx = c(:,1);
plot(yBoundary, xBoundary, 'g', 'linewidth', 2);
This is the image outcome I look for:
Related
In the script below I outline an object but in the final plot I have noticed that the left bottom of the plot is not 0,0. How can I fix it in the plot and also the X/Ycoord data?
Code:
clc
clear
close all
Iorig = imread('E:/drop.jpg');
I = rgb2gray(Iorig);
axis on
imshow(I)
T = adaptthresh(I, 0.65);
BW = imbinarize(I,T);
BW2 = imcomplement(BW);
BW2 = imfill(BW2, 'holes');
axis on
hold on;
[B,L] = bwboundaries(BW2,'noholes');
boundary = B{1};
Xcoord = boundary(:,2);
Ycoord = boundary(:,1);
plot(Xcoord, Ycoord, 'g', 'LineWidth', 2)
You can flip they y-axis using
set(gca,'YDir','normal')
I found this script below in https://blogs.mathworks.com/steve/2010/07/30/visualizing-regionprops-ellipse-measurements/
I wish to plot also the MajorAxisLength and the MinorAxisLength, how can I do it?
Script:
url = 'https://blogs.mathworks.com/images/steve/2010/rice_binary.png';
bw = imread(url);
imshow(bw)
s = regionprops(bw, 'Orientation', 'MajorAxisLength', ...
'MinorAxisLength', 'Eccentricity', 'Centroid');
imshow(bw)
hold on
phi = linspace(0,2*pi,50);
cosphi = cos(phi);
sinphi = sin(phi);
for k = 1:length(s)
xbar = s(k).Centroid(1);
ybar = s(k).Centroid(2);
a = s(k).MajorAxisLength/2;
b = s(k).MinorAxisLength/2;
theta = pi*s(k).Orientation/180;
R = [ cos(theta) sin(theta)
-sin(theta) cos(theta)];
xy = [a*cosphi; b*sinphi];
xy = R*xy;
x = xy(1,:) + xbar;
y = xy(2,:) + ybar;
plot(x,y,'r','LineWidth',2);
end
hold off
You have a lot of unnecessary code in what you posted. I don't think you actually attempted to adjust it for what you say you want to plot. Given what you said you are trying to do in the comments, I think this is the code you want in order to plot the Major Axis Length vs. Minor Axis Length
url = 'https://blogs.mathworks.com/images/steve/2010/rice_binary.png';
bw = imread(url);
s = regionprops(bw, 'Orientation', 'MajorAxisLength', ...
'MinorAxisLength', 'Eccentricity', 'Centroid'); %Get region props
Major=zeros(size(s));
Minor=zeros(size(s));
for k = 1:length(s)
Major(k)= s(k).MajorAxisLength; %get your y values
Minor(k)= s(k).MinorAxisLength; %get your x values
end
figure %create a new figure because you don't want to plot on top of the image
plot(Minor, Major, 'o') %plot
xlabel('Minor Axis Length')
ylabel('Major Axis Length')
I have simulated some random walkers. I used
plot(xb,yb,'b--o')
to show particles in each step. I saw a code in below link with beautiful particles with tail which moves in a blur way. Is there a way which I could my random walkers the same as the walkers in the link in mat lab? Could anyone tell me which should I use instead of the plot function I used?
beautiful particles
The code I tried:
clear all
close all
lbox=20;
%random fluctuation
eta = (2.*pi).*.1;
vs=0.02;
n=200;
birdl=[1:n];
axis([0 lbox 0 lbox])
axis('square')
hold on
xb=rand(n,1).*lbox; %first possition
yb=rand(n,1).*lbox; %first possition
vxb = 1;
vyb = 1;
for steps=1:5000;
xb = xb + vxb;
yb = yb+ vyb;
for bird1 = 1:n;
%periodic boundary condition
if(xb(bird1)<0);xb(bird1)=xb(bird1)+lbox; end
if (yb(bird1)<0);yb(bird1)=yb(bird1)+lbox;end
if (xb(bird1)>lbox);xb(bird1)=xb(bird1)-lbox;end
if (yb(bird1)>lbox);yb(bird1)=yb(bird1)-lbox;end
end
ang=eta.*(rand(n,1)-0.5);
vxb = vs.*cos(ang);
vyb = vs.*sin(ang);
cla
set(gcf,'doublebuffer','on')
plot(xb,yb,'.b')
%quiver(xb,yb,vxb,vyb,'b')
drawnow
end
If you want to create a sort of trail of where the particles have recently been, you can store the previous nStore plots and change their color so that older plots gradually darken and fade to black (alpha transparency like in your sample isn't possible with line objects in MATLAB). Here's a reworking of your code (with a few other improvements, like replacing the inner boundary condition loop with indexing):
clear all
close all
lbox = 20;
%random fluctuation
eta = (2.*pi).*1;
vs = 0.05;
n = 200;
set(gcf, 'doublebuffer', 'on', 'Color', 'k');
set(gca, 'Visible', 'off');
axis([0 lbox 0 lbox])
axis('square')
hold on
xb = rand(n, 1).*lbox; %first possition
yb = rand(n, 1).*lbox; %first possition
vxb = 1;
vyb = 1;
hList = [];
nStore = 30;
cMap = [zeros(nStore+1, 1) linspace(1, 0, nStore+1).' zeros(nStore+1, 1)];
for steps = 1:200
xb = xb + vxb;
yb = yb + vyb;
%periodic boundary condition
index = (xb < 0);
xb(index) = xb(index) + lbox;
index = (yb < 0);
yb(index) = yb(index) + lbox;
index = (xb > lbox);
xb(index) = xb(index) - lbox;
index = (yb > lbox);
yb(index) = yb(index) - lbox;
ang = eta.*(rand(n,1)-0.5);
vxb = vs.*cos(ang);
vyb = vs.*sin(ang);
h = plot(xb, yb, '.g', 'MarkerSize', 12);
if (numel(hList) == nStore)
delete(hList(nStore));
hList = [h hList(1:end-1)];
else
hList = [h hList];
end
set(hList, {'Color'}, num2cell(cMap(1:numel(hList), :), 2));
drawnow
end
And here's an animation:
I created the animation by adding the following code:
% After the drawnow...
frame = getframe(gca);
im = frame2im(frame);
imind(:, :, 1, steps) = uint8(rgb2ind(im, cMap, 'nodither'));
% After the loop...
imwrite(imind(:, :, 1, 1:2:end), cMap, 'randwalk.gif', ...
'Loopcount', Inf, 'DelayTime', 0);
I had to trim out some frames to make the gif smaller.
My shot at "nicer" random walk:
clear all
close all
lbox=20;
figure('Color',[0 0 0])
%random fluctuation
eta = (2.*pi).*1;
vs=0.02;
n=300;
birdl=[1:n];
axis([0 lbox 0 lbox])
axis('square')
hold on
xb=rand(n,1).*lbox; %first possition
yb=rand(n,1).*lbox; %first possition
vxb = 1;
vyb = 1;
for steps=1:5000;
xb = xb + vxb;
yb = yb+ vyb;
for bird1 = 1:n;
%periodic boundary condition
if (xb(bird1)<0);xb(bird1)=xb(bird1)+lbox; end
if (yb(bird1)<0);yb(bird1)=yb(bird1)+lbox;end
if (xb(bird1)>lbox);xb(bird1)=xb(bird1)-lbox;end
if (yb(bird1)>lbox);yb(bird1)=yb(bird1)-lbox;end
end
ang=eta.*(rand(n,1)-0.5);
vxb = vs.*cos(ang);
vyb = vs.*sin(ang);
cla
set(gca,'Color',[0 0 0]);
set(gcf,'doublebuffer','on')
set(gca,'YTick',[]);
set(gca,'XTick',[]);
plot(xb,yb,'.g','markersize',10)
% this should draw lines, but its slow and not as neat as a web app
% plot([xb xb-vxb*5]',[yb yb-vyb*5]','g')
drawnow
end
I have to binarize image so I have twice as many white pixels as black pixels.
Someone gave me answer:
binarized = im2bw(image, 0.28)
and I'm not sure how do this person know that level 0.28 gives twice as many white as black?
In this code, I had to use gamma correction, use imhist, binarize. My code:
close all;
clear all;
clc;
img = imread('cameraman.tif');
img = double(img)/255;
coeff = 0.6;
gamma = img.^coeff;
figure;
subplot(121); imshow(img); title('Oryginalny');
subplot(122); imshow(gamma); title('Po korekcji gamma');
equalized = histeq(gamma,32);
figure;
subplot(221); imshow(gamma); title('Po korekcji gamma');
subplot(222); imshow(equalized); title('Wyrównany');
subplot(223); imhist(gamma); title('Po korekcji gamma');
subplot(224); imhist(equalized); title('Wyrównany');
SE = strel('disk', 3);
eroded = imerode(equalized,SE);
opening = imdilate(eroded,SE);
figure;
subplot(121); imshow(equalized); title('Wyrównany');
subplot(122); imshow(opening); title('Otwarcie');
binarized = im2bw(opening, 0.28);
figure;
imshow(binarized); title('Po binaryzacji');
w = binarized == 1;
b = binarized == 0;
biale = sum(w(:));
czarne = sum(b(:));
I have a video when in some place the video rotated ... I don't know the angle and to what Direction it move. I tried to use:
function [ output_args ] = aaa( filename )
hVideoSrc = vision.VideoFileReader(filename, 'ImageColorSpace', 'Intensity');
imgA = step(hVideoSrc); % Read first frame into imgA
imgB = step(hVideoSrc); % Read second frame into imgB
figure; imshowpair(imgA, imgB, 'montage');
title(['Frame A', repmat(' ',[1 70]), 'Frame B']);
figure; imshowpair(imgA,imgB,'ColorChannels','red-cyan');
title('Color composite (frame A = red, frame B = cyan)');
ptThresh = 0.1;
pointsA = detectFASTFeatures(imgA, 'MinContrast', ptThresh);
pointsB = detectFASTFeatures(imgB, 'MinContrast', ptThresh);
% Display corners found in images A and B.
figure; imshow(imgA); hold on;
plot(pointsA);
title('Corners in A');
figure; imshow(imgB); hold on;
plot(pointsB);
title('Corners in B');
% Extract FREAK descriptors for the corners
[featuresA, pointsA] = extractFeatures(imgA, pointsA);
[featuresB, pointsB] = extractFeatures(imgB, pointsB);
indexPairs = matchFeatures(featuresA, featuresB);
pointsA = pointsA(indexPairs(:, 1), :);
pointsB = pointsB(indexPairs(:, 2), :);
figure; showMatchedFeatures(imgA, imgB, pointsA, pointsB);
legend('A', 'B');
[tform, pointsBm, pointsAm] = estimateGeometricTransform(...
pointsB, pointsA, 'affine');
imgBp = imwarp(imgB, tform, 'OutputView', imref2d(size(imgB)));
pointsBmp = transformPointsForward(tform, pointsBm.Location);
figure;
showMatchedFeatures(imgA, imgBp, pointsAm, pointsBmp);
legend('A', 'B');
% Extract scale and rotation part sub-matrix.
H = tform.T;
R = H(1:2,1:2);
% Compute theta from mean of two possible arctangents
theta = mean([atan2(R(2),R(1)) atan2(-R(3),R(4))]);
% Compute scale from mean of two stable mean calculations
scale = mean(R([1 4])/cos(theta));
% Translation remains the same:
translation = H(3, 1:2);
% Reconstitute new s-R-t transform:
HsRt = [[scale*[cos(theta) -sin(theta); sin(theta) cos(theta)]; ...
translation], [0 0 1]'];
tformsRT = affine2d(HsRt);
imgBold = imwarp(imgB, tform, 'OutputView', imref2d(size(imgB)));
imgBsRt = imwarp(imgB, tformsRT, 'OutputView', imref2d(size(imgB)));
figure(2), clf;
imshowpair(imgBold,imgBsRt,'ColorChannels','red-cyan'), axis image;
title('Color composite of affine and s-R-t transform outputs');
% Reset the video source to the beginning of the file.
reset(hVideoSrc);
hVPlayer = vision.VideoPlayer; % Create video viewer
% Process all frames in the video
movMean = step(hVideoSrc);
imgB = movMean;
imgBp = imgB;
correctedMean = imgBp;
ii = 2;
Hcumulative = eye(3);
while ~isDone(hVideoSrc) && ii < 10
% Read in new frame
imgA = imgB; % z^-1
imgAp = imgBp; % z^-1
imgB = step(hVideoSrc);
movMean = movMean + imgB;
% Estimate transform from frame A to frame B, and fit as an s-R-t
H = cvexEstStabilizationTform(imgA,imgB);
HsRt = cvexTformToSRT(H);
Hcumulative = HsRt * Hcumulative;
imgBp = imwarp(imgB,affine2d(Hcumulative),'OutputView',imref2d(size(imgB)));
% Display as color composite with last corrected frame
step(hVPlayer, imfuse(imgAp,imgBp,'ColorChannels','red-cyan'));
correctedMean = correctedMean + imgBp;
ii = ii+1;
end
correctedMean = correctedMean/(ii-2);
movMean = movMean/(ii-2);
% Here you call the release method on the objects to close any open files
% and release memory.
release(hVideoSrc);
release(hVPlayer);
figure; imshowpair(movMean, correctedMean, 'montage');
title(['Raw input mean', repmat(' ',[1 50]), 'Corrected sequence mean']);
end
Code from here
http://www.mathworks.com/help/vision/examples/video-stabilization-using-point-feature-matching.html,
but the MatLab doesn't recognize the function detectFASTFeatures
Someone can help me ?
Maybe someone have other function that find this points.
It seems to be a function in the computer vision toolbox that only comes with MATLAB r2014a:
http://www.mathworks.com/help/vision/ref/detectfastfeatures.html
If you have an older version of the MATLAB with Computer Vision System Toolbox, you can use the vision.CornerDetector object.