jinxiuliang的个人博客分享 http://blog.sciencenet.cn/u/jinxiuliang

博文

matlab彩色图像的批处理转换为灰度、二值和主成分图图像

已有 5246 次阅读 2016-7-28 22:11 |系统分类:科研笔记|关键词:学者| 二值和主成分图图像

这段代码主要用来进行图像的批处理转换为灰度、二值和主成分图图像,希望给大家借鉴。其中 RGB to bw可以直接实现,但是效果不好,所以先用RGB to gray,之后再gray to bw。RGB to PAC 涉及到三维数据转换为二维的问题。这里面涉及到数据排序的问题,需要使用到LengthSortStr函数,我以作为附件添加。由于不太熟悉科学网添加方式,所以添加的不是很好,望大家见谅。

% The RGB to gray
path='D:7-21-simplewaystems11HOG adaboost';
file='*.tif';
pic=dir([path,file]);
filename=str2mat(pic.name); %取得文件名
%调用函数
sortfile=LengthSortStr(filename);
num=size(pic,1);
image_filename='D:7-21-simplewaystems11HOG adaboost';
mkdir(image_filename,'stemsgray')
output_default_path = fullfile(image_filename,'stemsgray');
cd(output_default_path)
% message = 'Select the output folder';
% uiwait(msgbox(message));
% output_folder = uigetdir(output_default_path);
for i=1:num;
tiff{i}=imread([path,sortfile(i,:)]);
images{i}=rgb2gray(tiff{i}); % color to gray
images{i}=imadjust(images{i}); % ajusting image quality
filenames= strcat('stemsgray',num2str(i),'.tif');
imwrite(images{i},filenames)
end


% The gray to bw
clear;clc
path='D:7-21-simplewaystems11HOG adabooststemsgray';
file='*.tif';
pic=dir([path,file]);% read the number of images
filename=str2mat(pic.name); %obtain filenames
sortfile=LengthSortStr(filename);% sort function
num=size(pic,1);
image_filename='D:7-21-simplewaystems11HOG adaboost';
mkdir(image_filename,'stemsgraybw')
output_default_path = fullfile(image_filename,'stemsgraybw');
cd(output_default_path)
for i=1:num;
tiff{i}=imread([path,sortfile(i,:)]);
images{i}=im2bw(tiff{i},0.5)
filenames= strcat('wbjin',num2str(i),'.tif');
imwrite(images{i},filenames);
end


% The RGB to PCA

path='D:simplewaystemssimplywaycodebadresults';
file='*.tif';
pic=dir([path,file]);
filename=str2mat(pic.name); %取得文件名
%调用函数
sortfile=LengthSortStr(filename);
num=size(pic,1);
tiff={};
for i=1:num;
tiff{i}=imread([path,sortfile(i,:)]);
R(:,:)=tiff{i}(:,:,1); % 3D to 2D
G(:,:)=tiff{i}(:,:,2);
B(:,:)=tiff{i}(:,:,3);
[m n]=size(R);% size of 2D
R1=reshape(R,prod(size(R)),1); % Multi-row and Single-column
G1=reshape(G,prod(size(G)),1);
B1=reshape(B,prod(size(B)),1);
RGB1=[R1,G1,B1]; % three columns of RGB
[coeff,score,latent] = pca(double(RGB1));% PCA caculation
score1=score(:,1);% the first principal is selected.
% percetanges=cumsum(latent)./sum(latent);
pcaiamges{i}=reshape(score1,m,n);
pcaiamges1{i}=im2bw(pcaiamges{i})
filenames= strcat('bw',num2str(i),'.tif');
imwrite(pcaiamges1{i},filenames);
end

LengthSortStr.m




https://m.sciencenet.cn/blog-620863-993279.html

上一篇:matlab图像数据的调节和转换
下一篇:matlab图像数据的批处理cutting

1 田海静

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-6-16 01:40

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部