How to focus my image segmentation algorithm
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello , I have an arrayof images of copper electrodeposited plates and I want to measure the defects like warped areas, matte areas , stressing sheared areas but the thing is that I want to make the algorith focus only on the deposition ,where the defaults are laid , not the rest of the image that could be erased . Adjoint image to depiction.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1592676/image.jpeg)
Here we see all that defects , when applying an algorithm of segmentation like
m=imread('IMG_20240111_150427815.jpg') ;
z=rgb2gray(m);
f=imbinarize(z);
imshow(f);
k=strel('disk',1)
g=imclose(f,k);
imshow(g)
tophatFiltered = imtophat(g,k);
figure
imshow(tophatFiltered)
Here can see the algoritm is applied to all over the plate but not only to the electroplated desired area , I wonder if there is a way to automatically crop that area.,I leave more examples. Thanks in advance.
0 commentaires
Réponses (2)
Image Analyst
le 17 Jan 2024
I'm not sure what part of the mask you want to be extracted by cropping, but you can use imcrop or indexing
croppedImage = originalImage(row1:row2, col1:col2, :);
Image Analyst
le 18 Jan 2024
There is a lot of variation in your image capture setup. You can make vast improvements in it, including
Lay the panels on black velvet This will make it easier to distinguish the panel from the background especially when there is a super bright specular reflection in the panel.
To reduce specular reflections, put a circular polarizer filter in front of the camera lens, and if possible put a polarizing sheet in front of the lamps. Then rotate the lens filter to eliminate as much of the specular reflections as you can.
Use a lightmeter to make sure that the lighting exposure in Lux is the same from shot to shot.
If you don't care about the original color (don't need it to be natural) you can try different colored lights to accentuate the color difference between the copper and the brass. Try red, yellow, green, and blue lights and see which one gives you the most contrast between the two regions.
I made a stab at color segmentation for the first image. The color masking parameters may be different for different images, that's why I gave you the tips above -- so that we can use the same color segmentation parameters for all images.
Try this:
% Demo by Image Analyst
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 = 16;
markerSize = 20;
%--------------------------------------------------------------------------------------------------------
% READ IN TEST IMAGE
folder = pwd;
baseFileName = 'copper1.jpg';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(2, 2, 1);
imshow(rgbImage, []);
impixelinfo;
axis('on', 'image');
title('Original RGB Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Update the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Maximize window.
g = gcf;
g.WindowState = 'maximized';
g.Name = 'Demo by Image Analyst';
g.NumberTitle = 'off';
drawnow;
%--------------------------------------------------------------------------------------------
% Threshold image.
[mask,maskedRGBImage] = createMask(rgbImage);
% Fill holes.
mask = imfill(mask, 'holes');
% Take largest blob.
mask = bwareafilt(mask, 1);
subplot(2, 2, 2);
imshow(mask)
impixelinfo;
axis('on', 'image');
title('Mask', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
%--------------------------------------------------------------------------------------------
% Apply the modified mask to the original image.
% Mask image by multiplying each channel by the mask.
maskedRGBImage = rgbImage .* cast(mask, 'like', rgbImage); % R2016b or later. Works for gray scale as well as RGB Color images.
subplot(2, 2, 3);
imshow(maskedRGBImage)
impixelinfo;
axis('on', 'image');
title('Masked RGBImage', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
%================================================================================================
function [BW,maskedRGBImage] = createMask(RGB)
%createMask Threshold RGB image using auto-generated code from colorThresholder app.
% [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using
% auto-generated code from the colorThresholder app. The colorspace and
% range for each channel of the colorspace were set within the app. The
% segmentation mask is returned in BW, and a composite of the mask and
% original RGB images is returned in maskedRGBImage.
% Auto-generated by colorThresholder app on 17-Jan-2024
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.798;
channel1Max = 0.057;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.000;
channel2Max = 1.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.000;
channel3Max = 1.000;
% Create mask based on chosen histogram thresholds
sliderBW = ( (I(:,:,1) >= channel1Min) | (I(:,:,1) <= channel1Max) ) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
% Initialize output masked image based on input image.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1593561/image.png)
Obviously the result would be better if part of the copper was not super bright white. It has no color there and it's close to the background brightness so it gets excluded. It would be better to adjust your image capture to make all the coper region copper colored. I don't know if it will work well for your other images, for the reasons I already explained.
So now that you have the copper area you can do other analyses for the various different features you want to measure.
0 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!