Matlab keyed drawing

Source: Internet
Author: User

Http://blog.sina.com.cn/s/blog_5dd2e9270100xdcp.html

Code can't run, don't know what's going on

Poison matting MATLAB Code Note: After the image is displayed, click the left mouse button to connect the full background area, double-click the end of the left button, and then click the left mouse button to connect the full foreground area, and then left to end. Results can be obtained. A=imread (' ppmm2.bmp '); Imshow (A); hold on; Threshold = 1; Uniback=[0 0 255];% Choose Background object, and B results into the Foreground object and the% boundary region% Choo Se the outer line of the boundary region. Single Left click the mouse to specify Vertice. Double left click or Single right click to FINISH;OUTERBW = Roipoly (A); B (:,:, 1) = Immultiply (A (:,:, 1), OUTERBW); B (:,:, 2) = Immultiply (A (:,:, 2), OUTERBW); B (:,:, 3) = Immultiply (A (:,:, 3), OUTERBW); Back (:,:, 1) = Immultiply (A (:,:, 1), ~OUTERBW); Back (:,:, 2) = Immultiply (A (:,:, 2), ~OUTERBW);  Back (:,:, 3) = Immultiply (A (:,:, 3), ~OUTERBW); Imshow (B);% choose foreground object, and C results into A Circle of interest% Choose the inner line of the boundary region. Single Left click the mouse to specify Vertice. Double left click or Single right click to FINISH;INNERBW = Roipoly (B); C (:,:, 1) = Immultiply (B (:,:, 1), ~INNERBW); C (:,:, 2) = Immultiply (B (:,:, 2), ~INNERBW); C (:,:, 3) = Immultiply (B (:,:, 3), ~INNERBW); Fore (:,:, 1) = Immultiply (A (:,:, 1), INNERBW); Fore (:,:, 2) = Immultiply (A (:,:, 2), INNERBW); Fore (:,:, 3) = Immultiply (A (:,:, 3), INNERBW); Imshow (C); hold off; Rawalpha = (double (OUTERBW) +double (INNERBW))/2;i = double (A);% Method of "averaging" to get raw fore and background colors                              For i = 1:size (rawalpha,1) for j = 1:size (rawalpha,2) rawfore (i,j,1) = 0;         Rawfore (i,j,2) = 0;         Rawfore (i,j,3) = 0;                              Rawback (i,j,1) = 0;         Rawback (i,j,2) = 0;         Rawback (i,j,3) = 0;         If Rawalpha (i,j) = = 1 Rawfore (i,j,:) =fore (i,j,:);         End         If Rawalpha (i,j) = = 0 Rawback (i,j,:) =back (i,j,:);         End             If Rawalpha (i,j) = = 0.5 R=1;                 While 1 testfore = INNERBW (max (i-r,1): Min (i+r,size (a,1)), Max (j-r,1): Min (j+r,size (a,2))); If size (find (Testfore), 1) [I1, J1] = find (Testfore);                     I2 = I1 + Max (i-r,1)-1;                     J2 = J1 + Max (j-r,1)-1;                     Rs = Double (Fore (i2,j2,:));                     Ds (:, 1) =diag (Rs (:,:, 1));                     Ds (:, 2) =diag (Rs (:,:, 2));                                      Ds (:, 3) =diag (Rs (:,:, 3));                     If size (ds,1) = = 1 Rawfore (i,j,:) =ds;                     else Rawfore (i,j,:) = SUM (Ds)/size (ds,1);                     End                     Clear Ds;                 Break                 else r=r+1;             End             End             R=1;                 While 1 testback = ~OUTERBW (max (i-r,1): Min (i+r,size (a,1)), Max (j-r,1): Min (j+r,size (a,2)));                     If size (find (Testback), 1) [I1, J1] = find (Testback);                     I2 = I1 + Max (i-r,1)-1;                     J2 = J1 + Max (j-r,1)-1;         Rs = Double (Back (i2,j2,:));            Ds (:, 1) =diag (Rs (:,:, 1));                     Ds (:, 2) =diag (Rs (:,:, 2));                                      Ds (:, 3) =diag (Rs (:,:, 3));                     If size (ds,1) = = 1 Rawback (i,j,:) =ds;                     else Rawback (i,j,:) = SUM (Ds)/size (ds,1);                     End                     Clear Ds;                 Break                 else r=r+1;             End         End     End End end;% There is no Gaussian filter Denorm = rawfore-rawback;%red channelI1 = I (:,:, 1);D Enorm1 = Denorm (:,:, 1); for I=1:size (denorm1,1) for        j= 1:size (denorm1,2) if Denorm1 (i,j) ==0 Denorm1 (i,j) = 1;    End End;end;oldalpha = Rawalpha; Newalpha = rawalpha;h1=0;while 1 for i=1:size (oldalpha,1) for J=1:size (oldalpha,2) newalpha (i,j) = Ol            Dalpha (I,J); If Rawalpha (i,j) = = 0.5 Roui = ((I1 (i+1,j) + I1 (I-1,J)-2 * I1 (I,J)) * DENORM1 (I,J)-(I1 (i+1), j)-I1 (I,J)) * (Denorm1 (I+1,J)-Denorm1 (I,J))/(Denorm1 (I,J) * DENORM1 (I,J)); Rouj = ((I1 (i,j+1) + I1 (i,j-1)-2 * I1 (I,J)) * DENORM1 (I,J)-(I1 (i,j+1)-I1 (I,J)) * (Denorm1 (i,j+1)-Denorm1 (I,J))/(D                Enorm1 (i,j) * DENORM1 (I,J));                Rou = Roui + Rouj;                Newalpha (i,j) = (Oldalpha (i+1,j) + Newalpha (i-1,j) + Oldalpha (i,j+1) + Newalpha (i,j-1)-Rou)/4;                If Newalpha (i,j) <0 newalpha (i,j) = 0;                End                If Newalpha (i,j) >1 newalpha (i,j) = 1;            End        End    End   End    % Imshow (uint8 (newalpha*255));    Differencealpha = ABS (Newalpha-oldalpha);    Oldalpha = Newalpha;    If SUM (SUM (differencealpha)) < Threshold break;     End        H1=h1+1;end;for i=1:size (a,1) for j=1:size (a,2) if Oldalpha (i,j) ==0 newimage (i,j,:) =uniback '; else NewImage (i,j,1) =uniback (1) * (1-oldalpha (I,J)) +rawfore (i,j,1) *oldalpha(I,J);            NewImage (i,j,2) =uniback (2) * (1-oldalpha (I,J)) +rawfore (i,j,2) *oldalpha (I,J);        NewImage (i,j,3) =uniback (3) * (1-oldalpha (I,J)) +rawfore (i,j,3) *oldalpha (I,J);    End End;end;figure,imshow (Newalpha); Figure,imshow (Uint8 (newimage)); Figure,imshow (Uint8 (Rawfore)); Figure,imshow ( Uint8 (Rawback));

  

Matlab keyed drawing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.