cp_readImage函数随记

该代码段使用MATLAB进行图像读取,支持多种文件格式,对红外和可见光图片进行预处理,包括转换为灰度图和平滑处理,特别地,处理了图像的畸变校正。
摘要由CSDN通过智能技术生成
function [I1_gray, I2_gray, I1_rgb, I2_rgb, Filename1, Filename2, pathname] = cp_readImage(distort,inputpath,file1,file2)

%%
clc
close all
%% Read images needed to be registrated
disp('【0】Reading images ...')
filenameset = ['JPG' 'PNG' 'TIF' 'BMP' 'PEG' 'PPM' 'PGM'...
               'jpg' 'png' 'tif' 'bmp' 'peg' 'ppm' 'pgm' 'dat' 'jpeg'];
ptemp = matlabpath;
ind = strfind(ptemp,';');
filepath = ptemp(ind(end)+1:end);

if nargin == 0
    distort = 0
    [Filename1,pathname] = uigetfile([filepath '\*.*'],'pick an infrared image');
    if ~contains(filenameset,Filename1(end-2:end))
        error('Incorrect file format input!');
    end
    if sum( 'dat'== Filename1(end-2:end))==3
        [I1,I11,I2,pathname,Filename1,Filename2] = cp_readTempMat(pathname,Filename1);
    else
        I1 = imread([pathname Filename1]);
        [Filename2,pathname] = uigetfile([pathname '*' Filename1(end-3:end)],'pick a visible image');
        I2 = imread([pathname Filename2]);
    end
elseif nargin == 1
    [Filename1,pathname] = uigetfile([filepath '\*.*'],'pick an infrared image');
    if ~contains(filenameset,Filename1(end-2:end))
        error('Incorrect file format input!');
    end
    if sum( 'dat'== Filename1(end-2:end))==3
        [I1,I11,I2,pathname,Filename1,Filename2] = cp_readTempMat(pathname,Filename1);
    else
        I1 = imread([pathname Filename1]);
        [Filename2,pathname] = uigetfile([pathname '*' Filename1(end-3:end)],'pick a visible image');
        I2 = imread([pathname Filename2]);
    end
elseif nargin  == 4
    I1 = imread([inputpath file1]);
    I2 = imread([inputpath file2]);
    pathname = inputpath;
    Filename1 = file1;
    Filename2 = file2;
else
    error('No sufficient input parameters!');
end

path(path, pathname(1:end-1));

if distort
    if size(I1,1)~=768 && size(I1,1)>288 && size(I1,1)<1080 && size(I2,1)==1190
        I1(289:end,:,:)  = [];
        I2(1081:end,:,:) = [];
    elseif ( size(I1,1)==768 && size(I1,2)==576 )||( size(I1,1)==800 && size(I1,2)==600 )
        I1 = cp_undistortimg(I1);
        I2 = cp_undistortimg(I2);
        disp('   Undistort image successfully!');
    elseif size(I1,2)==768 && size(I1,1)==576
        I1 = cp_undistortimg(imrotate(I1,90));
        I2 = cp_undistortimg(I2);
        disp('   Undistort image successfully!');
    elseif size(I2,2)==768 && size(I2,1)==576
        I2 = cp_undistortimg(imrotate(I2,90));
        I1 = cp_undistortimg(I1);
        disp('   Undistort image successfully!');
    end
end
fprintf('\tsize of Image 1 is [%d×%d]\n    size of Image 2 is [%d×%d]\n',size(I1,1),size(I1,2),size(I2,1),size(I2,2));
if size(I1,3)==3 & size(I2,3)==3 
        I1_rgb = I1;
        I2_rgb = I2;
        I1_gray = double(rgb2gray(I1));
        I2_gray = double(rgb2gray(I2));
elseif size(I1,3)==3 & size(I2,3)==1 
        I1_gray = double(rgb2gray(I1));
        I2_gray = double(I2);
        I1_rgb = I1;
        I2_rgb = 0;
elseif size(I1,3)==1 & size(I2,3)==3 
        I1_gray = double(I1);
        I2_gray = double(rgb2gray(I2));
        I1_rgb = 0;
        I2_rgb = I2;
else
        I1_gray = double(I1);
        I2_gray = double(I2);
        I1_rgb = 0;
        I2_rgb = 0;
end
I1_gray = uint8((I1_gray-min(min(I1_gray)))./(max(max(I1_gray))-min(min(I1_gray))) * 255);
I2_gray = uint8((I2_gray-min(min(I2_gray)))./(max(max(I2_gray))-min(min(I2_gray))) * 255);

disp('【0】Completed to read image!')

实现了图片的定标,将红外图片转化为灰度图,再进行一次平滑处理。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值