To locate red color and display its pixel size.

9 ビュー (過去 30 日間)
Navodita Das
Navodita Das 2020 年 5 月 31 日
回答済み: darova 2020 年 6 月 14 日
Hello, can somebody please help me to correct the code. Below code is not giving expected output. It needs to detect the red color, locate it with bounding boxes and give its pixel size. But output image is almost black with only one pixel size and not even detecting all the red colors.
Input image-
clc
imgdata = imread('C:\Users\asus\Desktop\Paper meet 2020\454.jpg');
diff_im = imsubtract(imgdata(:,:,1), rgb2gray(imgdata));
%Use a median filter to filter out noise
diff_im = medfilt2(diff_im, [3 3]);
diff_im = im2bw(diff_im,0.18); % grayscle to binary image
left_red = bwareaopen(diff_im,300);% removes all pixels less than 300 from diff_im
bw = bwlabel(left_red, 8);% leftImage.............returns a label matrix, where conn specifies the connectivity.
L_stats = regionprops(bw, 'BoundingBox', 'Centroid'); %returns measurements for the set of properties specified by properties for each labeled region in the image I
% Display the image
imshow(left_red)
hold on
for object = 1:length(L_stats)
bb = L_stats(object).BoundingBox;
bc = L_stats(object).Centroid;
rectangle('Position',bb,'EdgeColor','r','LineWidth',2)
plot(bc(1),bc(2), '-m+')
a=text(bc(1)+15,bc(2), strcat('X: ', num2str(round(bc(1))), ' Y: ', num2str(round(bc(2)))));
L_cor(length(L_stats),1)=str2num(num2str(round(bc(1)))); %x
L_cor(length(L_stats),2)=str2num(num2str(round(bc(2)))) ; %y
set(a, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'black');
end
hold of
output image-
output expected similar to-
How do I correct the code and output?

回答 (1 件)

darova
darova 2020 年 6 月 14 日
Here is start
I0 = imread('image.jpeg');
ix = I0(:,:,1) > 220 & I0(:,:,2) < 50 & I0(:,:,3) < 50;
mask = uint8( repmat(ix,[1 1 3]) );
imshowpair(I0,I0.*mask)

Community Treasure Hunt

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

Start Hunting!

Translated by