I am getting the following error message in Matlab "Unrecognized function or variable 'adaptivethreshold'" when every time I want to use Adaptive Threshold function
clear;close all;
im1=imread('page.png');
im2=imread('tshape.png');
bwim1=adaptivethreshold(im1,11,0.03,0);
bwim2=adaptivethreshold(im2,15,0.02,0);
subplot(2,2,1);
imshow(im1);
subplot(2,2,2);
imshow(bwim1);
subplot(2,2,3);
imshow(im2);
subplot(2,2,4);
imshow(bwim2);
those coding from
https://www.mathworks.com/matlabcentral/fileexchange/8647-local-adaptive-thresholding
You should open MATLAB editor inside a folder containing all files: adaptivethreshold.m, page.png, testadaptivethreshold.m, tshape.png and run your script testadaptivethreshold.
Or, you can copy-paste the function definition below the script as follows and make sure also that the images are in the same folder or MATLAB search path.
clear;close all;
im1=imread('page.png');
im2=imread('tshape.png');
bwim1=adaptivethreshold(im1,11,0.03,0);
bwim2=adaptivethreshold(im2,15,0.02,0);
subplot(2,2,1);
imshow(im1);
subplot(2,2,2);
imshow(bwim1);
subplot(2,2,3);
imshow(im2);
subplot(2,2,4);
imshow(bwim2);
function bw=adaptivethreshold(IM,ws,C,tm)
%ADAPTIVETHRESHOLD An adaptive thresholding algorithm that seperates the
%foreground from the background with nonuniform illumination.
% bw=adaptivethreshold(IM,ws,C) outputs a binary image bw with the local
% threshold mean-C or median-C to the image IM.
% ws is the local window size.
% tm is 0 or 1, a switch between mean and median. tm=0 mean(default); tm=1 median.
%
% Contributed by Guanglei Xiong (xgl99#mails.tsinghua.edu.cn)
% at Tsinghua University, Beijing, China.
%
% For more information, please see
% http://homepages.inf.ed.ac.uk/rbf/HIPR2/adpthrsh.htm
if (nargin<3)
error('You must provide the image IM, the window size ws, and C.');
elseif (nargin==3)
tm=0;
elseif (tm~=0 && tm~=1)
error('tm must be 0 or 1.');
end
IM=mat2gray(IM);
if tm==0
mIM=imfilter(IM,fspecial('average',ws),'replicate');
else
mIM=medfilt2(IM,[ws ws]);
end
sIM=mIM-IM-C;
bw=imbinarize(sIM,0);
bw=imcomplement(bw);
end
And here is how it shows the output:
Related
Using
autoArrangeFigures(0,0,2); % (0,0,x) where x is monitor ID
one can choose where all figures are to be placed. I would like, however, to control which figures go to which monitor.
MWE attempt:
close all; clear all; clc
% make 10 figures
for i=1:10
figure()
end
autoArrangeFigures(0,0,2); % (0,0,x) where x is monitor ID
pause(2)
% make 10 figures
for i=1:10
figure()
end
autoArrangeFigures(0,0,1); % (0,0,x) where x is monitor ID
This only redirects all 20 figures to new monitor position. It doesn't keep the 10 figure positions from the first call of autoArrangeFigures(0,0,2) in monitor 2, but redirects all 10+10 figures to monitor 1 via autoArrangeFigures(0,0,1).
How to fix this?
autoArrangeFigures can be found at:
https://se.mathworks.com/matlabcentral/fileexchange/48480-automatically-arrange-figure-windows
One thing that you could do is modify the autoArrangeFigures function to take an additional (optional) argument figHandles.
Modify the first line to:
function autoArrangeFigures(NH, NW, monitor_id, figHandle)
To make this argument optional, you can change line 39
figHandle = sortFigureHandles(findobj('Type','figure'));
into
if ~exist('figHandle', 'var') || isempty(figHandle)
figHandle = sortFigureHandles(findobj('Type','figure'));
else
figHandle = figHandle(:); % make row vector
end
So then your MWE would look like:
clear fig
% make 10 figures
for i=1:10
fig(i) = figure();
end
autoArrangeFigures(0,0,2, fig); % (0,0,x) where x is monitor ID
% make 10 figures
for i=1:10
fig(10+i) = figure();
end
autoArrangeFigures(0,0,1,fig(11:20)); % (0,0,x) where x is monitor ID
Personally, I am a fan of distFig from the Matlab FEX, which allows what you want out of the box.
Example:
for i = 1:20
fig(i) = figure();
end
distFig('screen', 'Secondary', 'only', 1:10) % place only fig 1:10 on the secondary screen
distFig('screen', 'Primary', 'only', 11:20) % place only fig 11:20 on the primary screen
I am trying to write a program that can detect the path which I draw on paper and give me the coordinates. I want to guide a welding torch with use of these coordinates.
With use of edge() I could detect the edges of the path, but I have trouble detecting the middle of the path. I tried to interpolate the edges coordinates with a for loop but I can't.
This is my code:
%% defining & Addressing an Image
img0=imread('C:\Users\Sohrab\Downloads\Project\photo_2018-11-02_22-07-15.jpg');
img0=rgb2gray(img0);
img1=imresize(img0,0.6);
img1=medfilt2(img1,[10,10]);
%% Applying Edge Detection
img2=edge(img1,'sobel');
img3=edge(img1,'canny');
imgI=img3;
[Row,Col]=size(imgI);
%% Interpolation
for i = 1:1:Row
k=0;
ONPixel=0;
for j = 1:1:Col
if(imgI(i,j)==1)
ONPixel=ONPixel+j;
k=k+1;
end
end
imgI(i,:)=0;
if(k >= 2)
imgI(i,floor(ONPixel/k))=1;
end
end
%% showing Results
figure;
subplot(2,2,1)
imshow(img1)
title('ORGINAL IMAGE')
subplot(2,2,2)
imshow(img2)
title('SOBEl METHOD')
subplot(2,2,3)
imshow(img3)
title('CANNY Method')
subplot(2,2,4)
imshow(imgI)
title('Interpolation')
This is the result of interpolation:
the result is incorrect.
This is the image I used for calculation:
Here is the input image 5.png:
Here is my code:
clear all; close all; clc;
%Input Image
A = imread('C:\Users\efu\Desktop\5.png');
% figure, imshow(A);
C=medfilt2(A,[3 5]);
% figure,imshow(C);
D=imfill(C);
% figure,imshow(D);
%Image obtained using MATLAB function 'edge'
E=edge(D,'canny',[0.01 .02],3);
figure, imshow(E); title('Image obtained using MATLAB function');
image=E;
img=im2bw(image);
% imshow(img)
se = strel('line',3,0);
zz = imerode(img,se);
figure, imshow(zz);
Output after canny edge detection:
After Eroding:
Here the problem is that after eroding all horizontal edges are broken, but I don't want that. I want to extract all horizontal lines without breaking, besides want to remove all vertical and diagonal lines.
Please someone modify the code.
It's a hack but it works. A short note, I would advise against using clear all. It has some negative effects such as clearing references to your mex functions. I'm not really sure what that means but there seems to be no real use for it unless you want to clear global variables.
Basically what I did here was threefold. I added a gaussian filter to give a bit more tolerance, increased the erosion rate of strel, and searched across angles to give a bit of angle tolerance. In the end, you have a bit more than you started with on the horizontal part but it does clear the image up a lot better. If you wanted you could just add up the zz matrices and threshold it to get a new binary image which could be a bit closer to your original. Really enjoyed the question by the way and made me look forward to image processing in the fall.
clear; close all;
%Input Image
A = imread('5.png');
% figure, imshow(A);
C=medfilt2(A,[3 5]);
% figure,imshow(C);
D=imfill(C);
% figure,imshow(D);
%Image obtained using MATLAB function 'edge'
E=edge(D,'canny',[0.01 .02],4);
figure, imshow(E); title('Image obtained using MATLAB function');
image=E;
img=double(image);
img = imgaussfilt(img,.5);
% imshow(img)
zz_out = zeros(size(img));
% se = strel('line',3,-90);
% zz = imerode(img,se);
% se2 = strel('line',3,0);
% zz2 = imerode(img,se2);
for ii = -5:.1:5
se = strel('line',20,ii);
zz = imerode(img,se);
zz_out = or(zz,zz_out);
end
% zz_out = img-zz;
figure, imshow(zz_out);
I am trying to compile a Vibrato sound effect code that I found on the internet. The code has a function call wavread, in this function matlab is showing error, I've search another codes to do this and every code use this function to open de wav file, someone what is happening? The code below:
Vibrato Script:
clear all;
close all;
clc;
infile = 'musica.wav';
% read in wav sample
[ x, Fs, N ] = wavread(infile);
%set Parameters for vibrato
% Change these to experiment with vibrato
Modfreq = 10; %10 Khz
Width = 0.0008; % 0.8 Milliseconds
% Do vibrato
yvib = vibrato(x, Fs, Modfreq, Width);
% write output wav files
wavwrite(yvib, Fs, 'out_vibrato.wav');
% plot the original and equalised waveforms
figure(1)
hold on
plot(x(1:500),'r');
plot(yvib(1:500),'b');
title('Vibrato First 500 Samples');
Vibrato Function:
% Vibrato
function y=vibrato(x,SAMPLERATE,Modfreq,Width)
ya_alt=0;
Delay=Width; % basic delay of input sample in sec
DELAY=round(Delay*SAMPLERATE); % basic delay in # samples
WIDTH=round(Width*SAMPLERATE); % modulation width in # samples
if WIDTH>DELAY
error('delay greater than basic delay !!!');
return;
end
MODFREQ=Modfreq/SAMPLERATE; % modulation frequency in # samples
LEN=length(x); % # of samples in WAV-file
L=2+DELAY+WIDTH*2; % length of the entire delay
Delayline=zeros(L,1); % memory allocation for delay
y=zeros(size(x)); % memory allocation for output vector
for n=1:(LEN-1)
M=MODFREQ;
MOD=sin(M*2*pi*n);
ZEIGER=1+DELAY+WIDTH*MOD;
i=floor(ZEIGER);
frac=ZEIGER-i;
Delayline=[x(n);Delayline(1:L-1)];
%---Linear Interpolation-----------------------------
y(n,1)=Delayline(i+1)*frac+Delayline(i)*(1-frac);
%---Allpass Interpolation------------------------------
%y(n,1)=(Delayline(i+1)+(1-frac)*Delayline(i)-(1-frac)*ya_alt);
%ya_alt=ya(n,1);
end
The error that appears:
Undefined function or variable 'wavread'.
In the line:
[ x, Fs, N ] = wavread(infile);
The function wavread is not supported since Matlab R2015b.
This functionality has been replaced with audioread, and the prototype has slightly changed.
Please replace the faulty line with
% read in wav sample
[x, Fs] = audioread(infile);
Then the situation is the same with wavwrite that has been replaced with audiowrite.
You should change it to
% write output wav files
audiowrite('out_vibrato.wav', yvib, Fs);
I create a video of my plot series as below:
clc;
clear;
close all;
r0=1;
theta=0:0.01:2*pi;
a=0.2;
psi=0;
writerObj = VideoWriter('shapes.mp4'); % Name it.
writerObj.FrameRate = 1; % How many frames per second.
open(writerObj);
for k=1:3
pause(1);
r=r0+a*sin(k*theta+psi)+a*sin((k+1)*theta+psi)+a*sin((k+2)*theta+psi)+a*sin((k+3)*theta+0.8*pi);
figure();
x=r.*cos(theta);
y=r.*sin(theta);
plot(x,y);
axis([-2 2 -2 2]);
% axis equal
grid on
%%%%%%%%% Finding Area %%%%%%%%%%%%%
A = polyarea(x,y);
title({'line1',['Area = ' num2str(A)]})
% title(['Area = ' num2str(A)]);
set(gcf,'renderer','zbuffer');
view(2)
frame = getframe(gcf);
writeVideo(writerObj, frame);
close all
end
close(writerObj); % Saves the movie.
But when you watch the video it has poor quality (by poor quality I mean, the quality when you export matlab plots in a jpeg format for instance).
For matlab plots people usually export in .eps format so that they save high quality plots and use them in their report or presentations.
What is an equivalent way (.eps in plot saving) for video making in matlab to preserve a good quality.