how to perform character segmentation in Matlab [closed] - matlab

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have license plate image and I want to cut the numbers one by one.
any one have simple idea how to perform it?
after searching the web I found a way by doing the operation of horizontal and vertical smearing, but I really don't know what does it mean.
any explanation would help
Thanks in advance.

regionprops might work for you. If you take this sample license plate.
You could use a little script like this to cut out objects. Sorry, I just typed it together really quickly, but it gives you an idea.
clear all;
close all;
I = imread('plate.jpg');
BW = im2bw(I, 0.9);
BW = ~BW;
stats = regionprops(BW);
for index=1:length(stats)
if stats(index).Area > 200 && stats(index).BoundingBox(3)*stats(index).BoundingBox(4) < 30000
x = ceil(stats(index).BoundingBox(1))
y= ceil(stats(index).BoundingBox(2))
widthX = floor(stats(index).BoundingBox(3)-1)
widthY = floor(stats(index).BoundingBox(4)-1)
subimage(index) = {BW(y:y+widthY,x:x+widthX,:)};
figure, imshow(subimage{index})
end
end
This will output images like
and this
You still have to decide if it really is a letter. Be careful, the script will output a lot of images (about 30 or 40)

you can try this code(it's not mine)
% This is a program for extracting objects from an image. Written for vehicle number plate segmentation and extraction
% Authors : Jeny Rajan, Chandrashekar P S
% U can use attached test image for testing
% input - give the image file name as input. eg :- car3.jpg
clc;
clear all;
k=input('Enter the file name','s'); % input image; color image
im=imread(k);
im1=rgb2gray(im);
im1=medfilt2(im1,[3 3]); %Median filtering the image to remove noise%
BW = edge(im1,'sobel'); %finding edges
[imx,imy]=size(BW);
msk=[0 0 0 0 0;
0 1 1 1 0;
0 1 1 1 0;
0 1 1 1 0;
0 0 0 0 0;];
B=conv2(double(BW),double(msk)); %Smoothing image to reduce the number of connected components
L = bwlabel(B,8);% Calculating connected components
mx=max(max(L))
% There will be mx connected components.Here U can give a value between 1 and mx for L or in a loop you can extract all connected components
% If you are using the attached car image, by giving 17,18,19,22,27,28 to L you can extract the number plate completely.
[r,c] = find(L==17);
rc = [r c];
[sx sy]=size(rc);
n1=zeros(imx,imy);
for i=1:sx
x1=rc(i,1);
y1=rc(i,2);
n1(x1,y1)=255;
end % Storing the extracted image in an array
figure,imshow(im);
figure,imshow(im1);
figure,imshow(B);
figure,imshow(n1,[]);

Related

How to calculate and save correlation coefficients in matlab? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am working on pedestrian step detection (acceleration data), I want to calculate statistical features from my filtered signal. I have already calculated some and now i want to calculate correlation coefficients .
My data is of 1x37205 double. I calculated features using for loop with moving window size=2samples and 50% overlap of previous window. Below I am attaching the code I tried with to calculate corrcoef.
Now i want to check if it is the right way to calculate and also want to save the out put. I am unable to understand well that how to save all corrcoef's. Could some one guide me or provide any code help in matlab or how i can save?
%%Here M is mean and V is variance i already calculated from filtered data
for i=window:length(M)-window
C = corrcoef(M(i-window/2:i+window/2),V(i-window/2:i+window/2))
end
Looking at the corrcoef documentation you can see that the output is a 2x2 matrix.
The easiest way then to save all the coefficients would be to initialize your matrix C outside of the loop and then just assign values at each iteration.
Perhaps something like
C = zeros(2, 2, length(window:lenght(M)-window));
for i = window:length(M) - window
C(:, :, i) = corrcoef(M(i-window/2:i+window/2),V(i-window/2:i+window/2));
end
Even better, you can also see that the output of the corrcoef function is structured like this:
[1, c1;
c2, 1];
where c1 and c2 are the actual coefficients.
So you could also do:
C = zeros(length(window:lenght(M)-window), 2);
for i = window:length(M) - window
temp = corrcoef(M(i-window/2:i+window/2),V(i-window/2:i+window/2));
C(i, 1) = temp(1, 2); % "c1"
C(i, 2) = temp(2, 1); % "c2"
end
EDIT TO ANSWER TO COMMENT
To implement a sliding window with any size and with any overlap, you might want to change the indexing:
wdwSize = 2;
overlap = 0.5;
C = zeros(length(1:wdwSize*overlap:length(A)-wdwSize*overlap), 2);
for i = 1:wdwSize*overlap:length(A)-wdwSize*overlap
window = i:i+wdwSize - 1;
temp = corrcoef(M(window), V(window));
C(i, 1) = temp(1, 2); % "c1"
C(i, 2) = temp(2, 1); % "c2"
end
In your case wdwSize*overlap is an integer, but you might want to be careful for a general combination of window size and overlap.
According to the documentation, this function reurns a matrix.
If the only problem is literally "how to save this data", you can store it in a cell array.
numSamples = length(M)-window;
result = cell(1, numSamples);
for i=window:numSamples
result{i-window+1} = corrcoef(M(i-window/2:i+window/2),V(i-window/2:i+window/2))
end
Remember that you can retrieve data from the cell array just as you would do with a list, but instead of using "result(i)" you need to use "result{i}".

How to change the colors of the outliers in matlab boxplot? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I'm dealing with matlab boxplot and I want to change the colors of the outliers. I tired somethig like this without success:
figure
hold on
A=boxplot(Data,Gr,'labels',Labels,'colors',RGB,'notch','on');
grid on
ylabel('Length','Interpreter','latex','FontSize',25)
ax=gca;
ax.XTick = [1 : 44];
ax.XTickLabels=Labels
ax.XTickLabelRotation = angle;
ax.TickLabelInterpreter= 'Latex';
h = findobj(gcf,'tag','Outlier')
for i = 1:numel(h)
if rem(i,2)==0
h(i).MarkerEdgeColor = green;
end
end
Therefore i rilied on findobj function. Data is an array of different vectors each one containing several numbers. Gr groups each one of these while Label contain several names. RGB is an 22x3 array (one color for each variable). It returns me always red outliers. Can anyone help me?
I finally managed to solve my problem. The tag is wrong : I changed 'Outlier' with 'Outliers' and the code runs. Thank you anyway
#Edit 01/10/2020
I want to share with the community a part of a recent experience that I found to be useful in order to add some tips that could help Matlab users:
Basically I wanted to colour differently the boxplots and their outliers.
The result is reported in the image
angle=315;
Random = randn(5,5);
Data = [Random(1,1:end) Random(2,1:end) Random(3,1:end) Random(4,1:end)
Random(5,1:end)];
Gr = [zeros(size(Random(1,1:end))) ones(size(Random(1,1:end)))...
2*ones(size(Random(1,1:end))) 3*ones(size(Random(1,1:end)))...
4*ones(size(Random(1,1:end)))];
RGB = [rgb('DeepskyBlue') ; rgb('MediumSpringGreen') ; rgb('DeepSkyBlue');
rgb('MediumSpringGreen'); rgb('DeepSkyBlue')];
Labels =
{'$Label_{1}$','$Label_{2}$','$Label_{3}$','$Label_{4}$','$Label_{5}$'}
figure
hold on
B=boxplot(Data,Gr,'labels',Labels,'colors',RGB,'notch','on');
ylabel('$Your Data$','Interpreter','latex','FontSize',25)
bx = gca;
bx.XTick = [1 : 5];
bx.XTickLabels=Labels;
bx.XTickLabelRotation = angle;
bx.TickLabelInterpreter= 'Latex';
n = findobj(gcf,'tag','Outliers')
for j = 1:numel(n)
if rem(n(j).XData(1),2)~=0
n(j).MarkerEdgeColor = rgb('DeepSkyBlue');
else
n(j).MarkerEdgeColor = rgb('MediumSpringGreen');
end
end
The rgb function can be found in mathworks. Basically the idea is to find and recolour boxplot's odd and even positioned outliers with the same colour of the boxplot. I hope you find it useful to your purposes.

Matlab implementation of light speed labeling

I am trying to implement code for the light speed labeling technique described in this article (I cannot use the Image Processing Toolbox): https://pdfs.semanticscholar.org/ef31/7c257603004d818ca1e2a2aa67d36d40147e.pdf (see section 2, page 7).
Here is my Matlab code for LSL equivalence construction (algorithm 14, step 2).
function [EQ,ERAi,nea] = LSL_equivalence(EQ,ERim1,RLCi,ERAim1,ERAi,NERi,nea,lImg)
% LSL_EQUIVALENCE build the associative table between er and ea
% GOAL: to create a Look Up Table to be applied to ERi to create EAi.
for er = 1:2:NERi % check segments one by one
% read the boundaries of each segment to obtain de relative labels of every agacent segment in the prev line
j0 = RLCi(er);
j1 = RLCi(er+1);
er0 = ERim1(j0+1); % label of the first segment
er1 = ERim1(j1+1); % the label of the last segment
% check label parity: segments are odd, background is even
% bitand([1 2 3 4 5],1) == [1 0 1 0 1]
if bitand(er0,1) == 0 % if er0 is even
er0 = er0 + 1;
end
if bitand(er1,1) == 0 % if er1 is even
er1 = er1 -1;
end
if er1 >= er0 % if there is an adjacency
ea = ERAim1(er0+1); % absolute label of the first segment
a = EQ(ea+1); % a is the ancestor (smallest label of the equivalence class)
for erk = (er0+2):2:er1
eak = ERAim1(erk+1);
ak = EQ(eak+1);
% min extraction and propagation
if a < ak
EQ(eak+1) = a;
else
a = ak;
EQ(ea+1) = a;
ea = eak;
end
end
ERAi(er+1) = a; % the global min of all ak ancestors
else % if there are no adjacent labels make a new label
nea = nea + 1;
ERAi(er+1) = nea;
end
end
end
I am having some trouble with indexes, as the pseudo code described in the article has indexes starting with 0 and Matlab works with 1. I have already found some C++ code in this Stack Overflow post Implementing LSL for Connected Component Labeling/Blob Extraction (I applied suggested changes) and also in this git repo https://github.com/prittt/YACCLAB/blob/master/include/labeling_lacassagne_2016_code.inc. I fail to see the differences.
Also, I'm having some trouble understanding what an equivalence class is (which is what goes in matrix EQ). Thanks ahead of time!
I realize this is coming a bit late, but I just put up a piece of code that is similar to what the light speed labeling algorithm does. I'll give a brief description of how I solved the index problem.
The first step of LSL is take each column of pixels and finds the start and stop positions of consecutively labeled pixels. You can do that in Matlab like this:
I = imread('text.png');
[rows,cols] = find(xor(I(2:end,:),I(1:end-1,:)));
What this gives you is the row and column of the start and stop position of each run of pixels in a column, except its non-inclusive indexing. By non-inclusive indexing I mean the indices of pixels runs from I(r(2*n-1),c(2*n-1)) to I(r(2*n)-1,c(2*n)) for each pixel run (n). You should note that the paper operates along rows where the above code operates along columns, but the same principle applies. You should also note that the above code does not cover the circumstance of labeled pixels on the edge of the image.
If you want to see a complete implementation, I posted my code on the Matlab File Exchange. I don't claim that it copies LSL exactly, but it works on many of the same principles.
https://www.mathworks.com/matlabcentral/fileexchange/70323-ftllabel?s_tid=prof_contriblnk

Flow through a binary matrix [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have being supplied an mxn random generated binary matrix which allows flow through the value 1 but not through the value 0. I need to determine if there is a path of flow from the top to the bottom of the random generated matrix. The 1s have to be adjacent to one another. It is for programming in Matlab.
Any reference to helpful resources on how to code the problem, or algorithm examples will be greatly appreciated.
I have some code which initially searches for the first 1 in the top row.
The code therafter searches all the surrounding positions, below and adjacent to the current 1. The flow can only move diagonally down, directly left or directly right.
The purpose i to see if there is a way, given the above restrictions, to traverse from the top to the bottom of the matrix following only the 1's
Random=randi([0 1],6,6)
n =0;
while Random[0,n] ~= 1
n=n+1;
end
CoOrds = [0,n];
for i = 0:5
if Random[i+1, n-1] == 1
n=n-1;
elseif Random [i,n-1] == 1
i = i-1;
n = n-1;
elseif Random [i+1,n] ==1
n=n;
elseif Random [i+1,n+1] ==1
n=n+1;
elseif Random[i,n+1] == 1
i = i-1;
n=n+1;
end
end
You can use bwlabel and regionprops to check if a path exists:
lb = bwlabel(Random, 8); %// find 8-connected regions of the random mask
st = regionprops(lb, 'BounbdingBox'); %// find bounding boxes for all regions
b = vertcat(st.BounbdingBox);
A valid path is a path that its boundingbox height (4-th entry in 'BoundingBox') equals size(Random,1):
validPathRegion = find( b(:,4) == size(Random,1) );

Solving equations and plotting the results [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
(edited, i have changed the code) Well i have compound equations that i need to solve in matlab and find the result.
I have tried different techniques but have failed.
the equations are:
u(j-1)-2(u(j))+u(j+1)= -4*h^2*pi^2 * sin(2*pi*xj)
where
n=100
j=1 to n
xj=jh
h=1/n
u(0)==u(n)==0
I need to solve the equation and plot the results. so that i can compare the results with
the exact solution.
This is the code i have written so far...
function c= prob1()
n=100;
c=(0); % variable to store all results
u = linspace(1,n-1);
for k=3:90
jay=k;
h=1/k;
syms xj
eqn6 = u(jay-1) -2*u(jay)+u(jay+1)==-4*(h^2)*(pi^2)*sin(2*pi*xj);
A = solve(eqn6, xj); % solving the equation with xj as unknown
if(~(A==0))
c=vertcat(c,A); % just filtering out the results with 0 output
end
end
end
Now i GET answers in A like this " (625*asin(1/9877545463176224))/3927 ".
which i cannot plot.
Setting up the system of equations Au = b is done by translation the math into MATLAB language as follows:
n = 100;
h = 1/n;
j = 0:n; % include zero for the boundary condition
xj = j*h;
% set up right hand side
b = (-4*h^2*pi^2*sin(2*pi*xj))';
% overwrite the first and last value of b with the right hand side of the boundary conditions:
b([1 end]) = 0;
% This is the interesting part:
% set up A: 1*u(j-1) -2*u(j) + 1*u(j+1) and include the boundary conditions
main = [1; -2*ones(n-1,1); 1];
upper = [0; ones(n-1,1)];
lower = [ones(n-1,1); 0];
A = gallery('tridiag', lower, main, upper);
If you dont see why this works, I would suggest to write out the equations for at least j = 0, n/2 and n based on A and b, and to compare them with your equations.
Now, we are ready to solve the system. The system is small so I use the backslash operator (which is a direct method), but you could also pick iterative methods like bicgstab, gmres, qmr:
u = A\b;
Plot the resulting u:
plot(xj,u)