Black shapes against white background

1 visualizzazione (ultimi 30 giorni)
Kacper
Kacper il 8 Mar 2024
Modificato: DGM il 18 Mar 2024
I am trying to process images of 3d blocks so that the side facing the camera becomes black against a white background, just like below
But when I run my code I get the result below
Does anybody know what I should do? Here's the main loop of my code:
for i = 1:numel(imageFiles)
imagePath = fullfile(imageFolderPath, imageFiles(i).name);
originalImage = imread(imagePath);
grayImage = rgb2gray(originalImage);
darkThreshold = 78;
redMask = originalImage(:,:,1) < darkThreshold;
greenMask = originalImage(:,:,2) < darkThreshold;
blueMask = originalImage(:,:,3) < darkThreshold;
binaryMask = ~(redMask | greenMask | blueMask);
binaryMask = imopen(binaryMask, strel('disk', 20));
binaryMask = imclose(binaryMask, strel('disk', 15));
outputImage = uint8(cat(3, 255 * binaryMask, 255 * binaryMask, 255 * binaryMask));
figure;
subplot(1, 2, 1), imshow(originalImage), title('Original Image');
subplot(1, 2, 2), imshow(outputImage), title('Processed Image');
end
  1 Commento
DGM
DGM il 18 Mar 2024
Modificato: DGM il 18 Mar 2024
This is where having a telecentric lens would be helpful, though it's hard to say that it's warranted without knowing more about the actual task.
Otherwise, attach examples of the various images from the set, as any applied code would have to accomodate the variation within the set. A single screenshot isn't really sufficient.

Accedi per commentare.

Risposte (1)

Aishwarya
Aishwarya il 18 Mar 2024
Based on the information provided, I understand that you want to process the image in such a way that the 3D blocks are represented as 2D black shapes on a white background.
After reviewing the code, below are a few suggestions that could help in getting the desired result:
  • Adjust Structuring Element Size: As the image shows the edges are overly smoothed and close objects are merged, this could be because the size of the structuring element might be large for your images. Consider reducing the size of the structuring element used in the “imopen” and “imclose” functions as shown in the code snippet below.
binaryMask = imopen(binaryMask, strel('disk', 5)); % Adjust the structuring element size
binaryMask = imclose(binaryMask, strel('disk', 5)); % Adjust the structuring element size
  • Review the Morphological Operations: Experiment with other morphological operations like “imerode” and “imdilate” with different structuring elements. Using “imerode” and “imdilate” functions separately allows for more customized morphological adjustments.
  • Consider Adaptive Thresholding: Try using adaptive thresholding techniques (like “addatthresh” and “imbinarize”), which might better accommodate varying lighting conditions across different parts of the image. An example implementation of adaptive thresholding is shown below, adjust the parameters according to your images and desired result.
% Adaptive Thresholding
T = adaptthresh(adjustedImage, 0.4, 'ForegroundPolarity','dark');
binaryMask = imbinarize(adjustedImage, T);
You can refer to the below documentation for more information about the functions used:
I hope this information is helpful!

Categorie

Scopri di più su Image Processing Toolbox in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by