Threshold - White to black works but can it go for black particles on white background?

This code works for White particles on a black background, can it be tweaked to work for black particles on white background..?Thanks!

    s_wavelength=4; ; %use every 4th pixel for the perimeter calculation
    I = rgb2gray(RGB);
    threshold=.4 %black and white threshold
    bw = im2bw(I,threshold);
    bw = bwareaopen(bw,350);
    se = strel('disk',2);
    bw = imclose(bw,se);
    bw = imfill(bw,'holes');
    [B,L] = bwboundaries(bw,'noholes');
    imshow(label2rgb(L, @jet, [.5 .5 .5]))
    hold on
    for k = 1:length(B)
    boundary = B{k};
    plot(boundary(:,2), boundary(:,1), 'y', 'LineWidth', 1)
    stats = regionprops(L,'Area','Centroid','MajorAxisLength','MinorAxisLength')
    for k = 1:length(B)
    boundary = B{k};
    A = size(boundary)
    delta_sq = diff(boundary2).^2;
    perimeter = sum(sqrt(sum(delta_sq,2)));
    area2 = stats(k).Area;
    metric = 4*pi*area/perimeter^2;

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
Hello, and welcome.

You tagged this as a SQL question, but the content of your question doesn't look like SQL.  What language are you working with?  It might also be helpful to expand a little bit on the context of what you are doing, a little more background may help the experts suggest ideas.

rsageitAuthor Commented:
Thanks Bill, think that was tagged in error.. From what I could see this was in an Excel workbook.  This isn't my world (as is obvious, sorry) was asked if could help so just posted. Thanks!
Bill PrewIT / Software Engineering ConsultantCommented:
Well, this doesn't look like VBA, which would be the programming language used in Excel.  This actually looks a bit like MATLAB today, do you use that?  I am not well versed in that, but the code looks similar to MATLAB code I have viewed in the past.

I'll reach out to a couple of the experts recently active in the MATLAB topic area and see if they can help.

Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

rsageitAuthor Commented:
Thanks Bill, again.. yes I just heard back from the askee and yes it's MATLAB.. :)
If your MATLAB routine works for white dots on a black background you can invert to handle black dots on white.  
The function to use is imcomplement( IMAGE) and the details are here:

You can invert either the original RGB input image of the final BW output.  Try it both ways to check which you like best.

Also you can convert the RGB image directly to BW, you don't have to go through the gray scale step.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rsageitAuthor Commented:
Thanks! I will see if that works for them! Really appreciate your time.
For everyone's amusement, here is our favorite panda:
Then at line 5, bw is defined. It looks like:
Then, applying dglitch's suggestion: bwInv = imcomplement(bw);
Image Complement of bw to get: B <---> W
rsageitAuthor Commented:
Thank you, this Panda was very helpful! :) Thanks for the team work here!! Really appreciate it!
We were able to help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.