Guest User

Untitled

a guest
May 19th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scilab 2.57 KB | None | 0 0
  1. function [Out]=RGB2HSV(In)
  2.     [h,w,d]=size(In);
  3.     HSV=zeros(h,w,d);
  4.    
  5.     for i=1:h
  6.         for j=1:w
  7.             RGBmax=double(max(In(i,j,:)));
  8.             RGBmin=double(min(In(i,j,:)));
  9.            
  10.             HSV(i,j,3)=RGBmax; // WYZNACZAMY V
  11.            
  12.             if HSV(i,j,3)==0 then
  13.                 HSV(i,j,1)=0; // jesli V=0
  14.                 HSV(i,j,2)=0; // to H=0 i S=0
  15.             else
  16.                 R=double(In(i,j,1))/HSV(i,j,3);
  17.                 G=double(In(i,j,2))/HSV(i,j,3);
  18.                 B=double(In(i,j,3))/HSV(i,j,3);
  19.                
  20.                 RGBmax=max([R G B]);
  21.                 RGBmin=min([R G B]);
  22.                
  23.                 HSV(i,j,2)=RGBmax-RGBmin; // WYZNACZAMY S
  24.                
  25.                 if HSV(i,j,2)==0 then
  26.                    HSV(i,j,1)=0; // jesli S=0, to H=0
  27.                 else
  28.                     R=(R-RGBmin)/(RGBmax-RGBmin); // wykonujemy
  29.                     G=(G-RGBmin)/(RGBmax-RGBmin); // normalizacje
  30.                     B=(B-RGBmin)/(RGBmax-RGBmin); // skladowych RGB
  31.                    
  32.                     RGBmax=max([R G B]);
  33.                     RGBmin=min([R G B]);
  34.                    
  35.                     if RGBmax==R then // WYZNACZAMY SKLADOWA H
  36.                         HSV(i,j,1)=0+60*(G-B);
  37.                        
  38.                         if HSV(i,j,1)<0 then
  39.                             HSV(i,j,1)=HSV(i,j,1)+360;
  40.                         end
  41.                     elseif RGBmax==G
  42.                         HSV(i,j,1)=120+60*(B-R);
  43.                     else
  44.                         HSV(i,j,1)=240+60*(R-G);
  45.                     end
  46.                 end
  47.             end
  48.         end
  49.        
  50.         printf("%d\n", double(i/h)*100);
  51.     end
  52.    
  53.     Out=HSV;
  54. endfunction
  55.  
  56. function [Out]=negatyw(In)    
  57.     for i=1:256
  58.         LUT(i)=256-i;
  59.     end
  60.    
  61.     In(:,:)=LUT(In(:,:));  
  62.     Out=In;
  63. endfunction
  64.  
  65. global IPD_PATH;
  66.  
  67. RGB=ReadImage(IPD_PATH + 'demos\teaset.png');
  68.  
  69. //ShowColorImage(RGB, "Obraz");
  70.  
  71. //R=RGB(:,:,1);
  72. //G=RGB(:,:,2);
  73. //nG=negatyw(G);
  74. //B=RGB(:,:,3);
  75. //nB=negatyw(B);
  76. //
  77. //R=double(R)/256;
  78. //nG=double(nG)/256;
  79. //nB=double(nB)/256;
  80. //
  81. //M=R.*nG.*nB;
  82. //M=uint8(M*256);
  83. //
  84. //M=SegmentByThreshold(M,80);
  85.  
  86. //figure();
  87. //ShowImage(R, "czerwona");
  88. //figure();
  89. //ShowImage(G, "zielona");
  90. //figure();
  91. //ShowImage(B, "niebieska");
  92.  
  93. //ShowImage(M, "maska zlozenia");
  94.  
  95. tempRGB=zeros(1,1,3); // obrazek testowy - 1 piksel RGB
  96. tempRGB(1,1,1)=double(0.4392157);
  97. tempRGB(1,1,2)=double(0.6745098);
  98. tempRGB(1,1,3)=double(0.71372549);
  99.  
  100. M=RGB2HSV(tempRGB);
Add Comment
Please, Sign In to add comment