Detecting a washer using Image Processing

6 views (last 30 days)
BERG
BERG on 20 Feb 2020
Answered: Raynier Suresh on 20 Mar 2020
Hi im trying to detect a washer in an image using image processing just to lean matlab i saw a good tuto very interesting and tried it and it work now im tried to write the code using the same tmethod as they do but the difference is i have as input an grayscale image and i cannot continue because i'm a little bit lost could someone help me please here is the code i'm trying to write using the same method but it looks like i made it wrong
%% Detecting a washer using Image Processing
% this programm show how to detect a washer in a picutre
% morphology an object can be easily detected with sufficient contrast from the background
clear ALL
clc;
close all
%% firt step read an image
I = imread('Washer.png', 'png');
% imtool(img);
subplot(2,2,1)
imshow(I), title('original image');
%% second step convert original image to gray
gray = rgb2lab(I);
subplot(2,2,2)
imshow(gray), title ('gray image');
%% third step define classes
% this classes will be use to calculate the mean average on the pixel that
% will be use in the nearestNeighbour.
Washer = gray(168:229, 327:409, :);
backgr = gray(93:108, 93:108, :);
%% fourth step save image in a classe
% to save the picture in a classe we will use the nearestNeighbour method
% there we will compare the values of the pixel of the object and the image
% and chose the smallest one which correspond to the object in the image
% Save averages from classes in a matrix
classes = [[mean2(Washer(:,:,2)), mean2(Washer(:,:,3))]', [mean2(backgr(:,:,2)), mean2(backgr(:,:,3))]'];
[height, width, channels] = size(gray); % size of the image
% define the new image
classimg = zeros(height,width);
for i = 1:height
for j = 1:width
% display the new image by finding the smallest distance to the pixel's object
classimg(i,j) = nearestNeighbour2d(gray(i,j,2:3), classes);
end
end
% Show the class-image
subplot(2,2,3);
imshow(classimg, []); title ('new image');
% Index in position 3 exceeds array bounds (must not exceed 1 )
% Error in washer (line 36)
% classes = [[mean2(Washer(:,:,2)), mean2(Washer(:,:,3))]', [mean2(backgr(:,:,2)), mean2(backgr(:,:,3))]'];
could someone have an idea how to fix it thank

Answers (1)

Raynier Suresh
Raynier Suresh on 20 Mar 2020
To convert rgb to gray you could use “rgb2gray”. To detect any object in an image you could use either image segmentation or you can try edge detection if you know the structure of the object. The vision.ForegroundDetector is a tool which could help you in separating the foreground object from the background.
Referring to these links might be useful:

Community Treasure Hunt

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

Start Hunting!