Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Matlab - identify object and highlight

Options
  • 28-11-2011 3:05pm
    #1
    Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,092 Mod ✭✭✭✭


    Trying to find and highlight the component on the left, any ideas on the best method?

    Here is the image in greyscale, have it processed to black and white also.


    ux8q1.gif

    So I must somehow figure out how it is different than other objects so that I can highlight it alone. Can only highlight everything at the moment!


Comments

  • Registered Users Posts: 413 ✭✭ianhobo


    Well the first obvious difference is that the transistor is the only circle. It also seems to have a somewhat defined edge.

    If you threshold the image you should be left with a well defined circle with very thin legs protruding.
    If you thin the objects by only a few pixels, you should be left with a complete circle to identify and highlight.


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,092 Mod ✭✭✭✭Tar.Aldarion


    Thanks! Will try that. Is identifying a circle a trivial thing? Reason I didnt try that is the image was very messy with shadows and it wasn't a circle, so will have to try and remove the shadows somehow and then try your idea.

    what I was trying to do was isolate the object with 8 bays but am having problems with it hmm. especially trying to make it robust to the image being rotated. This is because I found all the connected components, 4 of them, and then hardcoded in which item to look at (which i can not do in practice due to rotation etc).

    maybe if i had a for loop to look at each connected component (object), and an if loop to check which has 8 bays, and to focus on that object then...if that is possible.

    Then I am unsure of how to highlight just one and not the others, as I can get the centroid of them all and overlay it, but not just one. Will keep working on that.


  • Registered Users Posts: 413 ✭✭ianhobo


    no, identifying a circle is not trivial.

    I'm not too sure what platform you are using (I think you said in your last post it was Matlab? If so, what matlab toolkits are you using? I imagine you have at least the ImageProcessingToolkit?)


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,092 Mod ✭✭✭✭Tar.Aldarion


    Have that, the main one is called VSG.


  • Registered Users Posts: 586 ✭✭✭Aswerty


    Not too au fait on machine vision but I think you're on the right track looking at bays I would say.

    An approach I would take might be:

    1. Threshold the image and get the components in white and the background in black.
    2. Isolate each component in separate images.
    3. Whittle the components down to their skeleton forms (can't remember the specific technique that does this) such that the component your looking to identify consists of 4 criss-crossing lines.
    4. Find the number of end points on each components skeleton.
    5. Where the number of end points is 8 identify this as your component.
    6. Return to the isolated image of the (now identified) isolated component from step 2 and combine it with the original image to highlight the identified components real image.

    I've not used Matlab for machine vision before so this may or may not be the right route to take using it.


  • Advertisement
  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,092 Mod ✭✭✭✭Tar.Aldarion


    That's pretty much what I'm trying so at least somebody else thought of the same way, thanks :)

    having a bit of an idiot moment trying to put each connected component into an image with a for loop.

    I can hardcode which one to show no problem, but I want to show each of them one by one and it's not working, think I'm making a simple mistake

    % There will be mx connected components. Here you can give a value between 1 and mx for L or in a loop you can extract all connected components
    
    for j=1:mx,
    [r,c] = find(L==j);  % not working, but something like L==3 would work fine to display an image with just the third object in it. I think it is because the print out statement is not in the loop. as seen below
    rc = [r c];
    [sx sy]=size(rc);
    n1=zeros(imx,imy); 
    
    end
    
    % Storing the extracted image in an array
    for i=1:sx
        x1=rc(i,1);
        y1=rc(i,2);
        n1(x1,y1)=255;
    end 
    
    figure,imshow(n1,[]); % I think this line is in the wrong place, all I can display is the highest value in the loop, so mx. But if I move this line into the loop i just get a blank black image as output
    
    


  • Closed Accounts Posts: 5,857 ✭✭✭professore


    ianhobo wrote: »
    no, identifying a circle is not trivial.

    I'm not too sure what platform you are using (I think you said in your last post it was Matlab? If so, what matlab toolkits are you using? I imagine you have at least the ImageProcessingToolkit?)

    Don't know much about it either - used a Texas Instruments machine vision API once back in the early 90's - however surely if you can get the outline of a circle you can get the largest diameter of each solid object try fitting a mathematical circle to it and work out how good it fits - is it chi squared from memory?

    Found this online might be useful: http://www.mathworks.com/matlabcentral/fileexchange/5557


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,092 Mod ✭✭✭✭Tar.Aldarion


    Fixed that, was a simple mistake :p

    thanks professore, will have a look at that.


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,092 Mod ✭✭✭✭Tar.Aldarion


    A question, while I am also trying to figure it out.

    This code will display each of the 4 objects in separate images.

    How do I address each image separately to manipulate it, what is each image called?
    I am a compete newb, I will prob figure this out soon :p

    I want to check the amount of bays on each image, I have that code complete, now how to address each image is the obstacle, must be very easy.

    I can address all at once in a loop all right.
    for j=1:mx,
    
    [r,c] = find(L==j);  
    rc = [r c];
    [sx sy]=size(rc);
    n1=zeros(imx,imy); 
    
    
    
    for i=1:sx
        x1=rc(i,1);
        y1=rc(i,2);
        n1(x1,y1)=255;
    end % Storing the extracted image in an array
    [B]figure,imshow(n1,[]);[/B]
    
    end
    
    


  • Registered Users Posts: 586 ✭✭✭Aswerty


    Since it looks like Matlab stores images in an array surely a 3d array stores multiple images. Accessing and storing each image then just means iterating through the 3rd dimension.


  • Advertisement
  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,092 Mod ✭✭✭✭Tar.Aldarion


    Thanks I now have everything working pretty well :)


    Now what I have to do is remove this blob thing from this image somehow, which i thought would be easy but it keeps staying put :pac:

    bay.jpg


    and also remove shadowing on the original image as this is affecting the results slightly, centroids not exactly in middle and so forth.

    ux8q1.gif


  • Registered Users Posts: 586 ✭✭✭Aswerty


    Looking good!

    Are you using an adaptive threshold or just a straight forward mid level threshold? If not I'd look at doing that to see if you can improve on the problems caused through lighting.

    Adaptive threshold is max light - min light / 2

    Mid level threshold is your typical 128 grey level on a scale of 256.


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,092 Mod ✭✭✭✭Tar.Aldarion


    Thanks you are a great help.


    My threshold at the moment is
    [hi_grey]=vsg('HighestGrey',D);
    [E]=vsg('Threshold',D,hi_grey);
    h=figure;image(uint8(E));set(h,'Name','Threshold');
    

    Some sort of inbuilt level threshold in the vsg toolbox, like you said, I think...
    I was going to change this due to having to use data driven thresholding like you suggest.


    I was trying this but it is giving some similar results with shadows. :/
    %data driven threshold
    [hi_g]=vsg('HighestGrey',D);
    [lo_g]=vsg('LowestGrey',D);
    thresh=uint8((hi_g+lo_g)/2);
    [E]=vsg('Threshold',D,thresh);
    h=figure;image(uint8(E));set(h,'Name','Threshold');
    


    I think I have messed up my processing steps on the image at the start? Any advice?
    [A]=imread('image.jpg');
    h=figure;image(uint8(A));set(h,'Name','Input');
    
    % apply lowpass filter to input image
    [Z]=vsg('LowPass',A);
    h=figure;image(uint8(Z));set(h,'Name','Lowpass');
    
    [C]=vsg('Cluster',Z,5);
    h=figure;image(uint8(C));set(h,'Name','Cluster');
    
    [D]=vsg('GreyScaler',C);
    h=figure;image(uint8(D));set(h,'Name','Greyscale');
    
    % [hi_grey]=vsg('HighestGrey',D);
    % [E]=vsg('Threshold',D,hi_grey);
    % h=figure;image(uint8(E));set(h,'Name','Threshold');
    [hi_grey]=vsg('HighestGrey',D);
    [E]=vsg('Threshold',D,hi_grey);
    h=figure;image(uint8(E));set(h,'Name','Threshold');
    
    %invert image
    [F]= vsg('Inverse',E);
    h=figure;image(uint8(F));set(h,'Name','Inverse');
    
    im1=rgb2gray(F); %reapply greyscale to make medfilt2's first imput 2 dimensional.
    im1=medfilt2(im1,[3 3]); %Median filtering the image to remove noise%
    BW = edge(im1,'sobel'); %finding edges
    

    i think im greyscaling a greyscaled image a lot :p


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,092 Mod ✭✭✭✭Tar.Aldarion


    New method!
    [A]=imread('pin.jpg');
    h=figure;image(uint8(A));set(h,'Name','Input');
    
    
    %data driven threshold
    [hi_g]=vsg('HighestGrey',A);
    [lo_g]=vsg('LowestGrey',A);
    thresh=uint8((hi_g+lo_g)/2);
    [BB]=vsg('Threshold',A,thresh);
    h=figure;image(uint8(BB));set(h,'Name','Threshold');
    
    % apply lowpass filter to input image
    [C]=vsg('LowPass',BB);
    h=figure;image(uint8(C));set(h,'Name','Lowpass');
    
    %invert image
    [F]= vsg('Inverse',C);
    h=figure;image(uint8(F));set(h,'Name','Inverse');
    


    Removes some shadowing but crates a false bay on the image, raging!


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,092 Mod ✭✭✭✭Tar.Aldarion


    So now the thresholding removes the shadows somewhat, so taht is good.

    All that is left to fix is the erroneous extra bay now calculated for some reason

    bays.jpg


    maybe because I didnt do this?

    3. Whittle the components down to their skeleton forms (can't remember the specific technique that does this) such that the component your looking to identify consists of 4 criss-crossing lines.

    I just got the bays of a sobel edge detected image. Worked fine for the old thresholding but not now.


  • Registered Users Posts: 586 ✭✭✭Aswerty


    That's a problem with thresholding, improve shadow distortion and you can end up increasing glare distortion and vice verse. Not sure how thresholding works in Matlab so can't help you on that account other than to say that you should check exactly what your threshold level is to make sure that it is reasonable.

    Step 3 was if you were trying to do end point counting as opposed to bay counting. It still might be of use to you in trying to eradicate the surplus bay. Alternatively there should be other edge detection techniques you could try as opposed to Sobel these might not be any better though.


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,092 Mod ✭✭✭✭Tar.Aldarion


    I will try and mess around with the thresholding so (if I can figure out how - third day of learning matlab...), and keep it data driven. If there is another way of doign this apart from what I have done -> max light - min light / 2


    Will look through the other edge techniques, that's great, have a lot here. Thanks again.


  • Registered Users Posts: 586 ✭✭✭Aswerty


    That basic formula should give a decent threshold for what you're doing. I imagine there is a huge amount of theory on thresholding on the web since it is a common process in machine vision.

    Good luck with Matlab anyway, it is a whore of a language but it is pretty damn useful.


Advertisement