Get the percentage of dark spots
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Chathurika Sandamali
le 8 Août 2021
Commenté : Chathurika Sandamali
le 15 Août 2021
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);
0 commentaires
Réponse acceptée
Image Analyst
le 15 Août 2021
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 commentaires
Plus de réponses (1)
darova
le 8 Août 2021
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
A00 = rgb2gray(A0);
imshow(A0)
imshow(R)
4 commentaires
Image Analyst
le 15 Août 2021
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().
Voir également
Catégories
En savoir plus sur Image Processing Toolbox dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!