how to separate two dice images via matlab

조회 수: 9 (최근 30일)
Sapir
Sapir 2015년 10월 10일
답변: Image Analyst 2016년 1월 22일
Hi, I'm using "Image Analyst"'s algorithm to identify black dots on two dice. I need to detect exactly how many dots on each dice. Any suggestions?
Attached the algorithm and the image.
code:
=======
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
% Read in a color demo image.
cam = webcam(2);
set(cam, 'Brightness', 150);
set(cam, 'Saturation', 110);
pos = [93.5 175.5 452 113];
rgb = snapshot(cam);
%work - [295.5 224.5 23 38]
X2 = imcrop(rgb, [83.5 189.5 453 87]);
rgbImage = X2;
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows ,columns, numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 2, 1);
imshow(rgbImage, []);
title('Original color Image');
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
spots = rgbImage(:, :, :) < 58;
spots = spots(:, :, 1) == spots(:, :, 2) == spots(:, :, 3);
subplot(2, 2, 2);
imshow(spots, []);
title('Thresholded Red Channel');
spots = imclearborder(spots,8);
subplot(2, 2, 3);
imshow(spots, []);
title('Border Cleared');
% Fill holes
spots = imfill(spots, 'holes');
subplot(2, 2, 4);
imshow(spots, []);
title('Final Spots Image');
impixelinfo;
% Count them
L = bwlabel(spots);
[labeledImage ,numberOfSpots] = bwlabel(spots);
message = sprintf('Done!\nThe number of spots (total on both dice) is %d', numberOfSpots);
msgbox(message);
Thanks in advanced.

답변 (1개)

Image Analyst
Image Analyst 2016년 1월 22일
Try this
[~, numberOfSpots] = bwlabel(spots);

카테고리

Help CenterFile Exchange에서 Explore and Edit Images with Image Viewer App에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by