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!

 
    RGB=imread('CW5000.jpg');
    s_wavelength=4; ; %use every 4th pixel for the perimeter calculation
    I = rgb2gray(RGB);
    threshold=.4 %black and white threshold
    bw = im2bw(I,threshold);
    imshow(bw)
    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)
    end
    stats = regionprops(L,'Area','Centroid','MajorAxisLength','MinorAxisLength')
    for k = 1:length(B)
    boundary = B{k};
    A = size(boundary)
    boundary2=boundary(s_wavelength:s_wavelength:A(1,1),:);
    delta_sq = diff(boundary2).^2;
    perimeter = sum(sqrt(sum(delta_sq,2)));
    area2 = stats(k).Area;
    area=polyarea(boundary2(:,1),boundary2(:,2));
    metric = 4*pi*area/perimeter^2;
    metric_string=sprintf('%2.2f',metric);
    text(boundary(1,2)+0,boundary(1,1)+50,metric_string,'Color','k',...
    'FontSize',12,'FontWeight','bold');
    %text(boundary(1,2)+40,boundary(1,1)+100,metric_string2,'Color','c',...
    %'FontSize',12,'FontWeight','bold');
    %text(boundary(1,2)+30,boundary(1,1)+150,metric_string3,'Color','g',...
    %'FontSize',12,'FontWeight','bold');
    %text(boundary(1,2)+30,boundary(1,1)+200,metric_string4,'Color','k',...
    %'FontSize',12,'FontWeight','bold');
    end

Open in new window

rsageitAsked:
Who is Participating?

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

x
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.


»bp
0
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!
0
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.


»bp
0
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.. :)
0
d-glitchCommented:
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:  
     https://www.mathworks.com/help/images/ref/imcomplement.html

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.  
   https://www.mathworks.com/help/images/ref/im2bw.html
0

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.
0
phoffric\Commented:
For everyone's amusement, here is our favorite panda:
RGB
Then at line 5, bw is defined. It looks like:
BW
Then, applying dglitch's suggestion: bwInv = imcomplement(bw);
Image Complement of bw to get: B <---> W
0
rsageitAuthor Commented:
Thank you, this Panda was very helpful! :) Thanks for the team work here!! Really appreciate it!
0
d-glitchCommented:
We were able to help.
0
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.