2018_7_5 自撰imrotate和bwlabel的matlab代码实现

imrotate代码部分:

function Myrotate(I,degree)

[m n]=size(I);
new_m = ceil(abs(m*cosd(degree)) + abs(n*sind(degree)));
new_n = ceil(abs(n*cosd(degree)) + abs(m*sind(degree)));
m1 = [1 0 0; 0 -1 0; -0.5*n 0.5*m 1];
m2 = [cosd(degree) -sind(degree) 0; sind(degree) cosd(degree) 0; 0 0 1];
m3 = [1 0 0; 0 -1 0; 0.5*new_n 0.5*new_m 1];


for i = 1:n
   for j = 1:m
%        加上常数维度,变为3维
      new_coordinate = [i j 1]*m1*m2*m3;
      col = ceil(new_coordinate(1));
      row = ceil(new_coordinate(2));
      new_img_forward(row, col) = I(j, i);
   end
end


imshow(new_img_forward);

end


bwlabel部分:

function SeedFilling(I)

imshow(I);
J=im2uint16(I);
[m,n]=size(J);


for index1=1:m
    for index2=1:n
        if J(index1,index2)> 30000
            J(index1,index2)=10000;
        else J(index1,index2)=0;
        end
    end
end
% first step:
% 标记类栈矩阵
stackMatrix = [];
index=1;
for index1=1:m
    for index2=1:n
        if J(index1,index2)==10000
            index3=index1; %初始化栈
            index4=index2;
            stackMatrix(:,end+1)=[index3;index4];           
            [c v]=size(stackMatrix);
            while c~=0 && v~=0
                %出栈
                %只在出栈的时候才赋值
                index3=stackMatrix(1,end);
                index4=stackMatrix(2,end);
                J(index3,index4)=index;
                stackMatrix(:,end)=[];
                % 4种情况讨论,找到标记值入栈
                if index3>1
                    if J(index3-1,index4)==10000
                        %入栈
                        stackMatrix(1,end+1)=index3-1;
                        stackMatrix(2,end)=index4;
                    end
                end
                if index4>1
                    if J(index3,index4-1)==10000
                        stackMatrix(1,end+1)=index3;
                        stackMatrix(2,end)=index4-1;
                    end
                end
                if index3<m
                    if J(index3+1,index4)==10000
                        %入栈
                        stackMatrix(1,end+1)=index3+1;
                        stackMatrix(2,end)=index4;
                    end
                end
                 if index4<n
                    if J(index3,index4+1)==10000
                        %入栈
                        stackMatrix(1,end+1)=index3;
                        stackMatrix(2,end)=index4+1;
                    end
                 end  
                 [c v]=size(stackMatrix);
            end
            index=index+1;        
        end                               
    end
   
end
H=mat2gray(J);
figure,imshow(H);

end

注:参考过网络,其中imrotate坐标变换参考https://blog.csdn.net/lkj345/article/details/50555870;

       bwlabel参考 https://blog.csdn.net/icvpr/article/details/10259577 


猜你喜欢

转载自blog.csdn.net/weixin_42034505/article/details/80937571
今日推荐