Get the percentage of dark spots

조회 수: 11(최근 30일)
Chathurika Sandamali
Chathurika Sandamali 2021년 8월 8일
댓글: Chathurika Sandamali 2021년 8월 15일
I want measure the percentage of patches in the stick. I already find the patches but I cannot find the how many patches are selected (red color spots).
Below is my code. Please help to find the count.
Img=imread('Images\IMG_5058.jpg');
Gimg=rgb2gray(Img);
figure(1);imshow(Gimg);
figure(2);imshow(Img);
%grey point less than 95
BW=Gimg<95;
%Find Weighted Centroid of the chosen dots and plot them together with original image
rp=regionprops(BW,Gimg,'WeightedCentroid');
% disp(rp);
n=numel(rp);
disp(n);
for j=1:n
Position(j,1)=rp(j).WeightedCentroid(1);
% disp(Position(j,1));
Position(j,2)=rp(j).WeightedCentroid(2);
% disp(Position(j,2));
end
for i=1:n-1
if (Position(i+1,1)-Position(i,1))<5 || (Position(i+1,2)-Position(i,2))<5
Position(i+1,1)=1/2*(Position(i,1)+Position(i+1,1));
Position(i+1,2)=1/2*(Position(i,2)+Position(i+1,2));
Position(i,1)=0;Position(i,2)=0;
end
end
[m,n]=size(Position);
% fprintf('%d - %d\n', m,n);
figure(3);imshow(Img); axis image; hold on;
lunt=0;
for i=1:m
for j=1:n-1
arr(:, j) = plot(Position(i,j),Position(i,j+1),'r*')
% plot(x(idx),y(idx),'r','linewidth',3)
% display(arr);
end
end
% fprintf('%d - %d\n', m,n);
for x=1:Position
fprintf('%d\n', Position(:,x));
lunt = lunt+1;
fprintf('Patch count = %d\n', lunt);
end
sizeP = length(Position);
% fprintf('%d\n', Position);
Position(all(Position==0,2),:)=[];
display(Position);
% fprintf('%f\n', Position);
% Position = Position*2;
fprintf('count = %d\n', lunt);
fprintf('%d\n', pastCount);

채택된 답변

Image Analyst
Image Analyst 2021년 8월 15일
Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 15;
baseFileName = 'IMG_5058.JPG';
fullFileName = fullfile('Images', baseFileName);
Img=imread(fullFileName);
Gimg=rgb2gray(Img);
figure(1);imshow(Gimg);
figure(2);imshow(Img);
%grey point less than 95
BW=Gimg<95;
%Find Weighted Centroid of the chosen dots and plot them together with original image
rp=regionprops(BW,Gimg,'WeightedCentroid');
% disp(rp);
n=numel(rp);
disp(n);
for j=1:n
Position(j,1)=rp(j).WeightedCentroid(1);
% disp(Position(j,1));
Position(j,2)=rp(j).WeightedCentroid(2);
% disp(Position(j,2));
end
for i=1:n-1
if (Position(i+1,1)-Position(i,1))<5 || (Position(i+1,2)-Position(i,2))<5
Position(i+1,1)=1/2*(Position(i,1)+Position(i+1,1));
Position(i+1,2)=1/2*(Position(i,2)+Position(i+1,2));
Position(i,1)=0;Position(i,2)=0;
end
end
[m,n]=size(Position);
% fprintf('%d - %d\n', m,n);
figure(3);imshow(Img); axis image; hold on;
lunt=0;
spotCount = 0;
for i=1:m
for j=1:n-1
arr(:, j) = plot(Position(i,j),Position(i,j+1),'r*');
% plot(x(idx),y(idx),'r','linewidth',3)
% display(arr);
spotCount = spotCount + 1;
end
end
% fprintf('%d - %d\n', m,n);
for x=1:Position
fprintf('%d\n', Position(:,x));
lunt = lunt+1;
fprintf('Patch count = %d\n', lunt);
end
sizeP = length(Position);
% fprintf('%d\n', Position);
Position(all(Position==0,2),:)=[];
display(Position);
% fprintf('%f\n', Position);
% Position = Position*2;
fprintf('Spot count = %d\n', spotCount);
fprintf('lunt = %d\n', lunt);
You get:
Spot count = 3089
lunt = 0
  댓글 수: 7
Chathurika Sandamali
Chathurika Sandamali 2021년 8월 15일
I did that. Thank you. @Image Analyst

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

추가 답변(1개)

darova
darova 2021년 8월 8일
Use first matrix of an image (red channel)
A0 = imread('image.png');
A1 = ~im2bw(A0,0.8); % select the stick
A2 = A0(:,:,1) > 210; % select red dots and background
R = A1 & A2; % red dots
P = sum(R(:))/sum(A1(:)) % percentage
P = 0.1199
A00 = rgb2gray(A0);
imshow(A0)
imshow(R)
  댓글 수: 4
Image Analyst
Image Analyst 2021년 8월 15일
Correct. That particular image does not have red spots on it. darova's code was meant for your images that DO have red spots on them (meaning actually burned into and part of the image), like the one in your original post. Those spots were made by you calling plot() to put a red spot on the point. So to count the number of red spots with your code, you just need to count the number of times you call plot().

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

Community Treasure Hunt

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

Start Hunting!

Translated by