关于图像处理
- 注意在进行图像处理前,要对读入的图像数据使用double()或im2double()进行类型转换!!
否则,uint8类型的图像数据运算结果超出255就会发生溢出!! - imagesc后加上axis image,这样可以使输出的图片与原始矩阵吻合;
绘制二维图,(默认)起点在左上角,axis ij; axis xy 可以改成起点在左下角; - 对于 JPEG格式,可以调整’模式’或’质量’属性可减少图像压缩。 PNG格式使用无损压缩。如果您想避免所有压缩(有损或无损),您也可以将图像保存为BMP格式。
X = imread('peppers.png');
imwrite(X,'peppers.png');
imwrite(X,'peppers.jpg', 'Quality',100);
imwrite(X,'peppers.jpg','Mode','lossless');
Q1:获得满足条件的索引向量
经常遇到比如求矩阵中大于0的元素个数这样的问题,这样简单的问题每次都出错,都是现查现用,今天就做一个汇总,记录一下。
比如说求矩阵A中大于0的元素个数,在matlab里可以这样写:
sum(A(:)>0),这也是最简单的一种写法。
还有一种是利用find函数,先返回满足条件的元素的索引组成的向量,然后输出索引的长度即可,
length(find(tm>0))
例
获得矩阵A中所有0元素的横坐标和
ind = find(A == 0);%获得索引向量
ind_x = ceil( ind./size(A, 1) );%横坐标列向量
XSum = sum(ind_x(:));%所有元素求和
Q2:单层目录 - 批量读取文件
srcFace = 'D:\face\facePN1\posdata';%被读取文件的存放目录(根据自己需要更改设置)
fileSavePath='D:\face\facePN1\posdataResize';%文件保存目录(根据自己需要更改设置)
src=srcFace;
srcsuffix='.jpg';%被读取的文件名后缀(根据被读取文件的实际文件类型设置)
srcsuffixSave='.png';%保存文件名后缀(根据自己需要更改设置)
files = dir(fullfile(src, strcat('*', srcsuffix)));
doDispOrSave = true ;% 是否显示或保存图像;可以设置为:true 或者 false
for file_i= 1 : length(files)
disp(file_i);%显示当前处理的文件序号
srcName = files(file_i).name;
noSuffixName = srcName(1:end-4);
srcName1=files(file_i).name;
pathImgName=sprintf('%s%s%s',src,'\',srcName1);
imgSrc=imread(pathImgName);%读入图像
%对读入的图像进行尺度缩放处理
imgResize=imresize(imgSrc,0.95);
%显示或者保存图像
if(doDispOrSave==true)%显示原始图像和处理后的图像
close all;
figure('Name','imgSrc-imgResize');
subplot(2,1,1); imshow(imgSrc);
subplot(2,1,2); imshow(imgResize);
else%保存处理后的文件
savePathName=sprintf('%s%s%s%s',fileSavePath,'\',noSuffixName,srcsuffixSave);
imwrite(imgResize,savePathName);
end
end
Q3:多层目录
Q4:查找路径下包含相应字段文件
function [ VecFiles ] = FindFiles( InputDir,ext,IsReturnNameOnly )
%查找当前路径下所有文件夹
%InputDir: 输入路径
%ext:查找文件名字符段
%IsReturnNameOnly:是否只返回文件名
%check当前路径是否合法
if ~isdir(InputDir)
msgbox('The input isnot a valid directory','Warning','warn');
return
else
if nargin == 1
ext = '*';
IsReturnNameOnly = 1;
elseif nargin == 2
IsReturnNameOnly = 1;
elseif nargin>3||nargin<1
msgbox('1 or 2 inputs are required','Warning','warn');
return
end
if nargout>1
msgbox('Too many output arguments','Warning','warn');
return
end
end
%初始化文件列表
filesname = {};
%查找当前路径下所有含有ext字段文件
strtmp = strcat(InputDir,'\*',ext);
files = dir(strtmp);
m = length(files);
%check是否为文件,如是则存入vector
num = 0;
for i =1:1:m
%排除.和..
if ~(strcmp(files(i).name,'.')||strcmp(files(i).name,'..'))
tmp = files(i).name;
if ~files(i).isdir
num = num + 1;
if IsReturnNameOnly %返回文件名列表
filesname{num} = tmp;
else %返回文件夹全路径列表
tmp = fullfile(InputDir,tmp);
filesname{num} = tmp;
end
end
end
end
%赋值到返回变量VecFolders
if nargout==1
VecFiles = filesname';%返回列向量
end
end
示例调用:
clc;
clear;
path = 'E:\CCS6.2';
a = FindFiles(path,'.txt',0);
获取子文件夹下特定后缀文件
r_suffix = '.jpg';
maindir = 'D:\Pictures'
subdir = dir(maindir);
dirpath1 = fullfile( maindir, subdir(7).name, strcat('*', r_suffix) );
dat = dir( dirpath1 );
datpath = fullfile( dirpath, dat( 1 ).name);
a = double( imread(datpath) );
imshow(a)
Q5:将文件添加到路径
currpath = fileparts(mfilename('fullpath'));
// mfilename p = mfilename('fullpath') 返回其中进行了调用的文件的完整路径和名称,不包括文件扩展名。
// fileparts 获取文件名的组成部分[filepath,name,ext] = fileparts(filename) 返回指定文件的路径名称、文件名和扩展名。
addpath(genpath([currpath,filesep,'utils']));
addpath(genpath([currpath,filesep,'io']));
addpath(genpath([currpath,filesep,'preproc']));
addpath(genpath([currpath,filesep,'postproc']));
Q6:tiff格式转换真彩色图像、索引色图像、灰度图像、 真彩色图像RGB、YIQ图像、HSV图像、YCbCr图像
I=imread(‘flower.tif’);%读入图片
whos I
imfinfo(‘flower.tif’)
imshow(I);title(‘原始tif图像’)
%%真彩图像、转索图像、灰度图像、二值图像的相互转换
imwrite(I,‘flower.jpg’,‘quality’,10)%tif转化rgb
I1=imread(‘flower.jpg’);
figure;imshow(I1);title(‘真彩色图像’)
[X,map] = rgb2ind(I1,32);%真彩转索引
figure;imshow(X,map),colorbar,title(‘索引图像’)
rgb=ind2rgb(X,map);%索引转rgb
figure;imshow(rgb);title(‘rgb图像’)
G=rgb2gray(I1);
figure;imshow(G);title(‘rgb转灰度图像’)
G1=ind2gray(X,map);%索引转灰度
figure;imshow(G1);title(‘索引转灰度图像’)
G3=im2bw(I1);%图像转二值图像
figure;imshow(G3);title(‘二值图像’)
%%RGB/YIQ/HSV/YCbCr之间的转换
image = imread(‘lena.jpg’);
figure;subplot(331);imshow(image);title(‘原始图像’)
%RGB&YIQ
YIQ=rgb2ntsc(image);
subplot(332);imshow(YIQ);title(‘YIQ图像’)
rgb=ntsc2rgb(YIQ);
subplot(333);imshow(rgb);title(‘rgb图像’)
%RGB&HSV
HSV=rgb2hsv(image);
subplot(334);imshow(image);title(‘原始图像’)
subplot(335);imshow(HSV);title(‘HSV图像’)
RGB1=hsv2rgb(HSV);
subplot(336);imshow(RGB1);title(‘RGB图像’)
%RGB&YCbCr
yc=rgb2ycbcr(image);
subplot(337);imshow(image);title(‘原始图像’)
subplot(338);imshow(yc);title(‘ycbcr图像’)
rgb2=ycbcr2rgb(yc);
subplot(339);imshow(rgb2);title(‘RGB图像’)