当前位置:网站首页>An error prone to appear when MATLAB is doing image processing: to improve the operation speed, use the pre declared zero matrix to store image data

An error prone to appear when MATLAB is doing image processing: to improve the operation speed, use the pre declared zero matrix to store image data

2022-06-21 22:36:00 Jida qinshaoyou

MATLAB A mistake that is easy to appear in image processing : In order to improve the operation speed, a pre declared zero matrix is used to store image data .

This misunderstanding is very hidden , Because such a pre declared zero matrix can significantly improve the operation speed , So they often think it is the right way . But to ensure that the image data looks normal , It is often necessary to use im2double Function to convert the input image data into double Type of matrix , such imshow The resulting image looks normal , In fact, the accuracy of image data has changed due to format conversion , It is easy to report errors when processing data .

as follows : The code in the annotation section is easy to cause changes in the image data .

src_img = strcat(img_path, img_name);
pure_name = size(img_name,2);
pic_odd_name = strcat(img_name(1:pure_name-4), '_odd.bmp');
pic_even_name = strcat(img_name(1:pure_name-4), '_even.bmp');
pic = imread(src_img);
% pic = im2double(pic);
[pic_height, pic_width, rgb_depth] = size(pic);
% pic_odd = zeros([pic_height, pic_width/2, rgb_depth]);
% pic_even = zeros([pic_height, pic_width/2, rgb_depth]);

for i = 1:1:pic_height
    for j = 1:1:pic_width
%         for k = 1:1:rgb_depth
            if(mod(j,2) == 1)   % even pixel
                 pic_even(i,(j+1)/2,:) = pic(i,j,:);
            else
                 pic_odd(i,j/2,:) = pic(i,j,:);
            end
%         end
    end
end

therefore , It can be handled directly , No need to declare the size of storage space in advance , Slow is slow . Another method is to observe the type of image data after the image data is read into the work area , Then declare the same type of zero matrix , This approach is also feasible in theory , Compared to not declaring storage space , It can be much faster when processing a large number of images .

原网站

版权声明
本文为[Jida qinshaoyou]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206212039544373.html