处理1张图片,获取4幅不同伽马校正图像
实验结果
实验代码
get_4_pictures_gamma_correction.m
%----批量处理伽马校正---
clc;clear;
name = 'airplane.png';
prefix = name(1:end-4); % 去掉name后面的.png
I = imread(name);
% figure,imshow(I);
gammaCorrection(name,1,0.75); %自定义函数
gamma_list = [0.75, 0.9, 1.1, 1.25];
for i = 1:length(gamma_list)
gamma = gamma_list(i);
s = gammaCorrection(name,1,gamma);
str0='D:\user\user\毕业设计\code_for_hashing\matlab_code\test\';
str1= [prefix,'_','gamma=',num2str(gamma)];%字符串拼接
str2 = '.png';
save_path=[str0,str1,str2]; % 字符串拼接
imwrite(s,save_path);
end
gammaCorrection.m
function [s]=gammaCorrection(name, a, gamma)
r = imread (name);
r=im2double(r);
s = a * (r .^ gamma);
% imwrite(s,'airplane_gamma=0.75.jpg','jpg');
subplot (1 ,2 ,1), imshow(r), title('Original');
subplot (1 ,2 ,2), imshow(s), title(sprintf('Gamma: %0.2f',gamma));
end
批量读取图片模板
来源于参考1
file_path = 'D:\user\user\毕业设计\code_for_hashing\pictures_5_csv\';% 图像文件夹路径
img_path_list = dir(strcat(file_path,'*.png'));%获取该文件夹中所有png格式的图像
img_num = length(img_path_list)%获取图像总数量
I=cell(1,img_num);
if img_num > 0 %有满足条件的图像
for j = 1:img_num %逐一读取图像
image_name = img_path_list(j).name;% 图像名
image = imread(strcat(file_path,image_name));
I{
j}=image;
fprintf('%d %d %s\n',i,j,strcat(file_path,image_name));% 显示正在处理的图像名
%图像处理过程 省略
%这里直接可以访问细胞元数据的方式访问数据
end
end
批量处理多张图片,分别获取4幅伽马校正图像
实验结果
>> load_and_read_image_sequences
img_num =
5
4 1 D:\user\user\毕业设计\code_for_hashing\pictures_5_csv\airplane.png
4 2 D:\user\user\毕业设计\code_for_hashing\pictures_5_csv\baboon.png
4 3 D:\user\user\毕业设计\code_for_hashing\pictures_5_csv\boat.png
4 4 D:\user\user\毕业设计\code_for_hashing\pictures_5_csv\house.png
4 5 D:\user\user\毕业设计\code_for_hashing\pictures_5_csv\peppers.png
实验代码
load_and_read_image_sequences.m
file_path = 'D:\user\user\毕业设计\code_for_hashing\pictures_5_csv\';% 图像文件夹路径
img_path_list = dir(strcat(file_path,'*.png'));%获取该文件夹中所有png格式的图像
img_num = length(img_path_list)%获取图像总数量
I=cell(1,img_num);
if img_num > 0 %有满足条件的图像
for j = 1:img_num %逐一读取图像
image_name = img_path_list(j).name;% 图像名
image = imread(strcat(file_path,image_name));
I{
j}=image;
fprintf('%d %d %s\n',i,j,strcat(file_path,image_name));% 显示正在处理的图像名
%图像处理过程
get_image_sequences_of_gamma_correction(file_path,image_name);
%这里直接可以访问细胞元数据的方式访问数据
end
end
get_image_sequences_of_gamma_correction.m
%----批量处理伽马校正---
function get_image_sequences_of_gamma_correction(path,name)
prefix = name(1:end-4); % 去掉name后面的.png
% I = imread(name);
% figure,imshow(I);
gamma_list = [0.75, 0.9, 1.1, 1.25];
for i = 1:length(gamma_list)
gamma = gamma_list(i);
file_name = [path,name];
s = gammaCorrection(file_name,1,gamma);
str0='D:\user\user\毕业设计\code_for_hashing\matlab_code\test\';
str1= [prefix,'_','gamma=',num2str(gamma)];%字符串拼接
str2 = '.png';
save_path=[str0,str1,str2]; % 字符串拼接
imwrite(s,save_path);
end
end
参考
[1]MATLAB批量读取一个文件夹下的图片
[2]how to load and read image sequences in order in matlab
[3]Gamma correction 伽马校准及 matlab 实现
[4]matlab imwrite写入指定文件夹
转载:https://blog.csdn.net/shizheng_Li/article/details/116760408
查看评论