Determine a regions average in MATLAB

后端 未结 2 752
面向向阳花
面向向阳花 2021-01-24 16:21

I need some help with RGB capture in a image. I am using impixel to manualy get RGB from a picture, but i would like to create a grid of let\'s say 20x20 px boxes where it will

2条回答
  •  猫巷女王i
    2021-01-24 16:59

    This is my complete code for automatic color grabing from pictures in folder. So the program asks you to chose a folder and after that you get a table full of information about color and roundess. I am using this code to get color from fruits that have white background . It does everything by itself. Hope it helps someone.

    clear all;
    clc;
    
    
    uiwait(msgbox('Chose the folder where your pictures are kept. Click OK to continue..'));
    
    % Opening the folder
    
    folder = uigetdir(pwd); 
    filePattern = fullfile(folder, '*.jpg');
    jpegFiles = dir(filePattern);
        for k = 1:length(jpegFiles)
            baseFileName = jpegFiles(k).name;
            fullFileName = fullfile(folder, baseFileName);
            [pathstr, name, ext] = fileparts(fullFileName);
            naziv_voca=name;
    
            %Taking RGB color
    
            slika = imread(fullFileName);
            [redovi stupci RGBboje] = size(slika);
            red_ink = floor(redovi/10);
            stup_ink = floor(stupci/10);
            r = 1;
            c = 1;
            for stupac = 1 : stup_ink  : stupci
                for red = 1 : red_ink : redovi
                red1 = red;
                red2 = red1 + red_ink;
                stupac1 = stupac;
                stupac2 = stupac1 + stup_ink;
                red2 = min(red2, redovi);
                stupac2 = min(stupac2, stupci);
                crveniS = slika(red1:red2, stupac1:stupac2, 1);
                zeleniS = slika(red1:red2, stupac1:stupac2, 2);
                plaviS = slika(red1:red2, stupac1:stupac2, 3);
                crvena(r,c) = mean2(crveniS);
                zelena(r,c) = mean2(zeleniS);
                plava(r,c) = mean2(plaviS);
                r = r + 1;
                    if r >redovi
                        r = 1;
                    end
                end
                c = c + 1;
                    if c >1
                        c = 1;
                    end
                end
    
            RGB=[crvena,zelena,plava];
            bijela=[255 255 255];
            tolerancija = 50;
            rez = RGB((abs(RGB(:,1)-bijela(1)) > tolerancija) | (abs(RGB(:,2)-bijela(2)) > tolerancija),:);
            trez=round(rez); 
    
    
            %Taking shape
    
            pic = rgb2gray(slika);
            threshold = graythresh(pic);
            bw = im2bw(pic,threshold);
            fbw = ones(size(bw))-imfill(ones(size(bw))-bw); 
            invImg = ~fbw;
            f = bwlabel(invImg);
            S = regionprops(f,'Area','Perimeter','centroid');
            Thr=100;
            S=S([S.Area]>Thr);
            score = (min(sqrt([S.Area]),[S.Perimeter]/4)./(max(sqrt([S.Area]), [S.Perimeter]/4))).^2;
            score=max(score);
    
            %Inserting data into table and creating data
    
            if exist('tablica.mat','file')
                vel=size(trez,1);
                for z=1:vel
                    s=load('tablica');
                    olddata=s.data;
                    temp=trez(z,:);
                    dataCell= [naziv_voca,num2cell(temp),num2cell(score)]; 
                    data=[olddata;dataCell];
                    save('tablica.mat','-append','data');
                end
            else
                stupac_rgb = num2cell(trez,1);
                zstupac = cellfun(@sum,stupac_rgb); 
                zred = size(trez,1);           
                rez = bsxfun(@rdivide,zstupac,zred); 
                trez=round(rez);
                data= [naziv_voca,num2cell(trez),num2cell(score)];
                save('tablica','data')
            end 
    
        end  
        uiwait(msgbox('Your information is saved'));
    

提交回复
热议问题