📄 sample_genmodel_fhedgecolsw.m
字号:
function segsAll = sample_genmodel_FHedgecolsW(im,genmodel,NUM_SAMPLES)%segsAll = sample_genmodel_FHedgecolsW(im,genmodel,[NUM_SAMPLES = 0])%Parses an image, returning an array of people segments%First element in the array is the best-scoring poseif nargin < 3, NUM_SAMPLES = 0;endBEST = 1;[imy imx imz] = size(im);numClass = max(genmodel.equiv_class);numTypes = size(genmodel.dag,1);imo = size(genmodel.orient,2);respIm = zeros([imy imx imo numTypes]);assert(numClass == numTypes);lens = genmodel.len*2; wids = genmodel.wid*2;%Convert orient to exp scale (and normalize)genmodel.orient = exp(genmodel.orient);genmodel.orient = genmodel.orient./repmat(sum(genmodel.orient,2),1,imo);%genmodel.orient(1,:) = 0; genmodel.orient(1,1) = 1;%genmodel.orient(2,:) = 0; genmodel.orient(2,19) = 1;%Flip the non-root ones since we'll be going convolution%genmodel.orient = fliplr(genmodel.orient);%genmodel.orient(genmodel.root,:) = fliplr(genmodel.orient(genmodel.root,:));%genmodel.orient(2,:) = 1;%Get out edge and color histogram map%[m,t] = mydetGMc(im,2); m = double(imdilate(m > 10,ones(3)));%imHist = imvq16(im);%[dummy,dummy,imHist] = unique(imHist);DEBUG = 0;if DEBUG, [m,t] = mydetGMc(im,2); m = double(imdilate(m > 10/255,ones(3))); imHist = im+1;else [m,t] = mydetGMc(im,2); m = double(imdilate(m > 10,ones(3))); imHist = imvq16(im); [dummy,dummy,imHist] = unique(imHist);endLOG_MAX = 500;LOG_MIN = -500;pNN = zeros(numTypes,1);%origWW = zeros(size(respIm));for p = 1:numTypes, ww = genmodel.ww(:,:,p); mask = imresize(ww,2*[genmodel.len(p) 2*genmodel.wid(p)],'nearest'); if any(mask(:)), mask = mask*(sum(abs(ww(:)))/sum(abs(mask(:)))); end resp = getSegmentsEdge(m,mask); %Treat this as a feature that is weighted by genmodel.www %origWW(:,:,:,p) = resp; resp = resp*genmodel.www(p); %Now take the color ww = genmodel.wc(:,:,p); mask = imresize(ww,2*[genmodel.len(p) 2*genmodel.wid(p)],'nearest'); if any(mask(:)), mask = mask*(sum(abs(ww(:)))/sum(abs(mask(:)))); end fgP = reshape(genmodel.fgP(imHist,find(genmodel.colmask(:,p))),imy,imx); resp = resp + getSegmentsEdge(fgP,mask); resp = partshiftZ0(resp,genmodel.len(p),0); %Shift so rectangles are anchored at the top resp(resp > LOG_MAX) = LOG_MAX; resp(resp < LOG_MIN) = LOG_MIN; resp = exp(resp); NN = sum(resp(:)); pNN(p) = log(NN); respIm(:,:,:,p) = resp/NN;end%keyboard;%tmp = respIm(:,:,:,2); [dummy,I] = max(tmp(:)); [y,x,a] = ind2sub([imy imx imo],I);%respIm(:,:,:,p) = 0; respIm(100,40,24,p) = 1;%respImOrig = respIm;%imagesc(sum(respIm(:,:,:,p),3))mean_y = round(genmodel.mu(:,2));mean_x = round(genmodel.mu(:,1));for p = genmodel.post(1:end-1), %Multiply in all the messages from the kids to the current respIm parType = genmodel.parents{p}; %kids = find(genmodel.dag(p,:)); %We sum over all orientations %currResp = sum(prod(respIm(:,:,:,[p kids]),4),3); %currResp = sum(respIm(:,:,:,p),3); currResp = reshape(respIm(:,:,:,p),imy*imx,imo); %Reflect borders %currResp = cat(2,currResp(:,imo/2:(end-1)),currResp,currResp(:,2:imo/2)); currResp = cat(2,currResp(:,(imo/2+1):end),currResp,currResp(:,1:(imo/2-1))); %Convolve currResp = conv2(currResp,fliplr(genmodel.orient(p,:)),'valid'); currResp = reshape(currResp,[imy imx imo]); %Smooth it %std = sqrt(var(p)); %kernal = exp(-1/(2*var(p))*[-3*std:3*std].^2); kernal = kernal/sum(kernal); %currResp = conv2(kernal',kernal,currResp,'same'); currResp = local_sum_zero(currResp,genmodel.box(p)*2); %Shift it currResp = partshiftZ0(currResp,mean_y(p),mean_x(p)); %Normalize %currResp = currResp/(sum(currResp(:)) + eps); %Pass message to the parent respIm(:,:,:,parType) = respIm(:,:,:,parType).*currResp;end%Ignore root position and orientationp = genmodel.root;currResp = respIm(:,:,:,p);%For now, zero out upside down roots%currResp(:,:,[7:19]) = 0;%Add in root potentialcurrResp = currResp.*repmat(permute(genmodel.orient(p,:),[1 3 2]),[imy imx 1]);%currResp(:,:,[1:5 19:24]) = 0;%currResp(:,:,[1:11 13:24]) = 0;%Debugging%Save the normalizing constantNN = sum(currResp(:));respIm(:,:,:,p) = currResp/NN;NN = log(NN) + sum(pNN);%Pass messages down%expOrient = zeros(size(genmodel.orient));%expOrient(p,:) = sum(reshape(currResp,imy*imx,imo));%expOrient(p,:) = expOrient(p,:)/sum(expOrient(p,:));%Define downstream orientations as walking counter-clockwise starting %from 13, pointing down%ie [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24]%-> [1 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2]%genmodel.orient = genmodel.orient(:,[1 end:-1:2]);%Store the probabilitiesrespImOrig = respIm;segsAll = cell(NUM_SAMPLES+1,1);TEMP = 1;for big_iter = 1:NUM_SAMPLES+1, respIm = respImOrig; p = 1; currResp = respIm(:,:,:,p); if big_iter == 1, BEST = 1; else BEST = 0; end if BEST, [val,ii] = max(currResp(:)); else %Sample ii = find(sampleWithR(currResp(:).^TEMP,2),1); end currResp(:) = 0; currResp(ii) = 1; respIm(:,:,:,p) = currResp; for p = genmodel.pre(2:end), parType = genmodel.parents{p}; currResp = respIm(:,:,:,parType); currResp = partshiftZ0(currResp,-mean_y(p),-mean_x(p)); currResp = reshape(currResp,imy*imx,imo); %Now do normal message passing %Circularly convolve %currResp = cat(2,currResp(:,imo/2:end-1),currResp,currResp(:,2:imo/2)); currResp = cat(2,currResp(:,(imo/2+1):end),currResp,currResp(:,1:(imo/2-1))); %Shift so previous center (13) is now at center(1) currOrient = genmodel.orient(p,[1 end:-1:2]); currResp = conv2(currResp,fliplr(currOrient),'valid'); %sum(currResp); %Useful debugging currResp = reshape(currResp,[imy imx imo]); currResp = local_sum_zero(currResp,genmodel.box(p)*2); %Pass it equally to all rotated copies currResp = respIm(:,:,:,p).*currResp; if BEST, [val,ii] = max(currResp(:)); else %Sample ii = find(sampleWithR(currResp(:).^TEMP,2),1); end currResp(:) = 0; currResp(ii) = 1; respIm(:,:,:,p) = currResp; %Save the marginal respIm(:,:,:,p) = currResp./(sum(currResp(:))+eps);end%Shift the respIm to align with center of segment[Y,X,I] = deal(zeros(numTypes,1));for p = 1:numTypes, currResp = partshiftZ0(respIm(:,:,:,p),-genmodel.len(p),0); currResp = currResp./sum(currResp(:)); [val,ii] = max(currResp(:)); [Y(p),X(p),I(p)] = ind2sub(size(currResp),ii);end%Convert to segments[uu,vv] = pol2cart(([15:15:360] + 90 - 15 + 180) * pi/180,1);vv = -vv;seg.u = uu(I)';seg.v = vv(I)';seg.len = genmodel.len;seg.w = genmodel.wid;seg.x = X;seg.y = Y;seg.type = [1:numTypes]';segsAll{big_iter} = seg;end%showsegIm(im,segsAll{1});
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -