clc; clear all; img_in = imread('ILSVRC2017_test_00000237.jpg'); m = size(img_in,1); n = size(img_in,2); img_lab = rgb2lab(img_in); img_L_mean = mean(mean(img_lab(:,:,1))); img_a_mean = mean(mean(img_lab(:,:,2))); img_b_mean = mean(mean(img_lab(:,:,3))); %高斯滤波 img_R = img_in(:,:,1); img_G = img_in(:,:,2); img_B = img_in(:,:,3); w = fspecial('gaussian',[7 7]); img_R_blur = imfilter(img_R,w); img_G_blur = imfilter(img_G,w); img_B_blur = imfilter(img_B,w); img_blur = cat(3,img_R_blur,img_G_blur,img_B_blur); figure('Name','滤波'); imshow(img_blur); img_lab_blur = rgb2lab(img_blur); %计算显著图 Sd = zeros(m,n); for i = 1:m for j = 1:n Sd(i,j) = sqrt((img_L_mean - img_lab_blur(i,j,1))^2 + (img_a_mean - img_lab_blur(i,j,2))^2 + (img_b_mean - img_lab_blur(i,j,3))^2); end end %归一化 Sd_normalized = figure_normalize(Sd); imwrite(Sd_normalized,'FT_saliency.jpg'); figure; imshow(Sd_normalized); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function Out_image = figure_normalize(In_image) o_max_image = max(max(In_image)); o_min_image = min(min(In_image)); Out_image = double(In_image - o_min_image)/double(o_max_image - o_min_image); end运行该代码报错未定义函数或变量 'figure_normalize'。
时间: 2023-06-14 09:04:22 浏览: 89
这个错误是因为你使用了一个名为`figure_normalize`的函数,但是你的代码中并没有定义这个函数。你需要自己编写这个函数并将其添加到代码中。
下面是一个可能的`figure_normalize`函数的示例,你可以将其添加到你的代码中:
```
function Out_image = figure_normalize(In_image)
o_max_image = max(max(In_image));
o_min_image = min(min(In_image));
Out_image = double(In_image - o_min_image)/double(o_max_image - o_min_image);
end
```
注意:如果你使用的是 MATLAB 的版本较新,可能会有现成的函数可以实现这个功能,例如`imadjust`函数。你可以查看 MATLAB 的文档进行了解。
相关问题
clc clear all %% img_in = imread('ILSVRC2017_test_00000237.jpg'); m = size(img_in,1); n = size(img_in,2); img_lab = rgb2lab(img_in); img_L_mean = mean(mean(img_lab(:,:,1))); img_a_mean = mean(mean(img_lab(:,:,2))); img_b_mean = mean(mean(img_lab(:,:,3))); %% %高斯滤波 img_R = img_in(:,:,1); img_G = img_in(:,:,2); img_B = img_in(:,:,3); w = fspecial('gaussian',[7 7]); img_R_blur = imfilter(img_R,w); img_G_blur = imfilter(img_G,w); img_B_blur = imfilter(img_B,w); img_blur = cat(3,img_R_blur,img_G_blur,img_B_blur); figure('name','滤波') imshow(img_blur) img_lab_blur = rgb2lab(img_blur); %% %计算显著图 Sd = zeros(m,n); for i = 1:m for j = 1:n Sd(i,j) = sqrt((img_L_mean - img_lab_blur(i,j,1))^2 + (img_a_mean - img_lab_blur(i,j,2))^2 + (img_b_mean - img_lab_blur(i,j,3))^2); end end %归一化 Sd_normalized = figure_normalize(Sd); imwrite(Sd_normalized,'FT_saliency.jpg') figure imshow(Sd_normalized) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function Out_image = figure_normalize(In_image) % 归一化至0-1 o_max_image = max(max(In_image)); o_min_image = min(min(In_image)); Out_image = double(In_image - o_min_image)/double(o_max_image - o_min_image); end改进该代码使其能在matlab上运行
clc;
clear all;
img_in = imread('ILSVRC2017_test_00000237.jpg');
m = size(img_in,1);
n = size(img_in,2);
img_lab = rgb2lab(img_in);
img_L_mean = mean(mean(img_lab(:,:,1)));
img_a_mean = mean(mean(img_lab(:,:,2)));
img_b_mean = mean(mean(img_lab(:,:,3)));
%高斯滤波
img_R = img_in(:,:,1);
img_G = img_in(:,:,2);
img_B = img_in(:,:,3);
w = fspecial('gaussian',[7 7]);
img_R_blur = imfilter(img_R,w);
img_G_blur = imfilter(img_G,w);
img_B_blur = imfilter(img_B,w);
img_blur = cat(3,img_R_blur,img_G_blur,img_B_blur);
figure('Name','滤波');
imshow(img_blur);
img_lab_blur = rgb2lab(img_blur);
%计算显著图
Sd = zeros(m,n);
for i = 1:m
for j = 1:n
Sd(i,j) = sqrt((img_L_mean - img_lab_blur(i,j,1))^2 + (img_a_mean - img_lab_blur(i,j,2))^2 + (img_b_mean - img_lab_blur(i,j,3))^2);
end
end
%归一化
Sd_normalized = figure_normalize(Sd);
imwrite(Sd_normalized,'FT_saliency.jpg');
figure;
imshow(Sd_normalized);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Out_image = figure_normalize(In_image)
o_max_image = max(max(In_image));
o_min_image = min(min(In_image));
Out_image = double(In_image - o_min_image)/double(o_max_image - o_min_image);
end
clear clc pathname = uigetdir; name_list=dir(pathname); for i=3:22 name_list(i).num=zeros(20,1); name_list(i).num(i-2)=1; end %pathname = uigetdir; img_name1=importdata('train_30_32.txt'); img_train_num=size(img_name1,1); num=0; for i=1:img_train_num img_name_char=cell2mat(img_name1(i)); label_1(i).name=extractBefore(cell2mat(img_name1(i)),'_'); imgPath = [pathname,'\',label_1(i).name,'\',cell2mat(img_name1(i))]; temp = imread(imgPath); train_x(:,:,i)=temp; % temp = double(temp(:)); % num=num+1; % imagedata1(:,num)=temp; end for i=1:img_train_num for j=1:20 if strcmp(name_list(j+2).name,label_1(i).name) label_1(i).num=name_list(j+2).num; end end train_y(:,i)=uint8(label_1(i).num); end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %pathname = uigetdir; img_name1=importdata('test_30_32.txt'); img_test_num=size(img_name1,1); num=0; for i=1:img_test_num img_name_char=cell2mat(img_name1(i)); label_2(i).name=extractBefore(cell2mat(img_name1(i)),'_'); imgPath = [pathname,'\',label_2(i).name,'\',cell2mat(img_name1(i))]; temp = imread(imgPath); test_x(:,:,i)=temp; % temp = double(temp(:)); % num=num+1; % imagedata2(:,num)=temp; end for i=1:img_test_num for j=1:20 if strcmp(name_list(j+2).name,label_2(i).name) label_2(i).num=name_list(j+2).num; end end test_y(:,i)=uint8(label_2(i).num); end save('E:\\imgdata_uint8.mat', 'train_x','train_y','test_x','test_y');逐行解释
这段代码的作用是读取指定目录下的图像文件,将其转换为数据集用于深度学习训练。
首先,使用 `uigetdir` 函数让用户选择一个目录,然后使用 `dir` 函数获取该目录下所有文件的信息,存储在 `name_list` 数组中。接着,使用循环遍历前 20 个文件夹(`name_list` 中的前两个元素为 `.` 和 `..`),并为每个文件夹创建一个标签向量 `num`,其中第 `i-2` 个元素为 1,其余为 0,表示该文件夹对应的标签。
接下来,读取训练集图像文件。首先从一个文件中读取所有图像文件名,然后使用 `imread` 函数读取每个图像文件,并将其存储在 `train_x` 数组中。同时,将每个图像文件的标签与对应的标签向量 `num` 进行匹配,并将结果存储在 `train_y` 数组中。这样,`train_x` 和 `train_y` 就构成了训练集。
最后,读取测试集图像文件,方法与读取训练集图像文件类似。将读取得到的图像文件和标签信息存储在 `test_x` 和 `test_y` 数组中,构成测试集。最后,将训练集和测试集数据保存在 `imgdata_uint8.mat` 文件中。
需要注意的是,这段代码仅适用于特定的图像数据集,如果要将其应用于其他数据集,需要进行相应的修改。
阅读全文