How do I count certain values within a column of a table?

조회 수: 3 (최근 30일)
Maizy Troxell
Maizy Troxell 2022년 1월 25일
댓글: Star Strider 2022년 1월 30일
I am currently working on a project that aims to create a program that counts cells on a specific image using image processing. I currently have the image altered so that the cells apear as white circular shapes on a solid black background (pictured below.)
I then used the code:
stats = regionprops('table',WBcells,'Centroid','MajorAxisLength','MinorAxisLength');
which produced the following table:
Now I need to figureout how to count values from the MajorAxisLength Column that are larger than 5 pixels and smaller than 26 pixels. If someone could please help me with this or give suggestions on a better way to count the cells that would be very helpful!

답변 (2개)

Star Strider
Star Strider 2022년 1월 25일
One approach —
MajorAxisLength = 30*rand(15,1)
MajorAxisLength = 15×1
25.3896 26.0832 15.9106 27.0072 2.4364 14.0032 12.0105 5.0400 22.0311 20.4358
Lv = (MajorAxisLength >= 5) & (MajorAxisLength <= 26)
Lv = 15×1 logical array
1 0 1 0 0 1 1 1 1 1
MeetCriteria = nnz(Lv)
MeetCriteria = 9
PctMeetCriteria = 100*MeetCriteria/numel(MajorAxisLength)
PctMeetCriteria = 60
See if that produces the desired result.
.
  댓글 수: 2
Maizy Troxell
Maizy Troxell 2022년 1월 30일
This does count all of the cells only 60 out of 173.
Star Strider
Star Strider 2022년 1월 30일
It counts all the cells and returns true only for those that meet the criteria. Then, it counts the true values to produce the ‘MeetCriteria’ variable. These can be combined into one line if desired:
MeetCriteria = nnz((MajorAxisLength >= 5) & (MajorAxisLength <= 26))
I kept them separate to demonstrate how the code works. (The ‘PctMeetCriteria’ is supplied to standardise the result.)

댓글을 달려면 로그인하십시오.


yanqi liu
yanqi liu 2022년 1월 26일
clc; clear all; close all;
img = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/873730/image.png');
im = imcrop(img, [88 117 410 318]);
J = rgb2hsv(im);
bw = im2bw(mat2gray(J(:,:,2)), 0.6);
WBcells = imopen(bw, strel('disk', 3));
stats = regionprops(WBcells,'Centroid','MajorAxisLength','MinorAxisLength');
% count values from the MajorAxisLength Column that are larger than 5 pixels and smaller than 26 pixels
ma = cat(1, stats.MajorAxisLength);
ce = cat(1, stats.Centroid);
ind = ma > 5 & ma < 26;
figure;
imshow(im);
hold on;
plot(ce(ind,1), ce(ind,2), 'r*');
  댓글 수: 1
Maizy Troxell
Maizy Troxell 2022년 1월 30일
This works great, however not all of the cells are marked. Also this program does not spit out a final count of the cells which is what I really needed help with.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by