Matlab 如何自动保存分割后的图像
我在matlab上做了一个孟加拉文字符分割的程序,我想把一系列分割后的图像自动保存在一个特定的文件夹中。所以有人帮我用matlab代码自动保存图像吗Matlab 如何自动保存分割后的图像,matlab,image-segmentation,Matlab,Image Segmentation,我在matlab上做了一个孟加拉文字符分割的程序,我想把一系列分割后的图像自动保存在一个特定的文件夹中。所以有人帮我用matlab代码自动保存图像吗 %%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 % R
%%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
%%行分段:
全部关闭;
清除所有;
clc;
im=imread('page_1.tif');%阅读图片
%图,imshow(im);
%%标题(“带噪声的输入图像”);
qw=im;
如果大小(im,3)=3%RGB图像%%转换为灰度
im=RGB2灰色(im);
结束
阈值=灰度阈值(im);%转换为二值图像
im=~im2bw(im,阈值);
im=BWAREOPEN(im,10);%删除包含小于60像素的所有对象
%图(2)%%显示图像二值图像
%imshow(~im);
%标题(“无噪声输入图像”);
%图,面积(im);
%查找感兴趣的区域:
a=(~im);
ijk=~a;
xy=总和(ijk,2);
测量值=区域属性(xy==0,'Area','PixelIdxList');
%获取长度大于等于20的区域的索引。
fiveLongRegions=find([measurements.Area]>=3);
指数=vertcat(测量值(fiveLongRegions).PixelIdxList);
c=a;
c(指数:)=0;
%图,imshow(c);
%标题(“线段感兴趣区域”);
ca=~c;
%线路分段:
[x,y]=尺寸(ca);
mat1=总和(ca,2);%对bwrowise的元素求和并保存到列矩阵mat1中
mat2=y-mat1;%从宽度-长度(列数)中减去求和矩阵(mat1)的每个元素
mat3=mat2~=0;
mat4=差异(mat3);
index1=查找(mat4);
[q,w]=尺寸(index1);%index2矩阵的大小为q*w
kap=1;
lam=1;
%iii=1;
%ii=1;
而长度kap=20。
fiveLongRegionsword=find([measurementsword.Area]>=5);
IndexeBrow=vertcat(measurementsword(FivelongRegionWord).PixelIdxList);
cword=aword;
cword(:,theindexebrow)=1;
%%图,imshow(c);
dword=~cword;
%%图,imshow(dword);
%%标题(“分词图像的感兴趣区域”);
[xword,yword]=大小(cword);
mat1word=sum(cword,1);%对bwrowise的元素求和并保存到列矩阵mat1中
mat2word=xword-mat1word;%从宽度-长度(列数)中减去求和矩阵(mat1)的每个元素
mat3word=mat2word~=0;
mat4word=diff(mat3word);
index1word=find(mat4word);
[qword,wword]=大小(index1word);%index2矩阵的大小为q*w
kapword=1;
lamword=1;
%%iii=1;
%%ii=1;
而kapord70);
ycha=acha;
ycha(xcha,:)=0;
ycha=BWAREOPEN(ycha,20);
图,imshow(ycha);
%图,面积(y);
%mat2=总和(y,2);
%x1=查找(mat2=长度为20)。
fiveLongRegions=find([measurements.Area]>=3);
指数=vertcat(测量值(fiveLongRegions).PixelIdxList);
c=a;
c(指数:)=0;
%图,imshow(c);
%标题(“线段感兴趣区域”);
ca=~c;
%线路分段:
[x,y]=尺寸(ca);
mat1=求和(ca,2);%求和bwrowise的元素并保存到列矩阵mat1中
mat2=y-mat1;%从宽度-长度(列数)中减去求和矩阵(mat1)的每个元素
mat3=mat2~=0;
mat4=差异(mat3);
index1=查找(mat4);
[q,w]=大小(index1);%index2矩阵的大小为q*w
kap=1;
lam=1;
%iii=1;
%ii=1;
而长度kap=20。
fiveLongRegionsword=find([measurementsword.Area]>=5);
IndexeBrow=vertcat(measurementsword(FivelongRegionWord).PixelIdxList);
cword=aword;
cword(:,theindexebrow)=1;
%%图,imshow(c);
dword=~cword;
%%图,imshow(dword);
%%标题(“分词图像的感兴趣区域”);
[xword,yword]=大小(cword);
mat1word=sum(cword,1);%bwrowise的元素求和并保存到列矩阵mat1中
mat2word=xword-mat1word;%从宽度-长度(列数)中减去求和矩阵(mat1)的每个元素
mat3word=mat2word~=0;
mat4word=diff(mat3word);
index1word=find(mat4word);
[qword,wword]=大小(index1word);%index2矩阵的大小为q*w
kapword=1;
lamword=1;
%%iii=1;
%%ii=1;
而kapord70);
ycha=acha;
ycha(xcha,:)=0;
ycha=BWAREOPEN(ycha,20);
图,imshow(ycha);
%图,面积(y);
%mat2=总和(y,2);
%x1=find(mat2假设您试图将代码末尾显示的所有n1cha
图像写入文件夹
您需要维护一个计数器,计算已保存的字符数,然后使用imwrite
保存字符:
% 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
%init循环前的变量
imgFolder='path/to/saved/images';
计数器=1;
而kap如果您想将其保存到名为“folder”的文件夹中,则会出现以下情况(我正在使用并复制粘贴您的代码):
folder=“folder”//**文件夹的路径和名称**
对于ncha=1:bcha%%对象提取
[rcha,ccha]=查找(Lcha==ncha);
n1cha=ycha(最小(rcha):最大(rcha),最小(ccha):最大(ccha));
图,imshow(~n1cha);
imwrite(n1cha,文件夹);**//自动将其保存到文件夹“folder”的代码**
结束
您不能将所有代码都转储到此处(顺便说一句,格式不好),并期望我们了解问题所在。您必须非常明确:您在做什么,以及您遇到问题的具体位置
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