• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 87
  • Last Modified:

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

0
rsageit
Asked:
rsageit
  • 4
  • 2
  • 2
  • +1
2 Solutions
 
Bill PrewCommented:
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 PrewCommented:
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
rsageitAuthor Commented:
Thanks! I will see if that works for them! Really appreciate your time.
0
 
phoffricCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now