how to plot the histogram of the segmented line of document images - matlab

I am writing a program for line segmentation and I want to plot a horizontal histogram of the input image and the segmented lines, but I don't know the code for plotting histograms. Please help me, I have given my program below:
close all;
clear all;
clc;
im=imread('test_img.tif');%read the image
figure, imshow(im);
title('INPUT IMAGE WITH NOISE');
area(im)
if size(im,3)==3 % RGB image %% Convert to gray scale
im=rgb2gray(im);
end
threshold = graythresh(im); %% Convert to binary image
im =~im2bw(im,threshold);
im = bwareaopen(im,15); %% Remove all object containing fewer than 60 pixels
figure(2) %% Show image binary image
imshow(~im);
title('INPUT IMAGE WITHOUT NOISE')
%find Area of Interest:
a=(~im);
ijk=~a;
xy=sum(ijk,2);
measurements = regionprops(xy == 0, 'Area', 'PixelIdxList');
% Get indexes of those regions that are >= 20 in length.
fiveLongRegions = find([measurements.Area] >= 5);
theIndexes = vertcat(measurements(fiveLongRegions).PixelIdxList);
c=a;
c(theIndexes,:)=0;
figure, imshow(c);
title('AREA OF INTEREST FOR LINE SEGMENT');
%Segmentation of line:
[x,y]=size(a);
mat1=sum(a,2);%sum the elements of bwrowise and save into column matrix mat1
mat2=y-mat1;%subtract each element of the sum matrix(mat1) from the width length(no. of columns)
mat3=mat2~=0;
mat4=diff(mat3);
index1=find(mat4);
[q,w]=size(index1);%size of index2 matrix is q*w
kap=1;
lam=1;
%iii=1;
%ii=1;
while kap<((q/2)+1);%number of loops=number of lines
k=1;
mat5=([]);
for j=(index1(lam)+1):1:index1(lam+1)
mat5(k,:)=a(j,:); %store the line segmented matrix
k=k+1;
end
lam=lam+2;
kap=kap+1;
figure, imshow(mat5);
imsave();
end

Related

Fit a distribution line in a histogram on Matlab

This is my code and the result I got. Something is not right — I used histfit(cntH,NumBins,'kernel'), and I was expecting the distribution line to start from zero and fit into the bars
How can I fix that?
clear all
clc
% yG = total
load yH
% specify number of bins and edges of those bins; this example evenly spaces bins
NumBins = 100;
BinEdges = linspace(0,35,70);
% use histcounts and specify your bins
cntH = histcounts(yH,'BinEdges',BinEdges);
% plot
figure(1); cla; hold on;
% convert bin edges into bin centers
b = BinEdges(1:end-1)+diff(BinEdges)/2
% use bar
bar(b,[cntH'],'stacked')
histfit(cntH,NumBins,'kernel');
% Labels
xlabel('Length (mm)')
ylabel('Count (log scale)')
set(gca,'YScale','log')
title('Count (log scale)')

bubble size distribution in bubble column using image processing technique

I have a video from which I have extracted several frames. I need to analyse them to get the total number and the sizes of the bubbles. The program I have so far can identify a single bubble in the image but has trouble to determine multiple bubbles.
code:
clc;
clear all;
close all;
% ----------------- Select the image -------------------------------
% Double click the image fle to be selected
[FileName,PathName] = uigetfile('*.png','Select the image');
file = fullfile(PathName,FileName);
img=imread(file); % Reads the image
rgb = imcrop(img);
figure,imshow(rgb)
d = imdistline; %to find the appropriate radius range
delete(d);
gray_image = rgb2gray(rgb);
figure, imshow(gray_image);
impixelinfo
[centers, radii] = imfindcircles(rgb,[3 10],'ObjectPolarity','dark')
hold on
[centers, radii] = imfindcircles(rgb,[3 10],'ObjectPolarity','dark', ...
'Sensitivity',0.96)
hold off
figure, imshow(rgb);
h = viscircles(centers,radii);
[centers, radii] = imfindcircles(rgb,[3 10],'ObjectPolarity','dark', ...
'Sensitivity',0.96);
length(centers)
delete(h); % Delete previously drawn circles
h = viscircles(centers,radii);
%second method(2 stage )
[centers, radii] = imfindcircles(rgb,[3 10], 'ObjectPolarity','dark', ...
'Sensitivity',0.96,'Method','twostage');
delete(h);
h = viscircles(centers,radii);
The Frame Given As Input To Code
Output i am getting
I need help to get the correct number of bubbles in the image.
since in output screenshot there is some cluster where algo fails to get the correct number of bubbles.

Convert Matlab code into Simulink

I would like to convert a image processing program(part of the program below) from Matlab to Simulink and possibly convert the simulink diagram into C code later on. I have 0 experience in Simulink and was wondering if there's any limitations on the types of matlab program/functions that can be converted and how I would go about doing this. Thanks.
clear all
clc
% Read in an image 1
C1 = imread('cloud1.jpg');
Cloud1 = C1(:,:,1); % use only one color
%Cloud1 = Cloud1'; % transpose to get (x,y) instead of (y,x)
Cloud1_xsize = size(Cloud1,2); % get x size of image
Cloud1_ysize = size(Cloud1,1); % get y size of image
%figure(3), imshow(Cloud1) % to plot you need to transpose back to their coordinate system
%hold on
% Read in an image 2
C2 = imread('cloud2.jpg');
Cloud2 = C2(:,:,1); % use only one color
%Cloud2 = Cloud2'; % transpose to get (x,y) instead of (y,x)
Cloud2_xsize = size(Cloud2,2); % get x size of image
Cloud2_ysize = size(Cloud2,1); % get y size of image
%figure(2), imshow(Cloud2)
%hold on
% show the shift in the initial images several times
num = 0;
for k = 1:4
num=num+1;
pause(.5)
figure(1), h1=imshow(C1)
xlabel('FIGURE 1')
F(num) = getframe(gcf);
%image(F.cdata)
%colormap(F.colormap)
pause(0.25)
figure(1), h2=imshow(C2)
xlabel('FIGURE 2')
num=num+1;
F(num) = getframe(gcf);
%image(F.cdata)
%colormap(F.colormap)
end
% Play the movie twenty times
%movie(F,20)
%%%% Set the template size %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% First calc the number of pixels in the shortest direction of the image (usually y direction)
MinSize = min(Cloud1_xsize, Cloud1_ysize); % number of pixels in shortest direction
%%% N is the minimum number of boxes in the shorter direction (usually y direction).
%%% In the shorter axis (usually y)there will be N-2 boxes analyzed.
%%% This is because the top and bottom boxes are considered too close to the edge to use.
%%% In the larger direction (usually x) there may be more boxes.
N = 6;
EdgeBoxSize = 1; % the number of edge boxes along each edge
TempWidth = floor(MinSize / N); % the pixel width of each template box
TempHeight = TempWidth; % make the template height and width the same size so corr part works good
%%% Now calculate the exact number of boxes in x and y directions
%%% This depends on the number of x versus y pixels.
Nx = floor(Cloud1_xsize/TempWidth);
Ny = floor(Cloud1_ysize/TempWidth);

How to save the segmented image automatically

I did a program on matlab for character segmentation of bengali script and i want to save the series of segmented image automatically in a specific folder. So anyone help me with the matlab code for saving image automatic
%%LINES SEGMENTATION:
close all;
clear all;
clc;
im=imread('page_1.tif');%read the image
%figure, imshow(im);
%%title('INPUT IMAGE WITH NOISE');
qw=im;
if size(im,3)==3 % RGB image %% Convert to gray scale
im=rgb2gray(im);
end
threshold = graythresh(im); %% Convert to binary image
im =~im2bw(im,threshold);
im = bwareaopen(im,10); %% Remove all object containing fewer than 60 pixels
%figure(2) %% Show image binary image
%imshow(~im);
%title('INPUT IMAGE WITHOUT NOISE');
%figure, area(im);
%find Area of Interest:
a=(~im);
ijk=~a;
xy=sum(ijk,2);
measurements = regionprops(xy == 0, 'Area', 'PixelIdxList');
%Get indexes of those regions that are >= 20 in length.
fiveLongRegions = find([measurements.Area] >= 3);
theIndexes = vertcat(measurements(fiveLongRegions).PixelIdxList);
c=a;
c(theIndexes,:)=0;
%figure, imshow(c);
%title('AREA OF INTEREST FOR LINE SEGMENT');
ca=~c;
%Segmentation of line:
[x,y]=size(ca);
mat1=sum(ca,2);%sum the elements of bwrowise and save into column matrix mat1
mat2=y-mat1;%subtract each element of the sum matrix(mat1) from the width length(no. of columns)
mat3=mat2~=0;
mat4=diff(mat3);
index1=find(mat4);
[q,w]=size(index1);%size of index2 matrix is q*w
kap=1;
lam=1;
%iii=1;
%ii=1;
while kap<((q/2)+1);%number of loops=number of lines
k=1;
mat5=([]);
for j=(index1(lam)+1):1:index1(lam+1)
mat5(k,:)=c(j,:); %store the line segmented matrix
k=k+1;
end
lam=lam+2;
kap=kap+1;
figure, imshow(mat5);
%%imsave();
%%WORDS SEGMENTATION:
bwword=im2bw(mat5);%convert the image im into binary(black and white)
aword=~bwword;
bword=sum(aword,1);
%%figure, area(b);
%%Measure lengths of all "0" regions.
measurementsword = regionprops(bword == 0, 'Area', 'PixelIdxList');
% Get indexes of those regions that are >= 20 in length.
fiveLongRegionsword = find([measurementsword.Area] >= 5);
theIndexesword = vertcat(measurementsword(fiveLongRegionsword).PixelIdxList);
cword=aword;
cword(:,theIndexesword)=1;
%%figure, imshow(c);
dword=~cword;
%%figure, imshow(dword);
%%title('AREA OF INTERESRT FOR WORD SEGMENTATION IMAGE');
[xword,yword]=size(cword);
mat1word=sum(cword,1);%sum the elements of bwrowise and save into column matrix mat1
mat2word=xword-mat1word;%subtract each element of the sum matrix(mat1) from the width length(no. of columns)
mat3word=mat2word~=0;
mat4word=diff(mat3word);
index1word=find(mat4word);
[qword,wword]=size(index1word);%size of index2 matrix is q*w
kapword=1;
lamword=1;
%%iii=1;
%%ii=1;
while kapword<((wword/2)+1);%number of loops=number of lines
kword=1;
mat5word=([]);
for jword=(index1word(lamword)+1):1:index1word(lamword+1)
mat5word(:,kword)=bwword(:,jword); %store the word segmented matrix
kword=kword+1;
end
lamword=lamword+1;
kapword=kapword+1;
figure, imshow(mat5word);
%imsave();
%%CHARACTER SEGMENTATION:
bwcha=im2bw(mat5word);%convert the image im into binary(black and white)
acha=~bwcha;
mat1cha=sum(a,2);
%figure, area(mat1);
xcha=find(mat1cha>70);
ycha=acha;
ycha(xcha,:)=0;
ycha = bwareaopen(ycha,20);
figure,imshow(ycha);
%figure, area(y);
%mat2=sum(y,2);
%x1=find(mat2<7);
%y1=y;
%y1(x1,:)=0;
%y1 = bwareaopen(y1,15);
%z=~y1;
%figure, imshow(z);
[Lcha, bcha,]=bwlabel(ycha); %% Label connected components
propiedcha=regionprops(Lcha,'BoundingBox'); %% Measure properties of image regions
for ncha=1:size(propiedcha,1) %% Plot Bounding Box
rectangle('Position',propiedcha(ncha).BoundingBox,'EdgeColor','g','LineWidth',1);
end
for ncha=1:bcha %% Objects extraction
[rcha,ccha] = find(Lcha==ncha);
n1cha=ycha(min(rcha):max(rcha),min(ccha):max(ccha));
figure, imshow(~n1cha);
%pause(0.2)
%imsave();
end
end
end
`enter co%%LINES SEGMENTATION:
close all;
clear all;
clc;
im=imread('page_1.tif');%read the image
%figure, imshow(im);
%%title('INPUT IMAGE WITH NOISE');
qw=im;
if size(im,3)==3 % RGB image %% Convert to gray scale
im=rgb2gray(im);`enter code here`
end
threshold = graythresh(im); %% Convert to binary image
im =~im2bw(im,threshold);
im = bwareaopen(im,10); %% Remove all object containing fewer than 60 pixels
%figure(2) %% Show image binary image
%imshow(~im);
%title('INPUT IMAGE WITHOUT NOISE');
%figure, area(im);
%find Area of Interest:
a=(~im);
ijk=~a;
xy=sum(ijk,2);
measurements = regionprops(xy == 0, 'Area', 'PixelIdxList');
%Get indexes of those regions that are >= 20 in length.
fiveLongRegions = find([measurements.Area] >= 3);
theIndexes = vertcat(measurements(fiveLongRegions).PixelIdxList);
c=a;
c(theIndexes,:)=0;
%figure, imshow(c);
%title('AREA OF INTEREST FOR LINE SEGMENT');
ca=~c;
%Segmentation of line:
[x,y]=size(ca);
mat1=sum(ca,2);%sum the elements of bwrowise and save into column matrix mat1
mat2=y-mat1;%subtract each element of the sum matrix(mat1) from the width length(no. of columns)
mat3=mat2~=0;
mat4=diff(mat3);
index1=find(mat4);
[q,w]=size(index1);%size of index2 matrix is q*w
kap=1;
lam=1;
%iii=1;
%ii=1;
while kap<((q/2)+1);%number of loops=number of lines
k=1;
mat5=([]);
for j=(index1(lam)+1):1:index1(lam+1)
mat5(k,:)=c(j,:); %store the line segmented matrix
k=k+1;
end
lam=lam+2;
kap=kap+1;
figure, imshow(mat5);
%%imsave();
%%WORDS SEGMENTATION:
bwword=im2bw(mat5);%convert the image im into binary(black and white)
aword=~bwword;
bword=sum(aword,1);
%%figure, area(b);
%%Measure lengths of all "0" regions.
measurementsword = regionprops(bword == 0, 'Area', 'PixelIdxList');
% Get indexes of those regions that are >= 20 in length.
fiveLongRegionsword = find([measurementsword.Area] >= 5);
theIndexesword = vertcat(measurementsword(fiveLongRegionsword).PixelIdxList);
cword=aword;
cword(:,theIndexesword)=1;
%%figure, imshow(c);
dword=~cword;
%%figure, imshow(dword);
%%title('AREA OF INTERESRT FOR WORD SEGMENTATION IMAGE');
[xword,yword]=size(cword);
mat1word=sum(cword,1);%sum the elements of bwrowise and save into column matrix mat1
mat2word=xword-mat1word;%subtract each element of the sum matrix(mat1) from the width length(no. of columns)
mat3word=mat2word~=0;
mat4word=diff(mat3word);
index1word=find(mat4word);
[qword,wword]=size(index1word);%size of index2 matrix is q*w
kapword=1;
lamword=1;
%%iii=1;
%%ii=1;
while kapword<((wword/2)+1);%number of loops=number of lines
kword=1;
mat5word=([]);
for jword=(index1word(lamword)+1):1:index1word(lamword+1)
mat5word(:,kword)=bwword(:,jword); %store the word segmented matrix
kword=kword+1;
end
lamword=lamword+1;
kapword=kapword+1;
figure, imshow(mat5word);
%imsave();
%%CHARACTER SEGMENTATION:
bwcha=im2bw(mat5word);%convert the image im into binary(black and white)
acha=~bwcha;
mat1cha=sum(a,2);
%figure, area(mat1);
xcha=find(mat1cha>70);
ycha=acha;
ycha(xcha,:)=0;
ycha = bwareaopen(ycha,20);
figure,imshow(ycha);
%figure, area(y);
%mat2=sum(y,2);
%x1=find(mat2<7);
%y1=y;
%y1(x1,:)=0;
%y1 = bwareaopen(y1,15);
%z=~y1;
%figure, imshow(z);
[Lcha, bcha,]=bwlabel(ycha); %% Label connected components
propiedcha=regionprops(Lcha,'BoundingBox'); %% Measure properties of image regions
for ncha=1:size(propiedcha,1) %% Plot Bounding Box
rectangle('Position',propiedcha(ncha).BoundingBox,'EdgeColor','g','LineWidth',1);
end
for ncha=1:bcha %% Objects extraction
[rcha,ccha] = find(Lcha==ncha);
n1cha=ycha(min(rcha):max(rcha),min(ccha):max(ccha));
figure, imshow(~n1cha);
%pause(0.2)
%imsave();
end
end
end
de here`ally i am providing my code
Assuming you are trying to write to folder all n1cha images that you show at the end of your code.
You'll need to maintain a counter that counts how many characters you already saved and then use imwrite to save the characters:
% init variables before loop
imgFolder = 'path/to/saved/images';
counter = 1;
while kap<((q/2)+1);%number of loops=number of lines
% your code here... nested loops...
% ...
% finally you get to this loop:
for ncha=1:bcha
% extract the character...
imwrite( n1cha, fullfile( imgFolder, sprintf( 'char_%04d.png', counter ) ) );
counter = counter + 1;
end
end
If you want to save it to folder with name 'Folder', here it goes(I'm using and copy paste your code):
folder = "Folder"; // **the path and name of your folder**<br/>
for ncha=1:bcha %% Objects extraction<br/>
[rcha,ccha] = find(Lcha==ncha);<br/>
n1cha=ycha(min(rcha):max(rcha),min(ccha):max(ccha));<br/>
figure, imshow(~n1cha);<br/>
imwrite(n1cha, folder); **//code to save it to folder 'Folder' automatically**<br/>
end

how to detect optic disc in retinal images using matlab

enter code here
% Finding mean for 49*49 sub images
meanmat=ones(209,209);
k=1;count=1;
[m n]=size(BW);
for i=25:1:m-24
for j=25:1:n-24
rowst=i-24;rowend=i+24;
colst=j-24;
colend=j+24;
summat=0;
for row=rowst:1:rowend
for col=colst:1:colend
summat=summat+BW(row,col);
end
end
meanval=(summat)/(49*49);
meanmat(k,count)=meanval;
if(count>208)
count=1;k=k+1;
else
count=count+1;
end
end
end
% mean and median filter
I = im2double(BW);
kernel = ones(80,80) / (80*80); % 49*49 mean kernel
J = conv2(I, kernel, 'same'); % Convolve keeping size of I
stdDevImage = stdfilt(BW);
varianceIMage = stdDevImage .^2;
figure,subplot(2,2,1);
imshow(BW);title('Original');
subplot(2,2,2);
imshow(meanmat);title(sprintf('MeanImage'));
subplot(2,2,3);
imshow(varianceIMage);title(sprintf('variance using stdfilt'));
subplot(2,2,4);
imshow(J);title(sprintf('Average Variance using conv2'));
I have calculated mean,variance and average variance for the image using the above code.But the optic disc was not detected properly.Can anyone help me with the matlab code for finding the optic disc?
%% Initial Image Processing.
% get image and mode from workspace
Im = evalin('base','Im');
singlemode = evalin('base','singlemode');
% apply filter on green component
f1 = fspecial('average',31); % create averaging filter
IMA = imadjust(Im(:,:,2)); % adjust green component intensity first
Imf = filter2(f1,IMA); % apply filter
Imf = mat2gray(Imf); % convert back from (0,1) to (0,255)
axes(handles.axes2); % show grayscale filtered image
imshow(Imf);
%% Edge Detection.
thrsh = str2num(get(handles.edit1,'String')); % default = 0.98
Ibw=im2bw(Imf,thrsh); % binary mask / black & white threshold
I_edge = edge(Ibw); % edge detection
% axes(handles.axes3);
% imshow(I_edge); % show image
%% Circular Hough transform to detect optical disc (OD).
% Rmin = str2num(get(handles.edit3,'String')); % minimum radius from user input
% Rmax = str2num(get(handles.edit4,'String')); % maximum radius from user input
Rmin = 80;
Rmax = 120;
radii = Rmin:5:Rmax; % radii to check
h = circle_hough(I_edge,radii,'same','normalise');
peaks = circle_houghpeaks(h,radii,'nhoodxy',2*(Rmax/2)+1,'nhoodr',5,'npeaks',1); % find the peaks in the transform, meaning find the best fit of a circle. and we only want 1 circle.
% Lines 196 -207 - if we're in single mode, we're plotting the circle in the top left image
if singlemode
axes(handles.axes1);
imshow(Im);
hold on
for peak = peaks
[x y] = circlepoints(peak(3)); % This function returns the x,y coordinates of the found circle, peak(3) is the radius of the found circle.
plot(x+peak(1),y+peak(2),'b-'); % we plot the x,y coordinates shifted by the center of the circle, which are peak(1) and peak(2)
end
plot(peak(1),peak(2),'r+'); % We plot the center of the circle with a red cross.
hold off
end
axes(handles.axes2); % Lines 208 - 214, we repeat the plot of the circle into axes2, the top right figure
hold on
for peak = peaks
[x y] = circlepoints(peak(3));
plot(x+peak(1),y+peak(2),'b-');
end
hold off
%% Region of Interest (ROI) Processing.
% we have localized the OD, define the ROI around this position for further
% processing.
axes(handles.axes2); % Activate axes2.
e = imellipse(gca,[peak(1)-2*peak(3) peak(2)-2*peak(3) 4*peak(3) 4*peak(3)]); %get an elliptical sub image centered around the peak(1) and peak(2) values (the centers of the previously found OD) and 2 times the radius that was found previously.
BW = createMask(e); % Create a mask from this region of interest = ROI.
Imf_masked = Imf.*BW; % Apply mask to the image, this will make everything outside of the ROI become black.
% axes(handles.axes4);
% imshow(Imf_masked);
thrsh = str2num(get(handles.edit5,'String')); % get threshold for ROI hough transform from the GUI, default = 0.65
Ibw=im2bw(Imf_masked,thrsh); % binary mask / black & white threshold (threshold the masked image).
I_edge = edge(Ibw); % edge detection
% axes(handles.axes3);
% imshow(I_edge); % show image
% hough transform to find circle in the ROI
h = circle_hough(I_edge,radii,'same','normalise');
peaks = circle_houghpeaks(h,radii,'nhoodxy',2*(Rmax/2)+1,'nhoodr',5,'npeaks',1);
if singlemode
axes(handles.axes1);
hold on
for peak = peaks
[x y] = circlepoints(peak(3));
plot(x+peak(1),y+peak(2),'g-');
end
plot(peak(1),peak(2),'g+');
hold off
end