Overcome for loops for fast processing

1 回表示 (過去 30 日間)
Muhammad Farhan  Mughal
Muhammad Farhan Mughal 2018 年 11 月 11 日
I have the following part of MATLAB code
skin_rgb = zeros(256.^3,1);
for i = [2000 : 2003,2008,2010:2011,2013:2015,2017:2018,2021,2024,2027:2041,2043,2045:2059,2061:2063]
I = imread(['skin_img',num2str(i),'.tif']);
[x,y,~]= size(I);
for l = 1:x
for j = 1:y
if (I(l,j,1) ~= 255 && I(l,j,2) ~= 255 && I(l,j,3) ~= 255)
aa = double([I(l,j,1) , I(l,j,2) , I(l,j,3)]);
[~,ee1] = ismember(aa,rgb_hist,'rows');
skin_rgb(ee1) = skin_rgb(ee1) + 1;
b1 = b1 + 1;
end
end
end
end
The output of the code is perfect. The issue is it is very slow. Inside the if loop the size of rgb_hist variable is 255.^3 x 3. Is there any way to get rid of the two nested for loops? As I am doing Image processing on high resolution image the variable x and y (different for each "i" iteration) have a big number in it.
Thank you
  2 件のコメント
Walter Roberson
Walter Roberson 2018 年 11 月 11 日
Is every possible combination in rgbhist?
Muhammad Farhan  Mughal
Muhammad Farhan Mughal 2018 年 11 月 11 日
Yes, every possible combination is in rgb_hist.

サインインしてコメントする。

採用された回答

Bruno Luong
Bruno Luong 2018 年 11 月 11 日
編集済み: Bruno Luong 2018 年 11 月 14 日
skin_rgb = 0;
for i = [2000 : 2003,2008,2010:2011,2013:2015,2017:2018,2021,2024,2027:2041,2043,2045:2059,2061:2063]
I = imread(['skin_img',num2str(i),'.tif']);
RGB = reshape(double(I),[],3)+1;
skin_rgb = skin_rgb + accumarray(RGB,1,256+[0 0 0]);
end
skin_rgb = permute(skin_rgb,[3 2 1]);
skin_rgb = skin_rgb(:);
  1 件のコメント
Muhammad Farhan  Mughal
Muhammad Farhan Mughal 2018 年 11 月 12 日
Thanks a lot.

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2018 年 11 月 11 日
skin_rgb = zeros(256.^3,1);
for i = [2000 : 2003, 2008, 2010:2011, 2013:2015, 2017:2018, 2021, 2024, 2027:2041, 2043, 2045:2059, 2061:2063]
I = imread(['skin_img',num2str(i),'.tif']);
r = 1 + double(I(:,:,1)); g = 1 + double(I(:,:,2)); b = 1 + double(I(:,:,3));
skin_rgb = skin_rgb + accumarray([r(:), g(:), b(:)], 1, [256.^3, 1]);
end
  15 件のコメント
Bruno Luong
Bruno Luong 2018 年 11 月 14 日
編集済み: Bruno Luong 2018 年 11 月 14 日
Then your rgb_hist is wrong.
This test code returns 1 everytime
% Such simple generate thing that you never bother to post
[B,G,R] = ndgrid(1:256);
rgb_hist = [R(:),G(:),B(:)];
% Quick check for order
rgb_hist(1:10,:)
rgb_hist(end+(-9:0),:)
% Random data
I = floor(256*rand(100,100,3));
aa = 1 + [I(100,100,1),I(100,100,2),I(100,100,3)]
% Counting algo
skin_rgb = accumarray(aa,1,256+[0 0 0]);
skin_rgb = permute(skin_rgb,[3 2 1]);
skin_rgb = skin_rgb(:);
% Check
[a1,a2] = ismember(aa,rgb_hist,'rows');
skin_rgb(a2) % <- This returns correctly 1
Muhammad Farhan  Mughal
Muhammad Farhan Mughal 2018 年 11 月 14 日
Thanks a lot

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeImages についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by