Black shapes against white background
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1638026/image.png)
But when I run my code I get the result below
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1638031/image.png)
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 commentaire
DGM
le 18 Mar 2024
Modifié(e) : DGM
le 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.
Réponses (1)
Aishwarya
le 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:
- "imerode" function: https://www.mathworks.com/help/images/ref/imerode.html
- "imdilate" function: https://www.mathworks.com/help/images/ref/imdilate.html
- "adaptthresh" function: https://www.mathworks.com/help/images/ref/adaptthresh.html
- "imbinarize" function: https://www.mathworks.com/help/images/ref/imbinarize.html
I hope this information is helpful!
0 commentaires
Voir également
Catégories
En savoir plus sur 3-D Volumetric Image Processing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!